2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
require 'sidekiq/api'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
Sidekiq::Worker.extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ApplicationWorker
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
include Sidekiq::Worker # rubocop:disable Cop/IncludeSidekiqWorker
|
2019-12-21 20:55:43 +05:30
|
|
|
include WorkerAttributes
|
2020-03-13 15:44:24 +05:30
|
|
|
include WorkerContext
|
2020-10-24 23:57:45 +05:30
|
|
|
include Gitlab::SidekiqVersioning::Worker
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
LOGGING_EXTRA_KEY = 'extra'
|
2021-09-04 01:27:46 +05:30
|
|
|
DEFAULT_DELAY_INTERVAL = 1
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
included do
|
|
|
|
set_queue
|
2021-06-08 01:23:25 +05:30
|
|
|
after_set_class_attribute { set_queue }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
def structured_payload(payload = {})
|
2021-04-29 21:17:54 +05:30
|
|
|
context = Gitlab::ApplicationContext.current.merge(
|
2021-01-29 00:20:46 +05:30
|
|
|
'class' => self.class.name,
|
2020-04-08 14:13:33 +05:30
|
|
|
'job_status' => 'running',
|
|
|
|
'queue' => self.class.queue,
|
|
|
|
'jid' => jid
|
|
|
|
)
|
|
|
|
|
|
|
|
payload.stringify_keys.merge(context)
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def log_extra_metadata_on_done(key, value)
|
|
|
|
@done_log_extra_metadata ||= {}
|
|
|
|
@done_log_extra_metadata[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def logging_extras
|
|
|
|
return {} unless @done_log_extra_metadata
|
|
|
|
|
|
|
|
# Prefix keys with class name to avoid conflicts in Elasticsearch types.
|
|
|
|
# Also prefix with "extra." so that we know to log these new fields.
|
|
|
|
@done_log_extra_metadata.transform_keys do |k|
|
|
|
|
"#{LOGGING_EXTRA_KEY}.#{self.class.name.gsub("::", "_").underscore}.#{k}"
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
class_methods do
|
2021-09-30 23:02:18 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def inherited(subclass)
|
|
|
|
subclass.set_queue
|
2021-06-08 01:23:25 +05:30
|
|
|
subclass.after_set_class_attribute { subclass.set_queue }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def generated_queue_name
|
|
|
|
Gitlab::SidekiqConfig::WorkerRouter.queue_name_from_worker_name(self)
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
override :validate_worker_attributes!
|
|
|
|
def validate_worker_attributes!
|
|
|
|
super
|
|
|
|
|
|
|
|
# Since the delayed data_consistency will use sidekiq built in retry mechanism, it is required that this mechanism
|
|
|
|
# is not disabled.
|
|
|
|
if retry_disabled? && get_data_consistency == :delayed
|
|
|
|
raise ArgumentError, "Retry support cannot be disabled if data_consistency is set to :delayed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if sidekiq retry support is disabled
|
|
|
|
def retry_disabled?
|
|
|
|
get_sidekiq_options['retry'] == 0 || get_sidekiq_options['retry'] == false
|
|
|
|
end
|
|
|
|
|
|
|
|
override :sidekiq_options
|
|
|
|
def sidekiq_options(opts = {})
|
|
|
|
super.tap do
|
|
|
|
validate_worker_attributes!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def perform_async(*args)
|
|
|
|
# Worker execution for workers with data_consistency set to :delayed or :sticky
|
|
|
|
# will be delayed to give replication enough time to complete
|
|
|
|
if utilizes_load_balancing_capabilities?
|
|
|
|
perform_in(delay_interval, *args)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def set_queue
|
2021-06-08 01:23:25 +05:30
|
|
|
queue_name = ::Gitlab::SidekiqConfig::WorkerRouter.global.route(self)
|
2018-03-17 18:26:18 +05:30
|
|
|
sidekiq_options queue: queue_name # rubocop:disable Cop/SidekiqOptionsQueue
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_namespace(new_namespace = nil)
|
|
|
|
if new_namespace
|
|
|
|
sidekiq_options queue_namespace: new_namespace
|
|
|
|
|
|
|
|
set_queue
|
|
|
|
else
|
|
|
|
get_sidekiq_options['queue_namespace']&.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue
|
|
|
|
get_sidekiq_options['queue'].to_s
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
# Set/get which arguments can be logged and sent to Sentry.
|
|
|
|
#
|
|
|
|
# Numeric arguments are logged by default, so there is no need to
|
|
|
|
# list those.
|
|
|
|
#
|
|
|
|
# Non-numeric arguments must be listed by position, as Sidekiq
|
|
|
|
# cannot see argument names.
|
|
|
|
#
|
|
|
|
def loggable_arguments(*args)
|
|
|
|
if args.any?
|
|
|
|
@loggable_arguments = args
|
|
|
|
else
|
|
|
|
@loggable_arguments || []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def queue_size
|
|
|
|
Sidekiq::Queue.new(queue).size
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def bulk_perform_async(args_list)
|
|
|
|
Sidekiq::Client.push_bulk('class' => self, 'args' => args_list)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def bulk_perform_in(delay, args_list, batch_size: nil, batch_delay: nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
now = Time.now.to_i
|
|
|
|
schedule = now + delay.to_i
|
|
|
|
|
|
|
|
if schedule <= now
|
2019-07-31 22:56:46 +05:30
|
|
|
raise ArgumentError, _('The schedule time must be in the future!')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
if batch_size && batch_delay
|
|
|
|
args_list.each_slice(batch_size.to_i).with_index do |args_batch, idx|
|
|
|
|
batch_schedule = schedule + idx * batch_delay.to_i
|
|
|
|
Sidekiq::Client.push_bulk('class' => self, 'args' => args_batch, 'at' => batch_schedule)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Sidekiq::Client.push_bulk('class' => self, 'args' => args_list, 'at' => schedule)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def delay_interval
|
|
|
|
DEFAULT_DELAY_INTERVAL.seconds
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|