Best rails solution for a mailer that runs every minute

前端 未结 4 905
有刺的猬
有刺的猬 2021-01-01 01:00

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 wou

相关标签:
4条回答
  • 2021-01-01 01:13

    It turns out there's actually something built just for this: ar_mailer. ar_mailer queues up the e-mails into the DB and then sends them out periodically using the ar_mailer command. You can call ar_mailer every minute.

    The nice thing about ar_mailer is that it basically requires very little change in terms of how you already send e-mails. You just need to inherit from ar_mailer instead of ActiveMailer. Using this method, you won't have to worry about running rake tasks in the background, forking processes, or anything like that - and in effect you get a real mail server with queued messages that are deleted when the mail is actually sent. This feature is important if you have a system that sends out large numbers of e-mail enmass. I've used ar_mailer to build a social network - so I can attest to its robustness.

    Here's a good article that talks about ar_mailer in depth. I would strongly advise against rolling your own solution here as Eric has built a time-tested solution to this very problem.

    0 讨论(0)
  • 2021-01-01 01:14
    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.)
    0 讨论(0)
  • 2021-01-01 01:15

    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)

    0 讨论(0)
  • 2021-01-01 01:15

    I do what Vlad suggested (#2), with only local requests honored, and I'm paranoid enough to also require a specific query string tacked on to the url.

    I have several periodic actions set up this way.

    0 讨论(0)
提交回复
热议问题