让邮件发送也变得有序

邮件发送应该是一个网站中不可或缺的功能,但如果同时触发了大量的邮件发送动作,很有可能让系统处于瘫痪状态,下面介绍一下如何让邮件发送也变得有序。
安装相关gem:

gem install delayed_job
gem install grimen-delayed_job_mailer

创建delayed_job表:

script/generate delayed_job_migration

会生成:

create_table :delayed_jobs, :force => true do |table|
    table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue
    table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.
    table.text     :handler                      # YAML-encoded string of the object that will do work
    table.string   :last_error                   # reason for last failure (See Note below)
    table.datetime :run_at                       # When to run. Could be Time.now for immediately, or sometime in the future.
    table.datetime :locked_at                    # Set when a client is working on this object
    table.datetime :failed_at                    # Set when all retries have failed (actually, by default, the record is deleted instead)
    table.string   :locked_by                    # Who is working on this object (if locked)
    table.timestamps
end

配置mailer.rb:

class Mailer < ActionMailer::Base
  include Delayed::Mailer
end

配置初始化文件config/initializers/delayed_mailer.rb:

class ActionMailer::Base
  #include Delayed::Mailer
end

配置完成,现在触发邮件发送的动作,系统就会将任务存入delayed_jobs表,依次执行。

 

 

 

你可能感兴趣的:(邮件发送)