在我的应用程序/控制器/model_controller.rb 我有(模型/方法的名称已更改以保护无辜者):
def background_sync
@background_task_uid = Model.async_process_model_cache({:name => 'name'})
@model_sync = ModelSync.new # Adds a new record in the queue of pending jobs
@model_sync.process_id = @background_task_uid # Puts the background process id into the new ModelSync record
@model_sync.save
end
在 app/workers/model_worker.rb 中:
def process_model_cache(options={})
[long background task]
result = Workling::Return::Store.set(options[:uid], 'done')
result = Workling::Return::Store.get(options[:uid]) #=> 'done'
end
请注意,set 和 get 在此工作程序中正常运行。问题是后来...
回到 app/views/model/index.html.rb,我有一个原型助手轮询对同一控制器的请求以确定后台作业是否完成:
<%= periodically_call_remote( :url => { :action => :background_complete }, :frequency => 5, :update => 'status_div') %>
而在apps/controllers/model_controller.rb中,检查后台作业状态的函数:
def background_complete
@background_task_uid = ModelSync.find(:last)
if @background_task_uid
@background_task_uid.each do |task|
unless task.process_id == "" || task.process_id.nil?
@result = Workling::Return::Store.get(task.process_id) #=> nil
if @result.nil?
task.destroy
end
else
task.destroy
end
unless @result.nil?
render :text => "<span style='font-size:12px;margin-left:20px;'>"+@result+"</span>"
else
@result = "none" if @result.nil?
render :text => "<span style='font-size:12px;margin-left:20px;'>"+@result+"</span>"
end
end
end
end
最后,在 config/environments/development.rb 中:
Workling::Return::Store.instance = Workling::Return::Store::MemoryReturnStore.new
Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new
(请注意,我已经尝试在最后一行注释和不注释的情况下运行它。如果注释掉,Workling 将恢复为 Spawn 而不是 Starling。)
所以问题是我在 background_complete 的这一行得到了 nil:
@result = Workling::Return::Store.get(task.process_id) #=> nil