如何手动运行Sidekiq作业

Jay*_*dse 15 ruby-on-rails-3 sidekiq

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

jhn*_*atr 10

您可能需要通过以下方式进行修改才能获得所需的工作(例如g8M 上面的建议),但是应该可以解决问题:

> job = Sidekiq::Queue.new("your_queue").first
> job.klass.constantize.new.perform(*job.args)
Run Code Online (Sandbox Code Playgroud)

如果要删除作业:

> job.delete
Run Code Online (Sandbox Code Playgroud)

在sidekiq 5.2.3上测试。


g8M*_*g8M 4

我不会尝试破解 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
Run Code Online (Sandbox Code Playgroud)

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