Scheduling tasks with rails

心不动则不痛 提交于 2019-12-30 13:29:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!