I got the same problem when I was trying to implement something in production, 
having read the same Recipie. Exactly the same error in fact. I replicated it 
in the rails console when trying to query the worker. Dropping out of the 
console and starting again actually worked but of course doesn't help when you 
want to query straight away.

Packet::InvalidWorker

I got the impression that it was trying to query the worker just a fraction 
before the worker was ready. I couldn't gather much more information than that. 
In the end I had to abandon BackgrounDRb for my project and use a cronjob and 
basic queue due to time constraints, so I'd really like to know what the 
problem was so that I don't give up on BDRb alltogether. I have to say I found 
the rubyforge site documentation far too lacking in details so again I look 
forward to see more in depth examples.

If it is all fixed now then its a bit late for this project so maybe I'll give 
it another look for the next time it BDRb seems like an appropriate solution.

RobL


hemant wrote:
On Sun, Jul 20, 2008 at 12:14 AM, Derrek Long <[EMAIL PROTECTED]> wrote:
Hello,

I'm trying to learn how to use BackgroundRB to run a long running process while 
showing status to the user via a webpage.
1. I tried using Advanced Rails Recipe 43, however, it appears as though backgroundrb has been 
updated since that book was published (2 months ago) as methods like "register_status", 
"ask_stutus" no longer exist.

Being one of the contributors for the recipe. I am terribly sorry for
this, but newer caching policies are a lot more stable. I am trying to
talk with Mike for releasing a new pdf that covers new version.


2. I tried reading the everything on http://backgroundrb.rubyforge.org/
3. I tried to update the recipe code with what I learned from reading the 
backgroundrb webpage
4. I failed spectacularly

Here's my code reworked to be simpler:

---------------
class BillingWorker < BackgrounDRb::MetaWorker
 set_worker_name :billing_worker
 set_no_auto_load(true)

 def create(args = nil)
   cache[worker_key] = 0
   index = 0
   args.times do
     sleep(4)
     index = index + 1
     cache[worker_key] =(index * 100) / args
   end
   exit
 end
end

class PaymentsController < ApplicationController
 def async_test
   session[:job_key] = Bill.find(1).start_test
   redirect_to :action => 'check_async_test_status'
 end
 def check_async_test_status
   @percent_complete = Bill.test_status(session[:job_key])
   if request.xhr?
     if @percent_complete == 100
       render :update do |page|
         flash[:notice] = "test is complete"
         session[:job_key] = nil
         page.redirect_to :action => "pending"
       end
     else
       render :update do |page|
         page[:billingStatus].setStyle :width => "[EMAIL PROTECTED] *2}px"
         page[:billingStatus].replace_html "[EMAIL PROTECTED]"
       end
     end
   end
 end
end

class Bill < ActiveRecord::Base
 def start_test
   MiddleMan.new_worker(:worker => :billing_worker, :worker_key => "foobar", :data 
=> 5)
 end

 def self.test_status(job_key)
   MiddleMan.worker(:billing_worker, "foobar").ask_result(job_key)
   # also tried MiddleMan.worker(:billing_worker).ask_result(job_key)
 end
end
---------------

hitting /payments/async_test always results in
Packet::InvalidWorker
/usr/local/lib/ruby/gems/1.8/gems/packet-0.1.8/lib/packet/packet_connection.rb:52:in
 `ask_worker'
/home/derrek/repos/prod/riderway/trunk/vendor/plugins/backgroundrb/server/lib/master_worker.rb:123:in
 `get_result_object'

I can't seem to get at the cache[worker_key] value.  Any help would be greatly 
appreciated.

Two things are causing problem:

1. MiddleMan.new_worker(...) was not returning the "worker_key" with
which it was created and hence, your session[:job_key] was probably
wrong. I fixed it in latest git push and have added testcase to cover
that also, hence shouldn't happen again.

2. Now, in your worker, you are using worker_key to cache the result:
cache[worker_key] = 0

Think of cache object as hash and whatever key you used here, must be
used while getting the results back and hence your code for fetching
the results should be:

MiddleMan.worker(:billing_worker, "foobar").ask_result("foobar")
_______________________________________________
Backgroundrb-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/backgroundrb-devel

_______________________________________________
Backgroundrb-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/backgroundrb-devel

Reply via email to