[Backgroundrb-devel] Advanced Rails Recipe 43 doesn't work, can't figure a work around

hemant gethemant at gmail.com
Sat Jul 19 15:29:22 EDT 2008


On Sun, Jul 20, 2008 at 12:14 AM, Derrek Long <derrek at ridecharge.com> 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 => "#{@percent_complete *2}px"
>          page[:billingStatus].replace_html "#{@percent_complete}%"
>        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")


More information about the Backgroundrb-devel mailing list