用Rails安排任务 [英] Scheduling tasks with rails
问题描述
我一直在遍历rails调度任务选项,并从任何时候偶然发现这段代码.
I have been going over rails scheduling tasks options and stumbled upon this piece of code from whenever.
case @environment
when 'production'
every 1.day, :at => "#{Time.parse('12:00 A').getlocal.strftime("%H:%M")}" do
runner "Company.send_later(:create_daily_stories!)"
end
when 'staging'
every 15.minutes do
command "thinking_sphinx_searchd reindex"
end
end
我对红宝石还很陌生,我不太了解公司"在这里的含义.换句话说,我想向人们发送电子邮件,并且我有一个名为email_controller的控制器类,其中有一个名为sendEmail的方法,我想使用此方法发送电子邮件吗?我应该说跑步者"email_controller.sendEmail"还是类似的东西?我不太明白.注-我是否使用模型或控制器代替公司?
I am fairly new to ruby and I dont quite understand what "Company" here stands for. In other words say i want to send an email out to people and i have a controller class called email_controller in which I have a method called sendEmail and I want to send emails using this how would i do it? Should i say runner"email_controller.sendEmail" or something like that? I dont quite get it. Note - Do i use the model or controller in place of company?
推荐答案
在这种情况下,Company
是一个示例模型,该模型具有称为create_daily_stories!
的类/单个方法.从理论上讲,它可能看起来像这样:
In this case, Company
is an example model that has a class/singleton method called create_daily_stories!
. In theory, it would probably look like this:
class Company < ActiveRecord::Base
# Send out daily stories to all companies
def self.create_daily_stories!
# Do some stuff
end
end
理想情况下,生成电子邮件驻留在业务逻辑中,因此应包含在模型中(假设您使用的是Rails之类的MVC框架).
Ideally, generating emails resides in the business logic and should thusly be contained within a model (assuming you're using an MVC framework like rails).
这篇关于用Rails安排任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!