Ruby-on-rails 如何在 Active Job (Rails 4.2) 中安排重复作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25798013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:54:06  来源:igfitidea点击:

How do I schedule recurring jobs in Active Job (Rails 4.2)?

ruby-on-railsruby-on-rails-4rails-activejob

提问by luis.madrigal

I found this Schedule one-time jobs in Railsbut this only shows how schedule one-time. I am interested in scheduling a recurring job.

在 Rails 中找到了这个 Schedule one-time jobs但这仅显示了如何安排一次性工作。我对安排定期工作感兴趣。

Delayed_job has this

Delayed_job 有这个

self.delay(:run_at => 1.minute.from_now)

How do I do something like that in Rails 4.2/Active Job?

我如何在 Rails 4.2/Active Job 中做类似的事情?

回答by omencat

Similar to rab3's answer, since ActiveJob has support for callbacks, I was thinking of doing something like

与 rab3 的回答类似,由于 ActiveJob 支持回调,我正在考虑做类似的事情

class MyJob < ActiveJob::Base
  after_perform do |job|
    # invoke another job at your time of choice 
    self.class.set(:wait => 10.minutes).perform_later(job.arguments.first)
  end

  def perform(the_argument)
    # do your thing
  end
end

activejob callbacks

活动作业回调

回答by Juanito Fatas

If you want to delay the job execution to 10 minutes later, two options:

如果您想将作业执行延迟到 10 分钟后,有两个选项:

  1. SomeJob.set(wait: 10.minutes).perform_later(record)

  2. SomeJob.new(record).enqueue(wait: 10.minutes)

  1. SomeJob.set(wait: 10.minutes).perform_later(record)

  2. SomeJob.new(record).enqueue(wait: 10.minutes)

Delay to a specific moment from now use wait_until.

从现在开始延迟到特定时刻使用wait_until

  1. SomeJob.set(wait_until: Date.tomorrow.noon).perform_later(record)

  2. SomeJob.new(record).enqueue(wait_until: Date.tomorrow.noon)

  1. SomeJob.set(wait_until: Date.tomorrow.noon).perform_later(record)

  2. SomeJob.new(record).enqueue(wait_until: Date.tomorrow.noon)

Details please refer to http://api.rubyonrails.org/classes/ActiveJob/Base.html.

详情请参考http://api.rubyonrails.org/classes/ActiveJob/Base.html

For recurring jobs, you just put SomeJob.perform_now(record)in a cronjob (whenever).

对于重复性工作,您只需放入SomeJob.perform_now(record)一个 cronjob(每当)。

If you use Heroku, just put SomeJob.perform_now(record)in a scheduled rake task. Please read more about scheduled rake task here: Heroku scheduler.

如果您使用 Heroku,只需放入 SomeJob.perform_now(record)预定的 rake 任务。请在此处阅读有关预定 rake 任务的更多信息:Heroku scheduler

回答by rafb3

You can just re-enqueue the job at the end of the execution

您可以在执行结束时重新排队作业

class MyJob < ActiveJob::Base
  RUN_EVERY = 1.hour

  def perform
    # do your thing

    self.class.perform_later(wait: RUN_EVERY)
  end
end

回答by trans1t

If you're using resque as your ActiveJob backend, you can use a combination of resque-scheduler's Scheduled Jobs and active_scheduler (https://github.com/JustinAiken/active_scheduler, which wraps the scheduled jobs to work properly with ActiveJob).

如果你使用 resque 作为你的 ActiveJob 后端,你可以使用 resque-scheduler 的 Scheduled Jobs 和 active_scheduler 的组合(https://github.com/JustinAiken/active_scheduler,它包装了调度的作业以与 ActiveJob 正常工作)。