【问题标题】:How to send mails automatically using whenever gem如何使用每当 gem 自动发送邮件
【发布时间】:2017-03-04 12:21:12
【问题描述】:

我曾经每天使用 gem 发送邮件,但它没有发送。我正在使用 amazon linux ec2 实例。请看一下我的代码。

schedule.rb:

every 5.minutes do
  runner "Listings.today_expired_spaces"
end

Listings.rb:(模型文件)

  def today_expired_spaces
        @list=List.find_by_date(Date.today)
        UserMailer.today_expired_list(@list).deliver  
  end

生产中:

运行这些命令后,我得到这样的响应

1.无论何时-w

[write] crontab 文件写入

2.whenever -i [write] crontab 文件更新

3.无论何时

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/ubuntu/my_server && bundle exec 脚本/rails runner -e 生产'\''Spaces.today_expired_spaces'\'''

[message] 上面是你的日程文件转换为 cron 语法;您的 crontab 文件未更新。

[message] 运行 `whenever --help' 以获得更多选项。

请提供解决方案。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 amazon-ec2 ruby-on-rails-3.2


    【解决方案1】:

    您应该在lib/tasks 文件夹中创建一个rake task,并将任务包含在config/schedule.rb 中的计划文件中

    in lib/tasks/send_mail_bang_bang.rake(文件名仅作为示例)

    require 'rake'
    namespace :task_namespace do
      desc 'task description'
      task :send_mail => :environment do
        # call Listing method to send mail
        Listings.today_expired_spaces
      end
    end
    

    config/schedule.rb

    every 5.minutes do
      rake 'task_namespace:send_mail'
    end
    

    您可以在“custom-rake-tasks”部分阅读更多关于 rake 任务 here 的信息

    希望对你有帮助

    【讨论】:

    • 我认为它应该适用于大多数服务器,只要你的服务器在 linux/ubuntu 上运行并支持 RoR ofc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多