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 dispatch process, but need access to the Rails environment (and possibly the parameters passed in).
What’s a good way to organize this and why? If you like to use a particular plugin or gem, explain why you find it convenient–don’t just list a plugin you use.
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_joband delayed_job. Withbackground_job, the worker was kept running via cron, so there was no daemon management. Withdelayed_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.
I have not found a good solution to running stuff on a schedule, aside from using
script/runnervia cron (I prefer to usescript/runnerover 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.