简体   繁体   English

Rails 4:ActionMailer何时发送?

[英]Rails 4: ActionMailer when to send?

I have a Rails API that is accessed by a desktop application to alert me of certain events that take place. 我有一个Rails API,桌面应用程序可以访问该API来提醒我发生的某些事件。 If a flag is toggled when the desktop application hits my API, it will send an email. 如果在桌面应用程序点击我的API时切换标志,它将发送一封电子邮件。 In testing this from curl, the email sending process takes about eight seconds on my development environment. 从curl进行测试时,在我的开发环境上,电子邮件发送过程大约需要八秒钟。 I suspect that once in the wild this may time out. 我怀疑一旦在野外这可能会超时。

On to my question, What is the typical process for handling automatic emails? 关于我的问题, 处理自动电子邮件的典型流程是什么? Do I typically send an email off as the API request comes in? 我通常会在API请求传入时发送电子邮件吗? Or do I have a background process that sits and waits for certain flags to be ticked then sends out emails? 还是我有一个后台进程,等待并等待某些标志被打勾,然后发送电子邮件?

There are many ways to approach that but given that you have an API you could just schedule a background job whenever your criteria are met. 有很多方法可以解决此问题,但是只要您有API,就可以在符合条件时安排一个后台作业。

So you receive an API request, you check whether your criteria are met, and if so you schedule an asynchronous email-sending job (in the background). 因此,您会收到一个API请求,检查是否满足您的条件,如果是,则计划一个异步电子邮件发送作业(在后台)。 Your API returns immediately. 您的API立即返回。

This way if a job fails (for timeout or other reasons) you can see it, reschedule automatic retries, get nice logs/reporting etc. And you don't block your whole Rails app while waiting for the email to be sent. 这样,如果作业失败(由于超时或其他原因),您可以看到它,重新安排自动重试,获取良好的日志/报告等。在等待发送电子邮件时,您不会阻止整个Rails应用程序。

There are many nice solutions for Rails background queuing systems including Sidekiq , resque , and delayed_job . 有Rails的背景排队系统中的许多不错的解决方案,包括Sidekiqresquedelayed_job的 I suggest that you go through their documentation to see which fits best your needs. 我建议您仔细阅读他们的文档,以了解最适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM