Rails cron with whenever, setting the environment

前端 未结 10 2087
孤街浪徒
孤街浪徒 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 14:12

    I would consider using the "rake" shortcut to make it even cleaner:

    every 1.day, :at => '4am' do
      rake "thinking_sphinx:stop"
      rake "thinking_sphinx:index"
      rake "thinking_sphinx:start"
    end
    
    0 讨论(0)
  • 2021-01-30 14:12

    Latest whenever allows easy Capistrano integration. You can add following to deploy.rb:

    set :whenever_environment, defer { stage }
    set :whenever_identifier, defer { "#{application}-#{stage}" }
    
    require "whenever/capistrano"
    
    0 讨论(0)
  • 2021-01-30 14:13

    This questions has been open a long time so I thought I would share what worked with whenever 0.9.7, Ruby 2.4.0, and RAILS 5.0.1. In the previously mentioned answer there are a lot of close tries but syntax error plagues them. Below is what worked and is very simple approach.

    schedule.rb

    require File.expand_path(File.dirname(__FILE__) + '/environment')
    set :output, {:standard => 'log/cron_log.log', :error => 'log/cron_error_log.log'}
    env :PATH, ENV['PATH']
    
    every :day, :at => '10am' do
         rake "somejob:run", :environment => @environment
    end
    

    Update the crontab(dev)

    whenever --set 'environment=development' --update-crontab
    

    Results(dev)

    0 10 * * * /bin/bash -l -c 'cd /my/rails/app && RAILS_ENV=development bundle exec rake somejob:run --silent >> log/cron_log.log 2>> log/cron_error_log.log'

    Update the crontab(prod)

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

    Results(prod)

    0 10 * * * /bin/bash -l -c 'cd /my/rails/app && RAILS_ENV=production bundle exec rake somejob:run --silent >> log/cron_log.log 2>> log/cron_error_log.log'

    Hopefully this can help someone out. Happy Coding this!

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

    For Whenever (0.9.2)

    Use the @environment variable for environment check:

    case @environment
    
    when 'production'
    
    every 1.minutes do
    
       rake "user:take_sample"
    
      end
    
    when 'development'
    
    every 1.minutes do
    
      rake "user:dev_sample"
    
      end
    
    end
    
    0 讨论(0)
提交回复
热议问题