【问题标题】:Rails cron with whenever, setting the environmentRails cron 与时俱进,设置环境
【发布时间】:2010-11-07 09:36:26
【问题描述】:

这个问题可能只有在您了解创建 cron 作业的whengem 时才有意义。我的 schedule.rb 中有一个任务,例如

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

但是当我使用更新我的 crontab 时

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

cron 作业仍然有 RAILS_ENV=development。我现在的生产和开发任务是一样的,我只需要改变环境变量,因为thinking_sphinx需要知道当前的环境。关于如何做到这一点的任何想法?

谢谢!

【问题讨论】:

  • 在我的特殊情况下(开始延迟作业)我使用 command "RAILS_ENV=#{@environment} #{Whenever.path}/bin/delayed_job start" 得到了这个工作
  • 如果您运行 rake,您还可以在 whenever 上下文中使用 rake 命令。

标签: ruby-on-rails cron whenever


【解决方案1】:

只要没有检测到您的环境,它就会默认使用生产环境。 您可以使用 set 为所有作业设置环境:

set :environment, 'staging' 

或每个工作:

every 2.hours do 
  runner 'My.runner', :environment => 'staging' 
end 

【讨论】:

  • 我可以将一个数组传递给environment hast 来设置多个环境吗?
  • 当我们处理多个环境时,这不是正确的方法
【解决方案2】:

不要写 RAILS_ENV 变量。它应该自动设置它。

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

它适用于我的应用程序:

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"

【讨论】:

    【解决方案3】:

    对于Whenever (0.9.2)

    使用@environment 变量进行环境检查:

    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
    

    【讨论】:

    • 但是请注意,@environment 变量默认始终是“生产”。吐出你的工作时,你必须whenever --set environment=development --write-crontab,然后这个答案才会奏效。
    【解决方案4】:

    如果您使用的是 bundler 和 capistrano,您可能还想尝试其他方法。

    在您的 deploy.rb 文件中,当您设置 :whenever_command 时,请勿简单地这样做:

    set :whenever_command, "bundle exec whenever"
    

    相反,这样做:

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

    现在,当 schedule.rb 文件加载时,RAILS_ENV 环境变量将可用,所以在 schedule.rb 中您现在可以这样做:

    set :environment, ENV['RAILS_ENV']
    

    瞧!你准备好了。

    【讨论】:

    • 我得到了这个:失败:“sh -c 'cd (REMOVED)/releases/20140127213854 && RAILS_ENV=production bundle exec 每当 --update-crontab (REMOVED) --set environment=production --角色 db'" 上(删除)
    【解决方案5】:

    这个问题已经开放了很长时间,所以我想我会分享在 0.9.7、Ruby 2.4.0 和 RAILS 5.0.1 中适用的内容。在前面提到的答案中,有很多密切尝试,但语法错误困扰着他们。以下是有效且非常简单的方法。

    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
    

    更新 crontab(dev)

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

    结果(开发)

    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'

    更新 crontab(prod)

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

    结果(产品)

    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'

    希望这可以帮助某人。 快乐编码!

    【讨论】:

    • env :PATH, ENV['PATH'} ??
    • 我尝试在output 日志和environment 上查找有关set 方法和用法的文档。我在哪里可以找到有关它的信息?
    【解决方案6】:

    如果您想将多个参数传递给任何时候,请注意。
    你必须这样做:

    whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
    

    【讨论】:

      【解决方案7】:

      允许轻松集成 Capistrano 的最新版本。您可以在 deploy.rb 中添加以下内容:

      set :whenever_environment, defer { stage }
      set :whenever_identifier, defer { "#{application}-#{stage}" }
      
      require "whenever/capistrano"
      

      【讨论】:

        【解决方案8】:

        我遇到了一个问题,即每次 cron 作业都没有设置环境 - 选择 /usr/bin/bundle 而不是 /usr/local/bin/bundle。

        解决方案是将以下内容添加到 schedule.rb 的顶部

        env 'PATH', ENV['PATH']
        

        【讨论】:

          【解决方案9】:

          在 config/schedule.rb 顶部添加以下代码行。

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

          并使用以下命令更新 crontab。

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

          然后最后使用命令重新启动 crontab

          service crond restart
          

          就是这样!

          最终的config/schedule.rb看起来是这样的

           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
          

          【讨论】:

            【解决方案10】:

            我会考虑使用“rake”快捷方式使其更干净:

            every 1.day, :at => '4am' do
              rake "thinking_sphinx:stop"
              rake "thinking_sphinx:index"
              rake "thinking_sphinx:start"
            end
            

            【讨论】:

            • 这个答案并没有说明如何改变环境,它只是提供了一种使任务更清洁的方法。查看 Trung LE 的答案,了解真正的方法
            • 根本不回答问题。
            • 请注意rake 命令确实实际上将RAILS_ENV 添加到以crontab 结尾的命令中。
            猜你喜欢
            • 1970-01-01
            • 2011-11-05
            • 1970-01-01
            • 1970-01-01
            • 2023-04-07
            • 2018-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多