I am new to Backgroundrb and am struggling to get a progress bar working on a long running job. I have it setup with memcache which is working. The code works when I check status using MiddleMan.worker(:transfer_worker).ask_result(:percent_complete) However, things get messed up when I run a second instance of this task and percent_complete is still 100% from the last run. So, I assume I need the ask_result version that uses the job_key so that I am looking at a separate instance from the cache. I tried result = MiddleMan.ask_result(:worker => :transfer_worker, :job_key => job_key) result[:percent_complete] which is similar to how ask_status was used in the Advanced Rails Recipes book, but this gives undefined method `ask_result'' for #<BackgrounDRb::ClusterConnection: 0x1f496a0> Any hints as to what I am screwing up? Thanks! Mark Lynn Sabado Technologies mark at sabado.com
Couple of things: Are you using memcache for session storage? (please, consider using it, if your rails app is already using memcache someway, its pretty cheap). Also, only when you are using memcache for result storage, your results will be available even if worker is processing some task, otherwise, result will be only available at the end of task (or if you are using thread pool). On Wed, 2008-09-17 at 15:04 -0400, Mark Lynn wrote:> I am new to Backgroundrb and am struggling to get a progress bar > working on a long running job. I have it setup with memcache which is > working. The code works when I check status using > > MiddleMan.worker(:transfer_worker).ask_result(:percent_complete) > > However, things get messed up when I run a second instance of this > task and percent_complete is still 100% from the last run.You need to reset the counter at the beginning of the task.> > So, I assume I need the ask_result version that uses the job_key so > that I am looking at a separate instance from the cache. I tried > > result = MiddleMan.ask_result(:worker > => :transfer_worker, :job_key => job_key) > result[:percent_complete] > > which is similar to how ask_status was used in the Advanced Rails > Recipes book, but this gives > > undefined method `ask_result'' for #<BackgrounDRb::ClusterConnection: > 0x1f496a0>Error is obvious, you can''t use ask_result like that. You need to go through the worker proxy only.
Here is a small patch that lets you reset memcache data using: MiddleMan.worker(:foo_worker).reset_memcache_result(key,value) diff --git a/lib/backgroundrb/rails_worker_proxy.rb b/lib/backgroundrb/rails_worker_proxy.rb index 0205f07..2b0a080 100644 --- a/lib/backgroundrb/rails_worker_proxy.rb +++ b/lib/backgroundrb/rails_worker_proxy.rb @@ -105,6 +105,13 @@ module BackgrounDRb middle_man.cache[gen_key(options)] end + def reset_memcache_result(job_key,value) + options = compact(:worker => worker_name,:worker_key => worker_key,:job_key => job_key) + key = gen_key(options) + middle_man.cache[key] = value + value + end + def return_result result result = Array(result) result.size <= 1 ? result[0] : result I will be commiting this to git, let me know,how it goes. On Wed, 2008-09-17 at 17:14 -0400, Mark Lynn wrote:> Thanks for the reply. > > I was using memcache for session storage, but stopped because we had > some special needs for session expiration. > > As to my issue, I am already resetting the counter at the beginning of > the backgroundrb task. However, this does not work because the rails > process often reaches the place where it reads :percent_complete > before the backgroundrb process has reset it. I see the memcached > "get" before I see any "set" operations. Is there a backgroundrb > method to clear the memcache value before starting the worker? I can > clear the memcache directly from Rails, but that means I am building > some assumptions about the memcache namespace being used into my code. > > I knew that the second ask_result was not correct? I don''t understand > the difference between the ask_result(p_data) method and the > ask_result(job_key) method. I am trying to get up to speed without > working through too much of the backgroundrb source code. I started > with the Advanced Rails Recipes book which is apparently totally > outdated and have looked through some of your code. I very much > appreciate your efforts on backgroundrb, but I think users could > really benefit from some examples - perhaps an updated version of the > Advanced Rails Recipe chapter. > > Thanks again. > > - Mark > > On Sep 17, 2008, at 4:43 PM, hemant kumar wrote: > > > Couple of things: > > > > Are you using memcache for session storage? (please, consider using > > it, > > if your rails app is already using memcache someway, its pretty > > cheap). > > Also, only when you are using memcache for result storage, your > > results > > will be available even if worker is processing some task, otherwise, > > result will be only available at the end of task (or if you are using > > thread pool). > > > > > > On Wed, 2008-09-17 at 15:04 -0400, Mark Lynn wrote: > >> I am new to Backgroundrb and am struggling to get a progress bar > >> working on a long running job. I have it setup with memcache which is > >> working. The code works when I check status using > >> > >> MiddleMan.worker(:transfer_worker).ask_result(:percent_complete) > >> > >> However, things get messed up when I run a second instance of this > >> task and percent_complete is still 100% from the last run. > > > > You need to reset the counter at the beginning of the task. > > > >> > >> So, I assume I need the ask_result version that uses the job_key so > >> that I am looking at a separate instance from the cache. I tried > >> > >> result = MiddleMan.ask_result(:worker > >> => :transfer_worker, :job_key => job_key) > >> result[:percent_complete] > >> > >> which is similar to how ask_status was used in the Advanced Rails > >> Recipes book, but this gives > >> > >> undefined method `ask_result'' for #<BackgrounDRb::ClusterConnection: > >> 0x1f496a0> > > > > Error is obvious, you can''t use ask_result like that. You need to go > > through the worker proxy only. > > > > > > > > > > Mark Lynn > Sabado Technologies > mark at sabado.com > 616-935-9910 > > >
Try usingkey = MiddleMan.new_worker(:worker => :transfer_worker, :worker_key => random_key)sleep 0.3 #hack if you''re running OS XMiddleMan.worker(:transfer_worker, key).method(:arg => args)Then use MiddleMan.worker(:transfer_worker, key).ask_result> From: mark at sabado.com> To: backgroundrb-devel at rubyforge.org> Date: Wed, 17 Sep 2008 15:04:57 -0400> Subject: [Backgroundrb-devel] ask_result usage> > > I am new to Backgroundrb and am struggling to get a progress bar > working on a long running job. I have it setup with memcache which is > working. The code works when I check status using> > MiddleMan.worker(:transfer_worker).ask_result(:percent_complete)> > However, things get messed up when I run a second instance of this > task and percent_complete is still 100% from the last run.> > So, I assume I need the ask_result version that uses the job_key so > that I am looking at a separate instance from the cache. I tried> > result = MiddleMan.ask_result(:worker > => :transfer_worker, :job_key => job_key)> result[:percent_complete]> > which is similar to how ask_status was used in the Advanced Rails > Recipes book, but this gives> > undefined method `ask_result'' for #<BackgrounDRb::ClusterConnection: > 0x1f496a0>> > Any hints as to what I am screwing up? Thanks!> > > Mark Lynn> Sabado Technologies> mark at sabado.com> > > > > _______________________________________________> Backgroundrb-devel mailing list> Backgroundrb-devel at rubyforge.org> http://rubyforge.org/mailman/listinfo/backgroundrb-devel _________________________________________________________________ Start het nieuwe schooljaar sterk en voordelig! http://www.startsterk.be