In one of my projects. I am using ar_mailer gem to store emails in the the database and send them in batches every few minutes. its really a handy gem.
About its usage and how it works, you can refer this link.
ar_mailer has 2 components, one which helps you to store emails in the database and the 2nd which reads emails from the database and sends them out. The 2nd component is available as a utility called "ar_sendmail", which supports various options. Below, I will show you how I first used it wrongly(which brought our site down!) and then correctly using monit.
Wrong Usage(for my project situation)
I setup a cron job which runs every minute, invokes ar_sendmail with following options
ar_sendmail -b 100 --max-age 0 -c "/data/project/current" -e production" --once
Above command invokes ar_sendmail once(--once) and send out a batch of 100 emails(-b).
Whats wrong above?
The cron job executes ar_sendmail every minute, ar_sendmail invokes the whole Rails environment, sends out emails and then finishes. There are 2 problems,
1. ar_mailer invokes the whole production environment every minute: it takes a while to load the production environment(all the plugins, gems etc. gets loaded) and every minute!
2. Multiple instances of ar_sendmail: Multiple instances of ar_sendmail start running since ar_sendmail doesn't create a file lock to avoid this situation. For me, once 7 instances of ar_sendmail started running which brought down the system and had to be rebooted.
Correct Usage
So from above we need to make sure that ar_sendmail doesn't invoke the whole Rails environment every minute and also multiple instances of ar_mailer doesn't get spawned.
1. "-d" option of ar_sendmail: ar_sendmail has "-d" option which runs ar_sendmail as a daemon. it invokes webrick server once for all. So the new ar_sendmail command looks like following
ar_sendmail -d -b 100 --max-age 0 --delay 60 -c "/data/project/current" -e "production"
Note the -delay and -d option
2. To make sure that multiple instances of ar_sendmail doesn't get spawned, use monit as described here
Life is back to normal...
1 comments:
normal = some uber rails hacking ;)
- Akshay
Post a Comment