Rails cron with whenever, setting the environment

前端 未结 10 2296
孤街浪徒
孤街浪徒 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: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.

提交回复
热议问题