[Backgroundrb-devel] Forward of moderated message

Ezra Zygmuntowicz ezmobius at gmail.com
Wed Aug 16 13:38:34 EDT 2006


	OK I know whats happening. Your while loop completes and calls kill  
on the worker before your task_progress  controller method ever gets  
called> So the worker is deleted and when you try to access it from  
rails you get an error because there is no longer a worker at that  
job key. The kill method is meant to be used within a worker that you  
fire and forget. If you want to get the results then you should not  
call kill right in your worker. You can call kill or delete_worker  
from rails in your done method.

	So if you want to see the progress bar working I suggest you put a  
sleep in the while loop. A while loop with no sleep can count to 1200  
before you could blink and then the kill method gets called and your  
screwed. So add sleep 0.3 or somethign within the while loop so you  
can see it work.

	And if you want to see the results of your worker in rails you  
should call the kill method from rails through the middlwman. If you  
want to just fire and forget a worker then you want to call kill  
within the worker when it is done working.

	Other then that your code is fine. Give it another shot with some  
sleep in the loop.

Cheers-
-Ezra



On Aug 16, 2006, at 10:25 AM, mattp at digimonkey.com wrote:

> Weird is, I had it working a second ago and then reloaded the  
> webrick server and now it's erroring out with:
>
> NoMethodError (You have a nil object when you didn't expect it!
> The error occured while evaluating nil.progress):
>     /app/controllers/operations_controller.rb:14:in `task_progress'
>
> Here's the code --
>
> [ /lib/workers/ping_worker.rb ]
>
> class PingWorker < BackgrounDRb::Rails
>
>   attr_accessor :progress
>
>   def do_work(args)
>     @progress = 0
>     calculate_the_meaning_of_life(args)
>     kill
>   end
>
>   def calculate_the_meaning_of_life(args)
>     while @progress < 100
>       @progress += 1
>     end
>   end
>
> end
>
> [ operations_controller.rb ]
>
> class OperationsController < ApplicationController
>   sidebar :general
>   sidebar :operations
>
> # start new worker and put the job_key into the session so you can
> # get the status of your job later.
> def background_task
>   session[:job_key] = MiddleMan.new_worker(:class => :ping_worker,
>                                            :args => {:baz =>  
> 'hello!', :qux => 'another arg!'})
> end
>
> def task_progress
>   if request.xhr?
>     progress_percent = MiddleMan.get_worker(session 
> [:job_key]).progress
>     render :update do |page|
>       page.call('progressPercent', 'progressbar', progress_percent)
>       page.redirect_to( :action => 'done')   if progress_percent >=  
> 100
>     end
>   else
>     redirect_to :action => 'index'
>   end
> end
>
> def results
>   @results = MiddleMan.get_worker(session[:job_key]).results
>   MiddleMan.delete_worker(session[:job_key])
> end
>
> def done
> end
>
> end
>
> Quoting Ezra Zygmuntowicz <ezmobius at gmail.com>:
>
>>
>> On Aug 16, 2006, at 10:08 AM, backgroundrb-devel-   
>> bounces at rubyforge.org wrote:
>>
>>>
>>> From: mattp at digimonkey.com
>>> Date: August 16, 2006 10:01:26 AM PDT
>>> To: backgroundrb-devel at rubyforge.org
>>> Subject: newbie needs help
>>>
>>>
>>> I'm sure this is the billionth newbie help request on here, but  
>>> I   sure am hitting my head against the wall on this.
>>>
>>> I've tried numberous of Ezra's examples that I was able to find    
>>> online.  The best I'm able to get is nothing happens and    
>>> development log says:
>>>
>>> NoMethodError (undefined method `progress' for #<PingWorker:  
>>> 0x3a786c0>):
>>>    /app/controllers/operations_controller.rb:13:in `get_progress'
>>>
>>> Any ideas?  I'm sure there's a basic thing I'm not doing  
>>> right...   but I couldn't figure out what that might be, based on  
>>> the   documentation.
>>>
>>> [mp]
>>>
>>>
>>
>>
>> Hi Matt-
>>
>> 	It looks like you are missing something. Can you post your worker
>> class? It looks like you either need a progress method or you need to
>> set attr_accessor :progress in your worker.
>>
>>
>> -Ezra
>
>



More information about the Backgroundrb-devel mailing list