我想知道是否有一种方法可以在ActiveJob中排队多个作业?类似于sidekiq的push_bulk。
Sidekiq::Client.push_bulk(sidekiq_items)
我需要排列数千个工作,并且逐个执行太耗时了。
我想知道是否有一种方法可以在ActiveJob中排队多个作业?类似于sidekiq的push_bulk。
Sidekiq::Client.push_bulk(sidekiq_items)
class ApplicationJob < ActiveJob::Base
def self.perform_later_bulk(array_of_args)
sidekiq_args = array_of_args.map { |args| [new(*args).serialize] }
Sidekiq::Client.push_bulk(
'class' => ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper,
'wrapped' => self,
'queue' => sidekiq_args[0][0]['queue_name'],
'args' => sidekiq_args
)
end
end
def SomeJob < ApplicationJob
def perform(arg1, arg2)
# etc
end
end
# Usage: SomeJob.perform_later_bulk([[arg1, arg2], [arg1, arg2], ...])
已在Rails 7上测试,但至少可以向后兼容到5.2。