[Backgroundrb-devel] getting started

hemant gethemant at gmail.com
Fri Jan 11 17:06:08 EST 2008

Hi Tim,

On Jan 12, 2008 2:49 AM, Tim Glen <tim at pivotib.com> wrote:
> Hey all,
> Been reading through all the old posts (and the docs) to familiarize
> myself but I still feel a bit lost.
> I'm on trunk and am having a bit of a hard time knowing where to start
> with the particular I'm trying to solve.
> So far, I've downloaded backgroundrb, done the basic rails setup,
> created my worker and started it using `script/backgroundrb start`.
> Here's the behaviour I'm looking to create:
> 1. When a file is uploaded, the worker needs to be triggered to start
> processing the file (this is a 20-60 minute job, depending on the file)
> 2. The user should be redirected to a page where they can see a status
> of the processing job (the ideal would be a percentage or status bar
> but I realize that's an implementation concern). most importantly,
> they need to know when it's been completed.
> Here are my questions - since this only needs to take place every
> month or so, I don't really feel like I need a long-running worker
> necessarily. From the docs, it seemed like I could call:
> MiddleMan.ask_work(:worker => :sales_processor, :worker_method
> => :do_work, :data => @upload.id)
> In the :do_work method, I'm calling register_status but am receiving
> nil when I call ask_status
> I guess I just feel like I'm shooting in the dark here - not sure if
> i'm going in the right direction at all. Should I be using ask_work
> with register_status? Perhaps send_request (though I don't want the
> http request to wait for the worker to be completed)? Does the worker
> need to be running on a schedule before I can use either of these?
> I would appreciate any direction.

Start your long running task like this:

MiddleMan.new_worker(:worker => :sales_processor_worker,:data => @upload.id)
# I think, you need _worker there, just check the name thats there in
worker class. Whatever name is used
# there, you must use it here too.

Now in sales_processor_worker

class SalesProcessorWorker < BackgrunDRb::MetaWorker
  set_worker_name :sales_processor_worker
  set_no_auto_load true
  # whatever you passed as an :data is available here as an argument
  def create(args = nil)
    register_status("Processing started")
    # do some more processing here

>From controller get status like this:

MiddleMan.ask_status(:worker => :sales_processor_worker)

Thats about it. The key is, if you don't need a perpetually running
worker around, disable auto loading of worker and start your worker
using MiddleMan.new_worker and then query status. Hope this helps.

Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.


More information about the Backgroundrb-devel mailing list