我正在为Resque/Redis使用resque-status ...
https://github.com/quirkey/resque-status
我基本上想创建一个新的Sinatra方法..类似于下面的内容。我只有2个JobsWithStatus,所以它可以返回两个或一个特定的,我真的不关心。
post '/getstatus' do
# return status here of all kinds (or specific)
end然后,我想通过jquery在前端输出完成百分比,使用每5秒检查一次状态的轮询计时器。
这就是我的东西
post '/refresh' do
job_id = PostSaver.create(:length => Forum.count)
status = Resque::Status.get(job_id)
redirect '/'
end它在文档中说我可以只使用status.pct_complete,但它总是返回0?即使这样,我还是ruby的新手,即使变量显示了正确的% complete,我也不确定如何让变量在单独的sinatra条目中工作(在/getstatus中而不是在/refresh中)。
我试过了,但是它一直返回0
post '/refresh' do
job_id = PostSaver.create(:length => Forum.count)
status = Resque::Status.get(job_id)
sleep 20
status.pct_complete.to_s
end发布于 2011-12-30 05:23:18
我在reddit…上看到了你的问题
要使状态返回为非0的值,您需要在运行计算期间使用at (http://rubydoc.info/github/quirkey/resque-status/master/Resque/JobWithStatus:at)方法设置百分比。
您可能不希望在操作中进行sleep调用。计时器应该在jQuery中。
共享状态
post '/refresh' do
job_id = PostSaver.create(:length => Forum.count)
status = Resque::Status.get(job_id)
sleep 20
"{'percent_complete':#{status.pct_complete},'job_id':'#{job_id}'}"
end然后在任何情况下获取状态(一些jQuery#ajax调用?),您可以从返回的JSON中获取job_id,然后在您的下一个请求中,您可能会这样做:
post '/status' do
status = Resque::Status.get(params['job_id'])
"{'percent_complete':#{status.pct_complete}}"
endhttps://stackoverflow.com/questions/8664249
复制相似问题