在我的apps / controllers / 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
答案 0 :(得分:1)
我知道你提出这个问题已经过去了一年,但现在就进入Starling,我自己也没看到这个问题。
但看起来你的问题是(来自development.rb):
Workling::Return::Store.instance = Workling::Return::Store::MemoryReturnStore.new
Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new
需要:
Workling::Return::Store.instance = Workling::Return::Store::StarlingReturnStore.new
Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new
至少为了那些谷歌搜索者的利益...... :)。
答案 1 :(得分:0)
找到这个问题的答案。解决方法是从config / environments / development.rb
中删除Workling :: Return :: Store.instance行然后按如下方式替换get AND set调用:
在app / workers / model_worker.rb中:
store = Workling::Return::Store::StarlingReturnStore.new
key, value = @uid, @progress
store.set(key, value)
在app / controllers / models_controller.rb中:
store = Workling::Return::Store::StarlingReturnStore.new
@result = store.get(task.process_id)
显然,有一种方法可以在environments.rb中声明一个快捷方式,以避免每次都调用一个新的StarlingReturnStore,但我不能深入,因为我无法做到这一点。
无论如何,这个修复对我有用。我从每个后台作业获取输出,通过set报告到控制器中的get,然后由AJAX调用捕获并通过RJS报告给页面。
尼斯!