We recently switched www.sharesight.co.nz to use mail_queue, a ruby on rails plug-in. This helped decouple our application from the mail server – improving the performance of page request/response times and isolating any mail server problems away from our application.
We have ended up making a few tweaks to the plug-in, detailed below:
The tweaks
- Added a sent flag onto the queued_mails, so we keep all mail sent out from our server. This proves may not be required but gives us a little confidence in seeing the sent mail, when it was queued and when it was sent, and provides an added bonus – if needed (in times of trouble) to reset the flag and have the mail sent out again.
- Added a failure count on the queued_mails, if a mail message is failing, we increase the count and the process will retry again during the next sweep – giving up on the message after several retries.
- Increase the size of the object to store the mail
Some problems we experienced
With large emails the ‘text’ field in mysql was not large enough to store the entire email message . The mail object then failed to be created and with no exception handling in place the email queue was blocked by one overlarge corrupt email – hence the failure count and the increase from text to mediumtext for the mail object.
With a large number of queued emails, the mail server started rejecting emails (due to our limited email account plan we are currently on) – and responding very slowly – this caused the MailQueue.process job to run before the previous job had completed – causing some emails to be sent twice – arghhh – hence see additional enhancements below.
Additional enhancements (TODO)
- Ensure that the process can not run – by either synchronising with a database flag (in_process) or exiting the process before the next one starts by keeping a tab on the time taken to run.
- Use backgroundrb to start the processing so the whole ruby environment does not have to be loaded every minute to check for mail.