ActionMailer Automatically sends an email everyday at a specific time

Hi,

I would like to have a notification system where an email is sent to
specific addresses every morning with different contents everyday. The
contents are stored in the data, but the number of recipients is a
few. So, it’s not a gigantic task.

I will be using ActionMailer on Rails 2.0.2, but I can’t come up with
a way to trigger the method to send the emails at a given time
everyday. If my understanding is correct, Unix has a cron job, so I
could use that, though that is not beyond the territory of Rails. Is
there any easier way to do that in Rails? Or would you recommend me
that I use a cron job for this?

  • T

On May 15, 9:22 am, “T K” [email protected] wrote:

I will be using ActionMailer on Rails 2.0.2, but I can’t come up with
a way to trigger the method to send the emails at a given time
everyday. If my understanding is correct, Unix has a cron job, so I
could use that, though that is not beyond the territory of Rails. Is
there any easier way to do that in Rails? Or would you recommend me
that I use a cron job for this?

You can either use cron and a standalone rails script that partially
loads some of the rails stuff and does it’s thing, or you can write a
standalone ruby script that simply sleeps and wakes up every minute or
so and runs when it’s the correct time (or close to it). You can
always use a daemon gem so that it keeps itself alive in case of
random crashes.

Cron/Ruby advantage:

More advanced timing (every other hour on alternate Thursdays in June,
for example).
More accurate if you need precise timing (instead of whatever your
polling resolution is in your ruby script)
Standard Unix tool, so sysadmins will easily know what you are doing
Stupidly simple to set up, and cron is old and stable, and won’t ever
change
No silly polling that wastes time as with the pure Ruby version (more
elegant)

Pure Ruby advantage:

More easily understandable for Ruby users not familiar with Unix, and
cross-platform (as Windows does not have an easily accessible
equivalent to cron)
No need to access system tools (normally available to only root user/
wheel group)
You can tie it directly into your server’s (i.e. Ruby software, not
the system itself) startup scripts so you don’t forget it’s there, and
it’s tied to your ruby app (it’s easy to forget cron stuff… and
sending out e-mails is something you don’t want to forget you are
doing), and you can easily turn it off without modifying the crontab.
Cron is really meant for system-wide administration tasks, not simply
running user programs periodically.
Database driven: you can manage the times it runs via the database,
instead of manually setting up/modifying a cron entry.
It will run under whatever user ruby/rails runs under (instead of the
cron’s user). Better security and you won’t accidentally FUBAR
something you don’t really own. This also makes system CPU/disk/etc.
quotas work correctly.
Keep all your periodic tasks in one file even if they don’t run at the
same time, instead of multiple scripts like cron would need.

As with almost everything… “It depends.” :wink: