What's the best way to organize worker processes in Rails?

前端 未结 4 1706
无人及你
无人及你 2020-12-23 17:49

I frequently have some code that should be run either on a schedule or as a background process with some parameters. The common element is that they are run outside the disp

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 18:45

    For me, not wanting to maintain a lot of extra infrastructure is a key priority, so I have used database-backed queues that are run outside of Rails.

    In my case, I've used background_job and delayed_job. With background_job, the worker was kept running via cron, so there was no daemon management. With delayed_job, I'm using Heroku and letting them worry about that.

    With delayed_job you can pass in as many arguments as your background worker needs to run.

    Delayed::Job.enqueue(MyJob.new(param[:one], param[:two], param[:three])
    

    I have not found a good solution to running stuff on a schedule, aside from using script/runner via cron (I prefer to use script/runner over a Rake task because I find it easier to test the code).

    I've never had to have a regularly scheduled background process that needed access to a particular Rails request so that hasn't been too much of a problem.

    I know there are other, cooler systems with more features but this has worked OK for me, and helps me avoid dealing with setting up a lot of new services to manage.

提交回复
热议问题