我有一个rake任务,应该每天向每天发送邮件== true的用户发送邮件。 (我的项目中已经有一个正在运行的邮件实现,它在创建帐户时发送邮件并进行密码重置)。我正在使用Whenever gem
问题是,因为我不在制作中,所以我不知道如何测试它是否有效。
这是我的UserMailer:
def daily_mail(user)
@user = user
mail to: user.email, subject: "Mail journalier"
end
我的佣金任务:
desc“向用户发送电子邮件”
task :email_sender => :environment do |_, args|
User.find_each do |user|
UserMailer.daily_mail(user).deliver if user.daily == true
end
end
我的日程表.rb:
every :day, :at => '12pm' do # Use any day of the week or :weekend, :weekday
rake "email_sender"
end
我在看了教程之后做了这个:
whenever --update-crontab store (and tried with --set too)
我试着做一个crontab -l,我明白了:
# Begin Whenever generated tasks for: store
0 12 * * * /bin/bash -l -c 'cd /Users/Marco/Documents/TBProject && RAILS_ENV=production bundle exec rake email_sender --silent'
# End Whenever generated tasks for: store
# Begin Whenever generated tasks for: /Users/Marco/Documents/TBProject/config/schedule.rb
0 12 * * * /bin/bash -l -c 'cd /Users/Marco/Documents/TBProject && RAILS_ENV=production bundle exec rake email_sender --silent'
# End Whenever generated tasks for: /Users/Marco/Documents/TBProject/config/schedule.rb
他们是否可以测试(或了解)我的任务是否适用于生产?
更新:
我试图运行“rake email_sender”,我在development.log中得到了这个:
[1m[36mUser Load (0.9ms)[0m [1mSELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1000[0m
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from block (2 levels) in <top (required)> at /Users/Marco/Documents/TBProject/lib/tasks/rake1.rake:5)
Rendered user_mailer/daily_mail.html.erb within layouts/mailer (1.2ms)
Rendered user_mailer/daily_mail.text.erb within layouts/mailer (0.2ms)
UserMailer#daily_mail: processed outbound mail in 189.6ms
Sent mail to example@railstutorial.org (8.6ms)
Date: Mon, 06 Jul 2015 19:24:29 +0200
From: admin@fluxio.com
To: example@railstutorial.org
Message-ID: <559ab9cd79f6_193e3fc21dc6020412765@Marcos-MacBook-Air.local.mail>
Subject: Mail journalier
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_559ab9cd64da_193e3fc21dc6020412690";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_559ab9cd64da_193e3fc21dc6020412690
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Daily Mail
----==_mimepart_559ab9cd64da_193e3fc21dc6020412690
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<h1>Daily Mail</h1>
</body>
</html>
----==_mimepart_559ab9cd64da_193e3fc21dc6020412690--
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from block (2 levels) in <top (required)> at /Users/Marco/Documents/TBProject/lib/tasks/rake1.rake:5)
Rendered user_mailer/daily_mail.html.erb within layouts/mailer (0.0ms)
Rendered user_mailer/daily_mail.text.erb within layouts/mailer (0.0ms)
UserMailer#daily_mail: processed outbound mail in 15.2ms
Sent mail to test@test123.ch (2.7ms)
Date: Mon, 06 Jul 2015 19:24:29 +0200
From: admin@fluxio.com
To: test@test123.ch
Message-ID: <559ab9cdd27c_193e3fc21dc602041295@Marcos-MacBook-Air.local.mail>
Subject: Mail journalier
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_559ab9cdc753_193e3fc21dc60204128e8";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Daily Mail
----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<h1>Daily Mail</h1>
</body>
</html>
----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8--
所以它将邮件发送给每天只有两个== true的用户,所以我的方法有效。但预定的工作是否有效?
答案 0 :(得分:0)
将UserMailer更改为
def daily_mail(user)
@user = user
mail_to :user.email, subject: "Mail journalier"
end