rails定时发件任务

定时任务的gem是whenever 教程来源 设置你的发件动作

file: app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  def digest_email_update(options)
    # ... email sending logic goes here
  end
end

创建你的rake任务

file: lib/tasks/email_tasks.rake

desc 'send digest email'
task send_digest_email: :environment do
  # ... set options if any
  UserMailer.digest_email_update(options).deliver!
end

执行rake send_digest_email发送邮件。 安装gem whenever

file: Gemfile

gem ‘whenever’, require: false

输入wheneverize指令创建 schedule.rb file

file: config/schedule.rb

every :day, at: '10pm' do
  # specify the task name as a string
  rake 'send_digest_email'
end

输入whenever whenever -w将记录写入


讲whenever套上capistrano In your “Capfile” file: require 'whenever/capistrano'