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

Derrek Long derrek at ridecharge.com
Sat Jul 19 14:44:00 EDT 2008


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.
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.

Thanks,
-Derrek


More information about the Backgroundrb-devel mailing list