Hi there,
do you agree that for having scheduled tasks in Rails, the leanest
solution is the following?
- Create a controller with an action for each task
- Implement the logic of the task as controller code
- Set up a cron job at the OS level that uses wget to invoke the URL of
this controller/action at the appropriate time intervals
Advantages:
- full access to all your Rails objects just as in a normal controller
- you can develop and test just as you do normal actions
- you can also invoke your tasks adhoc from a simple web page
- You don’t consume any more memory by firing up additional ruby/rails
processes
My additional question is: How would the cron job entry have to look
like in order to let cron “log in” (as admin for example) before calling
the action (for obvious security reasons)? This is what I have until
now:
20 * * * * /usr/bin/wget --quiet -O -
‘http://www.mydomain.com/my_controller/my_action’
Thank you for opinion and/or hints!
Tom
On Thu, Jun 3, 2010 at 5:12 PM, Tom Ha [email protected] wrote:
20 * * * * /usr/bin/wget --quiet -O -
‘http://www.mydomain.com/my_controller/my_action’
Use script/runner.
–
Greg D.
destiney.com | gregdonald.com
Quoting Tom Ha [email protected]:
[snip]
My additional question is: How would the cron job entry have to look
like in order to let cron “log in” (as admin for example) before calling
the action (for obvious security reasons)? This is what I have until
now:
One solution is to use HTTP Basic Authentication and curl (wget may be
able to
do this, I just know abt curl). Also, check for local_request? in the
controller (i.e., request is coming from the same computer).
HTH,
Jeffrey
Tom,
I think every scheduled task I’ve seen has called methods on models,
not controller actions. Here are some ways I’ve seen it done:
- Sometimes, for cron jobs run in production, folks will do a
“{#RAILS_ROOT}/script/runner -e production ‘Model.method’ >> /dev/null
2>&1”
- There’s repeated_job by ddollar:
GitHub - ddollar/repeated_job: Use Delayed::Job to run cron-like tasks.
It uses delayed_job, which is pretty sweet.
- Adam W. wrote a recent blog post about the shortcomings of
cron: http://adam.heroku.com/past/2010/4/13/rethinking_cron/. He
suggests a combination of rufus-scheduler and Minion+RabbitMQ.
Good luck.
Tilde E.
Hi Tom,
I am using BackgrounDRb and it does almost all of requirements you’ve
mentioned.
Check : http://backgroundrb.rubyforge.org/
Hope this helps!
Mutesa
I’m using GitHub - jmettraux/rufus-scheduler: scheduler for Ruby (at, in, cron and every jobs) to schedule mail
sending. I’ve added scheduling code in config/initializers dir and
it’s working quite well.
Thanks for all your suggestions!
(I like rufus-scheduler pretty much -> small memory foot print…)