Rails: 你如何告诉 cron 执行控制器方法?
Rails: how do you tell cron to execute a controller method?
文章控制器中的一堆邮件方法,将不同类型的文章与不同的用户混合在一起。从 Rails 内的某处调用时一切正常。但它们需要在一周内由 cron 执行。什么代替了 rake 文件中的 "xxxx" 来告诉它执行 Article 控制器中的“10_am_action”方法?
articles_controller.rb:
def 10_am_action
[sends the right e-mails to the right users]
end
计划任务:
0 10 * * 0 cd /data/website/current && RAILS_ENV=production bundle exec rake emails:10_am
emails.rake:
namespace :briefings do
desc "10 am e-mails"
task :10_am => :environment do
xxxxxx
end
end
你不能,因为控制器绑定到 HTTP。
但是,您可以重构代码并将电子邮件发送逻辑提取到邮件程序,然后从您的控制器和 rake 任务中调用该邮件程序。
文章控制器中的一堆邮件方法,将不同类型的文章与不同的用户混合在一起。从 Rails 内的某处调用时一切正常。但它们需要在一周内由 cron 执行。什么代替了 rake 文件中的 "xxxx" 来告诉它执行 Article 控制器中的“10_am_action”方法?
articles_controller.rb:
def 10_am_action
[sends the right e-mails to the right users]
end
计划任务:
0 10 * * 0 cd /data/website/current && RAILS_ENV=production bundle exec rake emails:10_am
emails.rake:
namespace :briefings do
desc "10 am e-mails"
task :10_am => :environment do
xxxxxx
end
end
你不能,因为控制器绑定到 HTTP。
但是,您可以重构代码并将电子邮件发送逻辑提取到邮件程序,然后从您的控制器和 rake 任务中调用该邮件程序。