问题
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
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?
回答1:
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
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).
回答2:
Resque is a great way to schedule tasks.
Take a look at Resque Railscast.
or possibly this Rails: Cron Job Scheduling using Redis, Resque and Rufus.
来源:https://stackoverflow.com/questions/8139020/scheduling-tasks-with-rails