Rails cron with whenever, setting the environment

前端 未结 10 2114
孤街浪徒
孤街浪徒 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条回答
  •  猫巷女王i
    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!

提交回复
热议问题