2013-03-01 53 views
3

在我們的登臺服務器上,我們在生產環境中運行我們的Rails應用程序,以便儘可能與我們的生產服務器相似。我們正在使用每當創建我們的crontab。但是,我們需要爲我們的網站地圖生成運行稍微不同的rake任務,因此它不能Ping Google和Bing。Capistrano和每當階段變量

在deploy.rb,我們有: set :stages, %w(production staging),但在這兩種部署/ staging.rb和部署/ production.rb我們:rails_env, "production"集,所以我不能使用Rails.env

schedule.rb,我想要做的事,如:

every :day, at: '1am' do 
    if @stage == 'production' 
    rake 'sitemap:refresh' 
    else 
    rake 'sitemap:refresh:no_ping' 
    end 
end 

我怎樣才能讓這個變量可用?

更新

我能夠把

set :whenever_variables, defer { "stage=#{stage}" }

到我的部署/ staging.rb來解決它。然後我就進入@stage在schedule.rb

+0

我得到一個錯誤 - 未定義的方法'延遲」。除了每當/ capistrano之外,我是否需要包含其他內容? – 2014-05-29 22:19:44

+0

@VivekRao我想出了一個解決方案,我把它作爲一個單獨的答案在下面發佈。 – dmccabe 2016-11-16 15:17:00

回答

5

真的不知道這是否會工作,但值得一試(從whenever readme

# deploy.rb 
set :whenever_environment, defer { stage } 
require "whenever/capistrano" 

然後在schedule.rb

set :environment, ENV['RAILS_ENV'] 

case environment 
when 'production', 'staging' 
    ... 
when 'production' 
    ... 
when 'staging' 
    ... 
end 

更新:你也可以使用

set(:whenever_command) { "STAGE=#{stage} bundle exec whenever" } 

這樣你有acc環境變量STAGEschedule.rb

+0

問題在於,在我們的例子中,environment/RAILS_ENV總是等於「生產」。我需要在capistrano階段。 – Jason 2013-03-01 16:33:08

+0

我還沒有嘗試過,但我認爲'set:every_environment,defer {stage}'所做的是每當環境設置爲'stage'的值時。 – jvnill 2013-03-01 16:36:42

+0

你也可以嘗試我更新的答案:) – jvnill 2013-03-01 16:42:08

0

@jvnill有正確的答案。如果你正在使用config/deploy /作爲單獨的環境,你可以通過將設置置於適當的階段使它保持整潔。

# config/deploy/staging.rb 
set :whenever_command, "STAGE=#{stage} bundle exec whenever" 

# config/deploy/production.rb 
set :whenever_command, "STAGE=production bundle exec whenever" 

# config/deploy.rb 
require "whenever/capistrano" 

通過要求'每當/ capistrano',你負責在部署後運行:everyize_update。

https://github.com/javan/whenever/blob/master/lib/whenever/capistrano/v2/hooks.rb

1

defer方法不會出現在斯特拉努的更高版本(3.4.1)/工作時(0.9.7)。我與NoMethodError: undefined method 'defer' for main:Object發生錯誤。下面是我工作:

deploy.rb:

set :whenever_environment, Proc.new { fetch :stage } 

schedule.rb:

if @environment == 'production' 
    every 15.minutes, roles: [:my_custom_role] do 
    rake 'my_rake_task' 
    end 
end