The best rail solution for an email program that runs every minute

I have an application that checks the database every minute for any emails that should be sent at this time. I was thinking of doing this task, which will be performed by the cron task every minute. Would there be a better solution for this?

From what I read, this is not ideal, because rake has to load the entire rails environment every minute, and it becomes expensive.

Thoughts?

Thanks.

+6
ruby-on-rails cron rake backgroundrb
source share
4 answers
  • You can use backgroundrb . This, however, eats up memory from your main Rails application, as it spawns a single Ruby instance that excludes backgroundrb .
  • You can also define a SystemController (or equivalent) in your main application with various actions appropriate for the various home tasks your application should perform. You can "drag" it from crontab using wget or curl , the advantage is that it shares resources with your main application. Depending on how paranoid or you are, or how vulnerable you are to DOS (or other types of attacks) by exposing such a controller to the outside world, you may block access to this controller URL from addresses other than loopback (ideal in your reverse proxy, as an alternative from the controller itself.)
+11
source share

One very simple method is to have a script that does.

 while true do check_and_send_messages() sleep 60 end 

.. This means that you are not constantly updating the Rails environment.

Obviously, it has various disadvantages, but also has some advantages (for example, with your 1-rake per minute, his Rake task takes more than one minute, Rake will work several times at once)

In addition, the Railscasts Rake in Background , Starling and Workwork and Custom Daemon episodes may give you some ideas (they describe just this task)

+1
source share

It turns out that in fact, something was built just for this: ar_mailer. ar_mailer sends emails to the database and periodically sends them using the ar_mailer command. You can call ar_mailer every minute.

The good thing about ar_mailer is that it basically requires very minor changes in terms of how you are sending emails. You just need to inherit from ar_mailer instead of ActiveMailer. Using this method, you do not have to worry about performing rake tasks in the background, branching processes or something like that - and in fact you get a real mail server with messages in the queue that are deleted when sending mail. This feature is important if you have a system that sends a large amount of enmass email. I used ar_mailer to create a social network, so I can confirm its reliability.

Here is a good article that details ar_mailer . I would strongly recommend abandoning your own solution here, as Eric created a time-tested solution to this problem.

+1
source share

I am doing what Vlad (No. 2) suggested, only following local requests, and I'm paranoid enough to also require the query string to be bound to a URL.

I have several periodic activities configured in this way.

0
source share

All Articles