Files
redux-scraper/app/lib/has_bulk_enqueue_jobs.rb
2025-01-01 03:29:53 +00:00

30 lines
941 B
Ruby

# typed: true
module HasBulkEnqueueJobs
extend ActiveSupport::Concern
included do
def bulk_enqueue_jobs(&block)
# must set total_limit to 0 before generating jobs,
# else the instances will have their own total_limit set
old_limit = Scraper::JobBase.good_job_concurrency_config[:total_limit]
Scraper::JobBase.good_job_concurrency_config[:total_limit] = nil
key_to_job =
GoodJob::Bulk
.capture(&block)
.map { |job| [job.good_job_concurrency_key, job] }
.to_h
ReduxApplicationRecord.transaction do
existing_keys =
GoodJob::Job.where(concurrency_key: key_to_job.keys).pluck(
:concurrency_key,
)
existing_keys.each { |key| key_to_job.delete(key) }
GoodJob::Bulk.enqueue(key_to_job.values)
end
ensure
Scraper::JobBase.good_job_concurrency_config[:total_limit] = old_limit
end
end
end