2018-06-27 03:31:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-05 13:18:15 -04:00
|
|
|
require 'sidekiq/api'
|
|
|
|
|
2017-11-28 11:08:30 -05:00
|
|
|
Sidekiq::Worker.extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ApplicationWorker
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-12-12 11:21:00 -05:00
|
|
|
include Sidekiq::Worker # rubocop:disable Cop/IncludeSidekiqWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
|
|
|
|
included do
|
2017-11-28 11:16:50 -05:00
|
|
|
set_queue
|
2017-11-28 11:08:30 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2017-11-28 11:16:50 -05:00
|
|
|
def inherited(subclass)
|
|
|
|
subclass.set_queue
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_queue
|
|
|
|
queue_name = [queue_namespace, base_queue_name].compact.join(':')
|
|
|
|
|
2017-12-12 11:21:00 -05:00
|
|
|
sidekiq_options queue: queue_name # rubocop:disable Cop/SidekiqOptionsQueue
|
2017-11-28 11:16:50 -05:00
|
|
|
end
|
|
|
|
|
2017-11-28 11:08:30 -05:00
|
|
|
def base_queue_name
|
|
|
|
name
|
|
|
|
.sub(/\AGitlab::/, '')
|
|
|
|
.sub(/Worker\z/, '')
|
|
|
|
.underscore
|
|
|
|
.tr('/', '_')
|
|
|
|
end
|
|
|
|
|
2017-11-28 11:16:50 -05:00
|
|
|
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
|
|
|
|
|
2017-11-28 11:08:30 -05:00
|
|
|
def queue
|
|
|
|
get_sidekiq_options['queue'].to_s
|
|
|
|
end
|
2017-11-29 10:30:17 -05:00
|
|
|
|
2019-07-05 13:18:15 -04:00
|
|
|
def queue_size
|
|
|
|
Sidekiq::Queue.new(queue).size
|
|
|
|
end
|
|
|
|
|
2017-11-29 10:30:17 -05:00
|
|
|
def bulk_perform_async(args_list)
|
|
|
|
Sidekiq::Client.push_bulk('class' => self, 'args' => args_list)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_perform_in(delay, args_list)
|
|
|
|
now = Time.now.to_i
|
|
|
|
schedule = now + delay.to_i
|
|
|
|
|
|
|
|
if schedule <= now
|
2019-04-10 09:30:09 -04:00
|
|
|
raise ArgumentError, _('The schedule time must be in the future!')
|
2017-11-29 10:30:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Sidekiq::Client.push_bulk('class' => self, 'args' => args_list, 'at' => schedule)
|
|
|
|
end
|
2017-11-28 11:08:30 -05:00
|
|
|
end
|
|
|
|
end
|