Re: Rails way to do long running tasks

Phlip
2009-06-07T09:54:17Z

Chris wrote:

>    *   how to send emails without making the user wait

Either Thread.new{}, or a plugin called Spawn. You need the latter if your
e-mails call templates which in turn call ActiveRecord. A separate thread
requires a separate database connection, and Spawn handles this.

>    *   how to execute long running tasks

The threading option has one flaw: If your web server expunges your Passenger
module from memory, the thread lapses.

When sending a few emails, if you can''t send them before process
harvesting
time, you have bigger problems than a missing email. Failing that analysis, you
need an out-of-process solution like BackgroundRB. It runs in a daemon of its
own and communicates with your app thru dRB. In exchange for a fatter and more
fragile implementation, you get a process you can control directly. However...

>    *   The best way to do periodic cron jobs in Rails

Create a folder called cron, write (using TDD!) a script that does what you
need, and install it into your OS''s cron system as the command line
script/runner lib/my_script.rb.

If I needed to send e-mails, I would pool them up, then push them out with a
cron. The previous two options allow communication back to the user in
subsequent controller actions. You probably don''t need that, so just
configure a
cron to run every 90 seconds, and send emails for up to 1 minute. Take a little
care to not send the same email twice, and you are set.

--
   Phlip