问题
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.
- 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 - 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