debian-mirror-gitlab/app/workers/mail_scheduler/notification_service_worker.rb

52 lines
1.6 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
require 'active_job/arguments'
module MailScheduler
class NotificationServiceWorker
include ApplicationWorker
include MailSchedulerQueue
2019-12-21 20:55:43 +05:30
feature_category :issue_tracking
2019-12-26 22:10:19 +05:30
worker_resource_boundary :cpu
2019-12-21 20:55:43 +05:30
2018-10-15 14:42:47 +05:30
def perform(meth, *args)
2019-02-15 15:39:39 +05:30
check_arguments!(args)
2018-10-15 14:42:47 +05:30
2019-02-15 15:39:39 +05:30
deserialized_args = ActiveJob::Arguments.deserialize(args)
2018-10-15 14:42:47 +05:30
notification_service.public_send(meth, *deserialized_args) # rubocop:disable GitlabSecurity/PublicSend
rescue ActiveJob::DeserializationError
2019-02-15 15:39:39 +05:30
# No-op.
# This exception gets raised when an argument
# is correct (deserializeable), but it still cannot be deserialized.
# This can happen when an object has been deleted after
# rails passes this job to sidekiq, but before
# sidekiq gets it for execution.
# In this case just do nothing.
2018-10-15 14:42:47 +05:30
end
def self.perform_async(*args)
2020-03-13 15:44:24 +05:30
super(*ActiveJob::Arguments.serialize(args))
2018-10-15 14:42:47 +05:30
end
2019-02-15 15:39:39 +05:30
private
2020-03-13 15:44:24 +05:30
# This is copied over from https://github.com/rails/rails/blob/v6.0.1/activejob/lib/active_job/arguments.rb#L50
# because it is declared as a private constant
PERMITTED_TYPES = [NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass].freeze
private_constant :PERMITTED_TYPES
# If an argument is in the PERMITTED_TYPES list,
2019-02-15 15:39:39 +05:30
# it means the argument cannot be deserialized.
# Which means there's something wrong with our code.
def check_arguments!(args)
args.each do |arg|
2020-03-13 15:44:24 +05:30
if arg.class.in?(PERMITTED_TYPES)
2019-02-15 15:39:39 +05:30
raise(ArgumentError, "Argument `#{arg}` cannot be deserialized because of its type")
end
end
end
2018-10-15 14:42:47 +05:30
end
end