[Backgroundrb-devel] Trouble using backgroundrb

Ger Apeldoorn gapeldoorn at wehkamp.nl
Mon Sep 24 04:38:33 EDT 2007


Hi all, 

I'm a newbie to backgroundrb and am having trouble integrating it into
my rails-app. I'm using namespaces to differentiate between the parts of
my app.

When I want to create a new worker, it seems that it cannot find the
worker object. Here's my code:

-------------------- controller app/passwd/index --------------------
class Passwd::IndexController < ApplicationController
  layout 'default'

  def index
    $subtitle = "AIX user inventory tool"
  end

  def createReport
    Passwdentry.delete_all
    User.delete_all
    session[:job_key] = MiddleMan.new_worker(:class => "passwd:get_passwds_worker", :args => "")
  end

  def get_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 done
    render :text => "De database is bijgewerkt"
    MiddleMan.delete_worker(session[:job_key])
  end
end

--------------------lib/workers/get_passwds_worker.rb--------------------
# Put your code that runs your task inside the do_work method it will be
# run automatically in a thread. You have access to all of your rails
# models.  You also get logger and results method inside of this class
# by default.
class Passwd::GetPasswdsWorker < BackgrounDRb::Worker::RailsBase
  attr_reader :progress
  def do_work(args)
    @progress = 0
    @progressinterval = 100/System.count
    System.find_all.each do |sys|
      getAndParsePasswdFile(sys.name)
      @progress += @progressinterval
    end
  end

  def getAndParsePasswdFile(sys)
    output = %x[some command-line-magic ;)]
    output.each do |outputline|
      outputline = outputline.split(":")
      thisline = Passwdentry.new
      thisline.setvalues(outputline[0], sys, outputline[2]);
      thisline.save
    end
  end
end
Passwd::GetPasswdsWorker.register

--------------------app/views/passwd/index/createReport.rhtml--------------------
<h2>De database wordt voorzien van de laatste data.</h2>
<div id='progressbar' class="progress"></div>
<%= periodically_call_remote(:url => {:action => 'get_progress'}, :frequency => 1) %>

--------------------conf/backgroundrb.yml--------------------
--- 
:host: localhost
:port: 2000
:rails_env: development
:load_rails: true
:worker_dir: lib/workers

Your help is *very much* appreciated!

Thanks,
Ger Apeldoorn



More information about the Backgroundrb-devel mailing list