[Backgroundrb-devel] Singleton-like Worker

David Lemstra david at lemstra.ca
Wed Aug 23 18:21:39 EDT 2006

Funny, I've been working on this for over a day now.
It uses a autostart singleton worker QueueWorker that acts as an
intermediary to your actual child workers. It sets up an array of
watchdog threads that each grab {args} from a queue and spawns a new
child worker when the previous child worker in the slot is finished.
It takes several layers of mutexes to make it all work but thanks to
singleton workers, its done w/ hardly any changes to the current code.

The original question cold be resolved by using this w/ just one
watchdog thread.

Yeah, eventually plan to add a priority Q as well...

Its's really messy right now though and am going on a week vacation
tomorrow. will send the dirty version if I don't get it cleaned up by then.
David Lemstra

P. Mark Anderson wrote:
> +1 useful on the thread pool.  I can't wait.  Really.  The queue I wrote
> has two priority levels so that quick or important tasks execute before
> those of normal priority.  It'd be great if Backgroundrb managed it all.
> pmark
> On Aug 23, 2006, at 12:44 PM, Ezra Zygmuntowicz wrote:
>> Sure I will take a look at it. But I might wait until after I
>> implement a thread pool to add it in. I am planning to make a
>> configurable thread pool for workers. So you could say that 30 workers
>> can be running at a time max. Then when you already have 30 workers
>> running, and you ask it to run another that one will go into the wait
>> pool until one of the first 30 finishes and makes room. This will make
>> it safer to run a lot of workers and you will be able to taylor the
>> size of the pool to fit your situation. But yeah do send me the code
>> so I can see what you did.
>> Cheers-
>> -Ezra
>> On Aug 23, 2006, at 12:30 PM, Michael Siebert wrote:
>>> built upon the singleton code, i made a little option :queue_run and
>>> modified Backgroundrb::rails to wait in that case. little hackery
>>> though, but it works.
>>> wanna have it? (as always not tested, no unit tests no nothing)
>>> 2006/8/23, Ezra Zygmuntowicz <ezmobius at gmail.com
>>> <mailto:ezmobius at gmail.com>>:
>>>     On Aug 23, 2006, at 11:33 AM, Michael Siebert wrote:
>>>>     Hello there,
>>>>     I need a Worker Class to be Singleton-like, that means there
>>>>     must be at maximum one Instance at a time. When a new Request is
>>>>     made to create another worker of this type, it should return and
>>>>     wait for the other instance to be destroyed.
>>>>     what i need to do: i have a worker who needs to do sth with java
>>>>     and openoffice, and it seems when i have more tha one worker
>>>>     simultaneously, everything goes terribly wrong...
>>>>     is there already sth in bgdrb or how would i do that?
>>>>     -- 
>>>>     Michael Siebert <info at siebert-wd.de <mailto:info at siebert-wd.de>>
>>>     Hey Michael-
>>>     Yeah there is already singleton type workers in the plugin.  It
>>>     doesn't work exactly as you want it to though. When you create a
>>>     singleton worker it will start doing its thing and any call to
>>>     new_worker for the same singleton will just return the key for
>>>     the already existing singleton worker. So you are always
>>>     garaunteed to get the same singleton. There is not currently any
>>>     support for returning and then starting up after the singleton
>>>     dies. But check it out anyway as it might do what you want.
>>>     There is also now an option to have a singleton worker so there
>>>     is always
>>>     only one of a certain worker. Even if you call new_worker twice
>>>     with this
>>>     option enabled you will get back the same instance of the class.
>>>     Do not
>>>     create a worker class and use it as a singleton and a non
>>>     singleton worker
>>>     at the same time. This is not supported.
>>>     MiddleMan.new_worker(:class => :foo_worker,
>>>                          :args => "Bar",
>>>                          :job_key => :singleton_worker,
>>>                          :singleton => true)
>>>     -Ezra

More information about the Backgroundrb-devel mailing list