Rails cron with whenever, setting the environment

前端 未结 10 2086
孤街浪徒
孤街浪徒 2021-01-30 13:15

This question will probably only make sense if you know about the whenever gem for creating cron jobs. I have a task in my schedule.rb like

every 1.day, :at =&g         


        
相关标签:
10条回答
  • 2021-01-30 13:53

    Watch out if you want to pass more than one param to whenever.
    You have to do it like that:

    whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
    
    0 讨论(0)
  • 2021-01-30 14:00

    I was having an issue where environment wasn't being set up for whenever cron jobs - /usr/bin/bundle was being picked up instead of /usr/local/bin/bundle.

    The solution was to add following to top of schedule.rb

    env 'PATH', ENV['PATH']
    
    0 讨论(0)
  • 2021-01-30 14:01

    Something else you may want to try if you're using bundler and capistrano.

    In your deploy.rb file, when you set the :whenever_command, DO NOT simply do this:

    set :whenever_command, "bundle exec whenever"
    

    Instead, do this:

    set(:whenever_command) { "RAILS_ENV=#{rails_env} bundle exec whenever" }
    

    Now, the RAILS_ENV environment variable will be available when the schedule.rb file is loaded, so in schedule.rb you can now do this:

    set :environment, ENV['RAILS_ENV']
    

    Voila! You're ready to go.

    0 讨论(0)
  • 2021-01-30 14:04

    Add the following line of code at top of config/schedule.rb.

     ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"
    

    and update the crontab using following command.

    whenever --update-crontab pvcnxt --set 'environment=production'
    

    and then finally restart crontab using command

    service crond restart
    

    Thats it!

    Final config/schedule.rb looks this way

     ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"
    
     env :PATH, ENV['PATH']
    
     require File.expand_path(File.dirname(__FILE__) + "/environment")
    
     set :output, "#{Rails.root}/logs/cron_log_#{ENV['RAILS_ENV']}.log"
    
     every 1.day, :at => '00:00 am' do
      command "cd #{Rails.root}/lib/tasks && rake clean__posts_table_rake"
     end
    
    0 讨论(0)
  • 2021-01-30 14:06

    Don't write the RAILS_ENV variable. It should set it automatically.

    every 1.day, :at => '4am' do
      command "cd #{RAILS_ROOT} && rake thinking_sphinx:stop"
      command "cd #{RAILS_ROOT} && rake thinking_sphinx:index"
      command "cd #{RAILS_ROOT} && rake thinking_sphinx:start"
    end
    

    It works in my app:

    every 4.days do
      runner "AnotherModel.prune_old_records"
    end
    
    $ whenever --set environment=production
    0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e production "AnotherModel.prune_old_records"
    
    $ whenever --set environment=development
    0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e development "AnotherModel.prune_old_records"
    
    0 讨论(0)
  • 2021-01-30 14:07

    Whenever doesn't detect your environment, it just defaults to using production. You can set the environment for all jobs using set:

    set :environment, 'staging' 
    

    Or per job:

    every 2.hours do 
      runner 'My.runner', :environment => 'staging' 
    end 
    
    0 讨论(0)
提交回复
热议问题