background process (cron job) and business rules + database connection

My application needs to send email reminders to people before certain events. I think cron job is the way to go which will run every hour.

Rather than putting the database connection and all the business rules in the shell script , I was wondering if there is a better way. I haven’t used rake but is it possible to write a rake job and execute it through a cron job. Any suggestions on handling these kinds of issues.

Thanks.

My application needs to send email reminders to people before certain
events. I think cron job is the way to go which will run every hour.

Rather than putting the database connection and all the business rules in
the shell script , I was wondering if there is a better way. I haven't used
rake but is it possible to write a rake job and execute it through a cron
job. Any suggestions on handling these kinds of issues.

Define a method in the appropriate model and then call it like this from cron:

/path/to/rails_root/script/runner "MyModel.my_method(arg, arg)"

Then you'll have access to your db connection just like you would normally as well as all your other models, etc.

-philip

You are forgetting backgroundrb!!!!

gnufied

Thanks guys.

I didn’t know about script/runner.

I also missed out on backgrounddrb.

It sure is possible to run a rake command from cron. However, you can also use script/runner for this:

./script/runner "your code goes here"

So for example script/runner "require 'send_reminders'" or script/runner 'Reminder.deliver_all' can be run from your cronjob.