[Backgroundrb-devel] worker cleanup

hemant kumar gethemant at gmail.com
Wed Dec 12 23:06:17 EST 2007


Hi Jason,

On Wed, 2007-12-12 at 13:29 -0800, Jason LaPier wrote:
> I was using an older version of backgroundrb and today I started to
> play with the most recent release. First up, I'd like to say, thanks
> so much for this library and thanks for making it more stable. From
> the looks of things, I think it's going to be easier to use now than
> it was several months ago when I first started using it.

Thanks

> 
> I'd like to make sure I'm using everything the way it was intended, so
> let me give a brief overview of what I use bdrb for:
> Periodically, a user will need to perform a specific function that's
> initiated by a long-running task. This task is specific to each user,
> so what I did before was create a new worker each time the task was
> started (from within a Rails Controller) and give the worker a key
> based on the user's id (something like :key =>
> "task_worker_#{user.id}"). Then, in other controller methods, I could
> check the status of the task by looking up that key. If the task was
> complete, I would delete the worker.
> 
> The biggest problem with this workflow is that I'm relying on a
> controller action to delete the worker. If, for some reason, the
> worker never gets deleted, the next time the user tries to perform the
> same task, my controller is going to see that the worker with that key
> already exists, causing me some problems. The only "fix" I can think
> of is to give each worker a unique ID, and store that in the User
> model, so I can avoid looking up an old worker.
> 
> With the newest release of bdrb, I'm looking at how the workers are
> slightly different, and I think I still want to create a new instance
> of my worker for each user, each time this task is invoked. If I'm
> going to continue to do it this way, is there a way I can have the
> worker delete itself when the work is done? Is this a bad idea? Or
> should I be thinking about a way to use a single worker to handle the
> multiple user tasks?

How long does a task for one particular user takes to run? If it doesn't
take much long, then you can keep your worker around forever and just
pass, arguments from controller to appropriate worker method. It can be
done async if we code carefully.

however, if you do not handle all the user events in separate worker
(which can get nasty pretty quickly), and want to delete workers when
task is done, just call exit on them. For example:

class FooWorker < BackgrounDRb::MetaWorker
  set_worker_name :foo_worker
  def create(args = nil)
  end
  def some_task(user_args = nil)
    # do your work
    exit
  end
end

Since, each worker is a process, calling exit on them would remove the
worker. 



> 
> Thanks again,
> 
> - Jason L.
> 
-- 
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org



More information about the Backgroundrb-devel mailing list