[Backgroundrb-devel] Req: Workers as singletons

Günter Ladwig gladwig at gmx.de
Mon Jul 3 11:28:53 EDT 2006


Hi,

it would be nice to be able to specify workers as singletons. By this  
I mean that every call to the new_worker method returns the same  
instance of said worker. This can be done transparently either by  
adding a new argument to new_worker (something like :singleton =>  
true) or adding a new method, like I did in my installation of  
BackgrounDrb:

     def get_worker_by_class(klass)
       klasse = klass.to_s.split('_').inject('') { |total,part| total  
<< part.capitalize }
       @jobs.each do |e|
         @logger.debug e.inspect
         if e[1].class.name == klasse
           return e[0]
         end
       end
       return nil
     end

     def new_singleton_worker(opts={})
       @mutex.synchronize {
         worker = get_worker_by_class(opts[:class])
         if worker.nil?
           job_key = opts[:job_key] || gen_key
           unless self[job_key]
             self[job_key] = instantiate_worker(opts[:class]).new(opts 
[:args])
             return job_key
           else
             raise "job_key: #{job_key} already refers to a running  
worker"
           end
         else
           return worker
         end
       }
     end

First the jobs array is checked if an instance of the requested  
worker class is already available. If yes, it's returned otherwise a  
new instance is created using the standard instantiate_worker method.

Could you include something like this in the next version?

Günter


More information about the Backgroundrb-devel mailing list