简体   繁体   English

Rails:X为true时发送邮件的最简单方法

[英]Rails: Simplest way of sending mail when X is true

So I've been looking for the simplest way to send an e-mail when X column of Payments table in the database is == 'condition'. 因此,我一直在寻找数据库中Payments表的X列=='condition'时发送电子邮件的最简单方法。 Basically what I want is to add a payment and set a date like 6 months. 基本上我想要的是添加付款并设置日期,例如6个月。 When 6 months have passed I want to send the mail. 6个月过去后,我想发送邮件。 I've seen many solutions like using Whenever cron jobs and others but I want to know the absolute simplest way (perhaps using Rails only without relying on outside source) to keep my application light and clean. 我已经看过许多解决方案,例如使用every cron作业和其他解决方案,但是我想知道绝对最简单的方法(也许仅在不依赖外部资源的情况下使用Rails)来保持应用程序的轻便和干净。 I was thinking I could use the auto generated created_at to evaluate when x time has passed. 我当时想我可以使用自动生成的created_at来评估x时间过去了。

The "easiest" way is to use Active Job in conjunction with a state machine: “最简单”的方法是将活动作业与状态机结合使用:

EmailJob.set(wait: 6.months).perform_later(user.id) if user.X_changed?

The problem with this is that the queue will accumulate jobs since jobs don't get handled right away. 这样做的问题是,由于作业不会立即得到处理,因此队列将累积作业。 This may lead to other performance issues since there are now more jobs to scan and they're taking up more memory. 这可能会导致其他性能问题,因为现在有更多的作业要扫描,并且占用了更多的内存。

Cron jobs are well suited for this kind of thing. Cron工作非常适合这种事情。 Depending on your hosting platform, there may be various other ways to handle this; 根据您的托管平台,可能会有其他多种方式来处理此问题。 for example, Heroku has Heroku Scheduler. 例如,Heroku具有Heroku Scheduler。

There are likely other ways to schedule repeating tasks without cron, such as this SO answer . 可能还有其他方法可以在不使用cron的情况下安排重复任务,例如SO答案

edit: I did use a gem once called 'fist_of_fury', but it's not currently maintained and I'm not sure how it would perform in a production environment. 编辑:我确实曾经使用过一个叫做'fist_of_fury'的gem,但是目前还没有维护它,我不确定它在生产环境中的表现如何。 Below are some snippets for how I used it in a rails project: 以下是我在Rails项目中如何使用它的一些摘要:

in Gemfile Gemfile中

gem 'fist_of_fury'

in config/initializers/fist_of_fury.rb config / initializers / fist_of_fury.rb中

# Ensure the jobs run only in a web server.
if defined?(Rails::Server)
  FistOfFury.attack! do
    ObserveAllJob.recurs { minutely(1) }
  end
end

in app/jobs/observe_all_job.rb app / jobs / observe_all_job.rb中

class ObserveAllJob
  include SuckerPunch::Job
  include FistOfFury::Recurrent

  def perform
    ::Task.all.each(&:observe)
  end
end

Since you have a column in your db for the time to send email, make it a datetime datatype and you can set the email date as soon as the event payment event is created. 由于您的数据库中有一列用于发送电子邮件的时间,因此将其设置为datetime数据类型,并且可以在创建事件付款事件后立即设置电子邮件日期。 Then, you can have a rake task where, 然后,您可以执行rake任务,

range = Time.now.beginning_of_day..Time.now.end_of_day
Payment.where(your_datetime_custom_column: range).each do |payment|
  payment.user.send_email
end

and you can run this task everyday from the scheduler. 并且您可以每天从调度程序运行此任务。

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

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