Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best rails solution for a mailer that runs every minute

I have an application that checks a database every minute for any emails that are supposed to be sent out at that time. I was thinking about making this a rake task that would be run by a cron job every minute. Would there be a better solution to this?

From what I have read, this isn't ideal because rake has to load the entire rails environment every minute and this becomes expensive.

Thoughts?

Thanks.

like image 392
vrish88 Avatar asked Mar 18 '09 00:03

vrish88


2 Answers

  1. You can use backgroundrb. This, however, will eat up memory away from your main Rails app as it will spawn one Ruby instance exclusive to backgroundrb.
  2. You can also define a SystemController (or equivalent) in your main application, with various actions corresponding to the various household tasks your application should perform. You can "prod" it from crontab using wget or curl, the advantage being that it shares resources with your main application. Depending on how paranoid or you are, or on how vulnerable to DOS (or other types of attacks) exposing such a controller to, possibly, the outside world, you may choose to block access to this controller's URL from addresses other than the loopback (ideally in your reverse proxy, alternatively from the controller itself.)
like image 173
vladr Avatar answered Sep 30 '22 20:09

vladr


One really simple method would be to have a script that does..

while true do
    check_and_send_messages()
    sleep 60
end

..which means you are not constantly respawning the Rails environment.

Obviously it has various flaws, but also has some benefits (for example, with your 1-Rake-per-minute, it the Rake task takes more than one minute, Rake will be running multiple times at once)

Also, the Railscasts episodes Rake in Background, Starling and Workling, and Custom Daemon might give you some ideas (they are describing exactly this task)

like image 34
dbr Avatar answered Sep 30 '22 19:09

dbr