A cron job for rails: best practices?

2019-01-02 16:40发布

What's the best way to run scheduled tasks in a Rails environment? Script/runner? Rake?

20条回答
妖精总统
2楼-- · 2019-01-02 17:10

I used clockwork gem and it works pretty well for me. There is also clockworkd gem that allows a script to run as a daemon.

查看更多
弹指情弦暗扣
3楼-- · 2019-01-02 17:12

Assuming your tasks don't take too long to complete, just create a new controller with an action for each task. Implement the logic of the task as controller code, Then set up a cronjob at the OS level that uses wget to invoke the URL of this controller and action at the appropriate time intervals. The advantages of this method are you:

  1. Have full access to all your Rails objects just as in a normal controller.
  2. Can develop and test just as you do normal actions.
  3. Can also invoke your tasks adhoc from a simple web page.
  4. Don't consume any more memory by firing up additional ruby/rails processes.
查看更多
登录 后发表回答