這是我正在使用的。 這就是工作被稱爲:
Setting::progress = "Starting..."
Navvy::Job.enqueue(EmailWorker, :async_email, stuff)
設置超級簡單:
class Setting < ActiveRecord::Base
def Setting::progress=(value)
setn = Setting.find_or_initialize_by_name("email_status")
setn.value = value
setn.save
end
end
和壯工工作EmailWorker是:
class EmailWorker
def self.async_email(options)
# send one at a time
total = options[:list].length
errors = []
options[:list].each_with_index do |email_addr, n|
begin
Setting::progress = "#{n+1}/#{total}: #{email_addr}"
Postoffice.one_notice(email_addr, options).deliver
rescue Exception => e
Setting::progress = "#{email_addr} #{e.message}"
errors << "<span class='red'>#{email_addr}</span> #{e.message}"
end
# get stack level too deep errors at random when this is removed. I don't get it.
sleep 0.05
end
Setting::progress = "DONE sending #{total} with #{errors.length} errors<br/>#{errors.join("<br/>")}"
"Done" # just for display in Navvy console output
end
end
然後觸發頁有這樣的:
<%-
# had to add this back as rails 3 took it away
def periodically_call_remote(options = {})
frequency = options[:frequency] || 10 # every ten seconds by default
code = "new PeriodicalExecuter(function() {#{remote_function(options)}}, #{frequency})"
javascript_tag(code)
end
-%>
<script type="text/javascript">
//<![CDATA[
check_var = true;
//]]>
</script>
<%= periodically_call_remote(
:condition => "check_var == true",
:url => { :controller => "application",
:action => "update" },
:frequency => '3') -%>
<div id="progress">
<p>Monitoring progress of emails</p>
</div>
多數民衆贊成一再呼籲10
而且繼承人的方法:它通過update.rjs警告有關挖土的
page.assign('check_var', @again)
page.replace_html "progress", :partial => "info", :locals => { :info => @progress }
page.visual_effect :highlight, 'progress', :startcolor => "#3333ff", :restorecolor => '#ffffff', :duration => 0.5
一個詞trigers就地更新
def update
raise "not for direct" if (!request.xhr?)
@progress = Setting::progress
@again = !(/^DONE/ =~ @progress)
render :action => "update"
end
- 因爲它會作爲一個後臺任務,直到遇難,它會保持執行你的舊應用程序,當你部署。你必須在舊的「當前」中殺死挖土者,並轉移到新的「當前」。
來源
2011-04-22 20:36:10
a9k
我想這是我必須要做的。雖然我認爲可能有另一種方式。我沒有提及這是一個單頁應用程序,只能在本地網絡上運行。 – fxe74 2011-04-18 01:42:22