Rails cron 与无论何时,设置环境

发布于 2024-07-26 02:25:31 字数 651 浏览 12 评论 0原文

如果您了解用于创建 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需要知道当前环境。 关于如何做到这一点有什么想法吗?

谢谢!

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 => '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

However when I update my crontab using

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

the cron jobs still have RAILS_ENV=development. My tasks on production and development are the same right now, I just need to change the environment variable because thinking_sphinx needs to know the current environment. Any ideas on how to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

深海少女心 2024-08-02 02:25:31

每当未检测到您的环境时,它就会默认使用生产环境。
为所有作业设置环境:

set :environment, 'staging' 

您可以使用 set:或每个作业

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

Whenever doesn't detect your environment, it just defaults to using production.
You can set the environment for all jobs using set:

set :environment, 'staging' 

Or per job:

every 2.hours do 
  runner 'My.runner', :environment => 'staging' 
end 
迷离° 2024-08-02 02:25:31

不要写入 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"

Don't write the RAILS_ENV variable. It should set it automatically.

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

It works in my app:

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"
若沐 2024-08-02 02:25:31

对于 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

For Whenever (0.9.2)

Use the @environment variable for environment check:

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
思念绕指尖 2024-08-02 02:25:31

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

在您的deploy.rb文件中,当您设置:whenever_command时,不要只需执行以下操作:

set :whenever_command, "bundle exec whenever"

相反,执行以下操作:

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

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

set :environment, ENV['RAILS_ENV']

瞧! 你准备好了。

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.

左耳近心 2024-08-02 02:25:31

这个问题已经开放很长时间了,所以我想我会分享在 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

结果(dev)

0 10 * * * /bin/bash -l -c 'cd /my/rails/app && RAILS_ENV=开发包 exec rake somejob:run --silent >>> log/cron_log.log 2>> 日志/cron_error_log.log'

更新 crontab(prod)

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

结果(prod)

0 10 * * * /bin/bash -l -c 'cd /my/rails/app && RAILS_ENV=生产包 exec rake somejob:run --silent >>> log/cron_log.log 2>> 日志/cron_error_log.log'

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

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!

一抹淡然 2024-08-02 02:25:31

如果您想在任何时候传递多个参数,请小心。
你必须这样做:

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

Watch out if you want to pass more than one param to whenever.
You have to do it like that:

whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
流星番茄 2024-08-02 02:25:31

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

set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }

require "whenever/capistrano"

Latest whenever allows easy Capistrano integration. You can add following to deploy.rb:

set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }

require "whenever/capistrano"
暗喜 2024-08-02 02:25:31

我遇到了一个问题,每当 cron 作业 - /usr/bin/bundle 而不是 /usr/local/bin/bundle 被拾取时,都没有设置环境。

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

env 'PATH', ENV['PATH']

I was having an issue where environment wasn't being set up for whenever cron jobs - /usr/bin/bundle was being picked up instead of /usr/local/bin/bundle.

The solution was to add following to top of schedule.rb

env 'PATH', ENV['PATH']
微凉 2024-08-02 02:25:31

在 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

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
沐歌 2024-08-02 02:25:31

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

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

I would consider using the "rake" shortcut to make it even cleaner:

every 1.day, :at => '4am' do
  rake "thinking_sphinx:stop"
  rake "thinking_sphinx:index"
  rake "thinking_sphinx:start"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文