Rails cron随时随地,设置环境 [英] Rails cron with whenever, setting the environment

查看:131
本文介绍了Rails cron随时随地,设置环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能只有当你知道任何gem创建cron作业才有意义。我有一个任务在我的schedule.rb像

 每1.day,:at => '4am'do 
commandcd#{RAILS_ROOT}& rake thinking_sphinx:stop RAILS_ENV =#{RAILS_ENV}
commandcd#{RAILS_ROOT}& rake thinking_sphinx:index RAILS_ENV =#{RAILS_ENV}
commandcd#{RAILS_ROOT}& rake thinking_sphinx:start RAILS_ENV =#{RAILS_ENV}
end
pre>

但是当我使用

 更新我的crontab时 - update-crontab appname --set environment = production 

cron作业仍然具有RAILS_ENV = development。我的生产和开发任务现在是一样的,我只需要改变环境变量,因为thinking_sphinx需要知道当前的环境。有关如何执行此操作的任何建议吗?



谢谢!

解决方案

我会考虑使用rake快捷方式,使它更清洁:

 每1.day,:at = '4am'do 
rakethinking_sphinx:stop
rakethinking_sphinx:index
rakethinking_sphinx:start
end


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!

解决方案

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

这篇关于Rails cron随时随地,设置环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆