Cron job in ruby on rails not work

坚强是说给别人听的谎言 提交于 2020-01-06 04:47:07

问题


I followed the railscast http://railscasts.com/episodes/164-cron-in-ruby but I cant seem to make it worked.

  • I have schedule.rb in my config.
  • I wished to refresh my Database.count everyday in my homepage.
  • I cannot find the deploy.rb in my folder. Where is it?

For testing purpose, I changed it to every 2 seconds.

[schedule.rb]

every '2 * * * *' do
  rake "pages_controller:home"
end

[pages_controller.rb]

class PagesController < ApplicationController
def home
  @title = "Home"
  @companies = Company.find(:all, :limit => 20)
  @count = Company.count

end

I have put

 gem 'whenever', :require => false

in my gemfile. What have gone missing?


回答1:


I have used cron job but i run it as rake task please you can try it

  every 3.minutes do
   set :environment, 'development'
   rake "daily",:output => {:error => 'error.log', :standard => 'cron.log'}
  end

And my task is like

  require 'rubygems'

  task :daily => :environment do
    puts "i am fine"
    # do your code
  end

If cron job run fine then there will be nothing in cron.log.Otherwise it will show you if

any error occurs and this file will be generate in your app root directory.

try it..




回答2:


So... Couple things.

  1. You need a Capfile and a config/deploy.rb to deploy your code using Capistrano and Whenver. You get this by running capify . ... You should most likely watch the railscast on capistrano deployment
  2. Whenever is using configured like:

schedule.rb

every 1.day, :at => '4:30 am' do 
  runner 'Rails.cache.clear'
end

I really don't think rake "pages_controller:home is going to work unless this is something you've already created elsewhere. I'm further assuming you are caching this page, and that's why you need to refresh the cache.

Finally, you're setting your environment to development, which makes me think you are not deploying this, but instead just wanting to reset the cached home page... So just run rake cache:clear



来源:https://stackoverflow.com/questions/9995256/cron-job-in-ruby-on-rails-not-work

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