Rails cron with whenever, setting the environment

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

提交回复
热议问题