2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
module WaitableWorker
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
class_methods do
|
2018-03-27 19:54:05 +05:30
|
|
|
# Schedules multiple jobs and waits for them to be completed.
|
2022-08-13 15:12:31 +05:30
|
|
|
def bulk_perform_and_wait(args_list)
|
2018-03-27 19:54:05 +05:30
|
|
|
# Short-circuit: it's more efficient to do small numbers of jobs inline
|
2022-08-27 11:52:29 +05:30
|
|
|
if args_list.size == 1 && !always_async_project_authorizations_refresh?
|
2022-08-13 15:12:31 +05:30
|
|
|
return bulk_perform_inline(args_list)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
bulk_perform_async(args_list)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Performs multiple jobs directly. Failed jobs will be put into sidekiq so
|
|
|
|
# they can benefit from retries
|
|
|
|
def bulk_perform_inline(args_list)
|
|
|
|
failed = []
|
|
|
|
|
|
|
|
args_list.each do |args|
|
2021-09-30 23:02:18 +05:30
|
|
|
worker = new
|
|
|
|
Gitlab::AppJsonLogger.info(worker.structured_payload(message: 'running inline'))
|
|
|
|
worker.perform(*args)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2019-07-07 11:18:12 +05:30
|
|
|
failed << args
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
bulk_perform_async(failed) if failed.present?
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
def always_async_project_authorizations_refresh?
|
|
|
|
Feature.enabled?(:always_async_project_authorizations_refresh)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def perform(*args)
|
|
|
|
notify_key = args.pop if Gitlab::JobWaiter.key?(args.last)
|
|
|
|
|
|
|
|
super(*args)
|
|
|
|
ensure
|
|
|
|
Gitlab::JobWaiter.notify(notify_key, jid) if notify_key
|
|
|
|
end
|
|
|
|
end
|