2018-06-27 07:31:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-20 17:37:38 +00:00
|
|
|
require 'active_job/arguments'
|
|
|
|
|
|
|
|
module MailScheduler
|
2020-02-19 18:09:10 +00:00
|
|
|
class NotificationServiceWorker # rubocop:disable Scalability/IdempotentWorker
|
2018-04-20 17:37:38 +00:00
|
|
|
include ApplicationWorker
|
2021-04-30 18:10:09 +00:00
|
|
|
|
2021-07-21 12:09:35 +00:00
|
|
|
data_consistency :always
|
|
|
|
|
2021-04-30 18:10:09 +00:00
|
|
|
sidekiq_options retry: 3
|
2018-04-20 17:37:38 +00:00
|
|
|
include MailSchedulerQueue
|
|
|
|
|
2021-10-27 15:13:41 +00:00
|
|
|
feature_category :team_planning
|
2019-10-30 15:14:17 +00:00
|
|
|
worker_resource_boundary :cpu
|
2020-06-12 12:08:56 +00:00
|
|
|
loggable_arguments 0
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2018-04-20 17:37:38 +00:00
|
|
|
def perform(meth, *args)
|
2018-12-21 12:32:25 +00:00
|
|
|
check_arguments!(args)
|
2018-04-20 17:37:38 +00:00
|
|
|
|
2018-12-21 12:32:25 +00:00
|
|
|
deserialized_args = ActiveJob::Arguments.deserialize(args)
|
2018-04-20 17:37:38 +00:00
|
|
|
notification_service.public_send(meth, *deserialized_args) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
rescue ActiveJob::DeserializationError
|
2018-12-21 12:32:25 +00:00
|
|
|
# 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-04-20 17:37:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.perform_async(*args)
|
2020-01-29 18:08:47 +00:00
|
|
|
super(*ActiveJob::Arguments.serialize(args))
|
2018-04-20 17:37:38 +00:00
|
|
|
end
|
2018-12-21 12:32:25 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-01-29 18:08:47 +00:00
|
|
|
# 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,
|
2018-12-21 12:32:25 +00:00
|
|
|
# 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-01-29 18:08:47 +00:00
|
|
|
if arg.class.in?(PERMITTED_TYPES)
|
2018-12-21 12:32:25 +00:00
|
|
|
raise(ArgumentError, "Argument `#{arg}` cannot be deserialized because of its type")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-20 17:37:38 +00:00
|
|
|
end
|
|
|
|
end
|