简体   繁体   English

如何在Rails ActiveJob中创建日常工作(类似cron)?

[英]How to create a daily job (cron-like) in Rails ActiveJob?

I'm aware of this thread: A cron job for rails: best practices? 我知道这个帖子: rails的cron工作:最佳实践? , but there's no mention of ActiveJob. ,但没有提到ActiveJob。 My motivation to do it with ActiveJob is because it's built-in in Rails and here's an excerpt from its docs: 我使用ActiveJob做的动机是因为它内置在Rails中,这里是它的文档的摘录:

"These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings." “这些工作可以是定期清理,计费和邮寄等所有工作。”

How do I create a daily job (cron-like) in Rails ActiveJob? 如何在Rails ActiveJob中创建日常工作(类似cron)? Since I don't see the example to run a regularly scheduled job in its docs . 由于我没有看到在其文档中运行定期计划作业的示例。

Or should I stick with the whenever gem? 或者我应该坚持whenever宝石?

Stick with the whenever gem or similar gem eg chrono , clockwork , rufus-scheduler . 坚持whenever宝石或类似的宝石,例如chronoclockworkrufus-scheduler

What you're reading in the ActiveJob documentation is a bit confusing, because it could seem as if ActiveJob may be able handle the responsibility of regular scheduling. 您在ActiveJob文档中阅读的内容有点令人困惑,因为看起来好像ActiveJob可以处理定期调度的责任。 What the documentation should say IMHO is that the jobs are regularly scheduled by some other system or tool. 文档应该说恕我直言,工作是由其他一些系统或工具定期安排的。

So, ActiveJob is about queued jobs? 那么,ActiveJob是关于排队的工作?

Yes, it's about Rails providing a standard interface for adding a job to a queue, and calling a perform method. 是的,它是关于Rails提供标准接口,用于将作业添加到队列,并调用perform方法。 ActiveJob provides the method interfaces that enable adapters for many job-processing queues, backends, immediate runners, etc. ActiveJob提供了方法接口,可以为许多作业处理队列,后端,直接运行程序等启用适配器。

It's working for me: 它对我有用:

every 1.day, at: '9:36 am' do
  runner 'SomeJob.perform_later'
end

I'm using whenever and ActiveJob 我使用的wheneverActiveJob

If you dont want to add a cron job, can put in the end of job a call to repeat the same job 1 day after the first execution 如果你不想添加一个cron作业,可以在作业结束时调用第一次执行后1天重复相同的工作

class SomeJob < ApplicationJob      
   def perform(*args) 
     #execution here...
     SomeJob.set(wait_until: Time.now + 1.day).perform_later(...)
   end
end

I know that is not a good pratice 我知道这不是一个好的实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM