lui*_*gal 37 ruby-on-rails ruby-on-rails-4 rails-activejob
我在Rails中找到了这个 Schedule一次性工作, 但这只显示了如何一次性安排.我有兴趣安排一份经常性的工作.
Delayed_job有这个
self.delay(:run_at => 1.minute.from_now)
Run Code Online (Sandbox Code Playgroud)
我如何在Rails 4.2/Active Job中做类似的事情?
ome*_*cat 25
与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
Run Code Online (Sandbox Code Playgroud)
Jua*_*tas 23
如果要将作业执行延迟到10分钟后,有两个选项:
SomeJob.set(wait: 10.minutes).perform_later(record)
SomeJob.new(record).enqueue(wait: 10.minutes)
延迟到现在使用的特定时刻wait_until.
SomeJob.set(wait_until: Date.tomorrow.noon).perform_later(record)
SomeJob.new(record).enqueue(wait_until: Date.tomorrow.noon)
详情请参阅http://api.rubyonrails.org/classes/ActiveJob/Base.html.
对于重复性工作,你只需要SomeJob.perform_now(record)输入一个cronjob(无论何时).
如果你使用Heroku,只 SomeJob.perform_now(record)需要安排一个预定的rake任务.请在此处阅读有关预定rake任务的更多信息:Heroku调度程序.
raf*_*fb3 11
您可以在执行结束时重新排队作业
class MyJob < ActiveJob::Base
RUN_EVERY = 1.hour
def perform
# do your thing
self.class.perform_later(wait: RUN_EVERY)
end
end
Run Code Online (Sandbox Code Playgroud)