如何手动运行Sidekiq作业

时间:2015-02-25 16:07:20

标签: ruby-on-rails-3 sidekiq

我有一个使用Sidekiq的应用程序。 Web服务器进程有时会把工作放在Sidekiq上,但我不一定要让工作人员运行。是否有一个可以从Rails控制台调用的实用程序,它可以从Redis队列中取出一个作业并运行相应的Sidekiq工作程序?

2 个答案:

答案 0 :(得分:4)

这是您可能需要进行修改才能获得所需工作的一种方式(可能像g8M suggests above),但应该可以解决问题:

> job = Sidekiq::Queue.new("your_queue").first
> job.klass.constantize.new.perform(*job.args)

如果要删除作业:

> job.delete

在sidekiq 5.2.3上进行了测试。

答案 1 :(得分:2)

我不会试图破解sidekiq的API来手动运行作业,因为它可能会留下一些不需要的内部状态,但我相信以下代码可以正常工作

# Fetch the Queue
queue = Sidekiq::Queue.new # default queue
# OR
# queue = Sidekiq::Queue.new(:my_queue_name)

# Fetch the job
# job = queue.first
# OR
job = queue.find do |job|
  meta = job.args.first
  # => {"job_class" => "MyJob", "job_id"=>"1afe424a-f878-44f2-af1e-e299faee7e7f", "queue_name"=>"my_queue_name", "arguments"=>["Arg1", "Arg2", ...]}

  meta['job_class'] == 'MyJob' && meta['arguments'].first == 'Arg1'
end

# Removes from queue so it doesn't get processed twice
job.delete

meta = job.args.first
klass = meta['job_class'].constantize
# => MyJob

# Performs the job without using Sidekiq's API, does not count as performed job and so on.
klass.new.perform(*meta['arguments'])

# OR

# Perform the job using Sidekiq's API so it counts as performed job and so on.
# klass.new(*meta['arguments']).perform_now

如果这不起作用或有人知道更好的方法,请告诉我。