Ruby on rails Rails cron与where一起设置环境
只有当您了解创建cron作业的方法时,这个问题才可能有意义。我的日程安排中有一项任务Ruby on rails Rails cron与where一起设置环境,ruby-on-rails,cron,whenever,Ruby On Rails,Cron,Whenever,只有当您了解创建cron作业的方法时,这个问题才可能有意义。我的日程安排中有一项任务 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_EN
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
但是,当我使用
whenever --update-crontab appname --set environment=production
cron作业仍然有RAILS\u ENV=development。我现在在生产和开发方面的任务是一样的,我只需要更改环境变量,因为thinking_sphinx需要了解当前的环境。有什么办法吗
谢谢 不要写入RAILS\u 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"
我会考虑使用“Rake”快捷方式使它更干净:
every 1.day, :at => '4am' do
rake "thinking_sphinx:stop"
rake "thinking_sphinx:index"
rake "thinking_sphinx:start"
end
只要没有检测到您的环境,它就默认使用生产环境。 您可以使用set:
set :environment, 'staging'
或每项工作:
every 2.hours do
runner 'My.runner', :environment => 'staging'
end
如果要向任何时候传递多个参数,请小心。
你必须这样做:
whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
如果您使用bundler和capistrano,您可能还想尝试一些其他功能 在deploy.rb文件中,当您设置:where_命令时,不只需执行以下操作:
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']
瞧!您可以开始了。最新版本,只要允许轻松集成Capistrano。您可以将以下内容添加到deploy.rb:
set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }
require "whenever/capistrano"
无论何时(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
在config/schedule.rb顶部添加以下代码行
ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"
并使用以下命令更新crontab
whenever --update-crontab pvcnxt --set 'environment=production'
最后使用命令重新启动crontab
service crond restart
就这样
Finalconfig/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
这个问题已经存在很长时间了,所以我想我会与大家分享一下0.9.7、Ruby 2.4.0和RAILS 5.0.1的工作原理。在前面提到的答案中,有很多接近的尝试,但语法错误困扰着他们。以下是有效的方法,也是非常简单的方法 附表1.rb 更新crontab(dev) 结果(开发) 0 10***/bin/bash-l-c'cd/my/rails/app&&rails\u ENV=development bundle exec rake somejob:run--silent>>log/cron\u log.log 2>>log/cron\u error\u log.log' 更新crontab(prod) 结果(prod) 0 10***/bin/bash-l-c'cd/my/rails/app&&rails\u ENV=production bundle exec rake somejob:run--silent>>log/cron\u log.log 2>>log/cron\u error\u log.log' 希望这能帮助一些人。
很高兴编写此代码 我遇到了一个问题,即每当选取cron作业-/usr/bin/bundle而不是/usr/local/bin/bundle时,没有为其设置环境 解决方案是将以下内容添加到schedule.rb的顶部
env 'PATH', ENV['PATH']
这个答案并没有告诉我们如何改变环境,它只是提供了一种让任务更干净的方法。请注意,
rake
命令实际上将RAILS\u ENV
添加到命令中,该命令最终出现在crontab
中。我得到了以下信息:失败:“sh-c”cd(已删除)/releases/20140127213854&&RAILS_ENV=production bundle exec每当--update crontab(已删除)--set environment=production--roles db''on(已删除)我可以将数组传递给环境吗?必须设置多个环境吗?但是,在处理多个环境时,这不是正确的方法,默认情况下,@environment变量始终为“production”。你必须where--set environment=development--write crontab
当你吐出你的作业时,这个答案就会起作用。env:PATH,env['PATH'}??在我的特殊情况下(启动延迟的作业),我使用命令“RAILS_env={@environment}{where.PATH}/bin/delayed_job start”
如果运行rake,也可以在上下文中使用rake
命令。
whenever --set 'environment=development' --update-crontab
whenever --set 'environment=production' --update-crontab
env 'PATH', ENV['PATH']