Skip to content

Instantly share code, notes, and snippets.

@tobias
Created April 27, 2011 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tobias/944090 to your computer and use it in GitHub Desktop.
Save tobias/944090 to your computer and use it in GitHub Desktop.
class ServiceService
def initialize(options)
@queue = Queue.new( '/queues/service_request' )
@sub_services = []
@running = true
@sub_services_mutex = Mutex.new
end
def start()
Thread.new do
while @running
request = @queue.receive( :timeout => 5000 )
if request && @running
@sub_services_mutex.synchronize do
service = request.delete( :service_class ).new( request )
service.start
@sub_services << service
end
end
end
end
end
def stop()
@running = false
@sub_services_mutex.synchronize { @sub_services.each( &:stop ) }
end
end
#usage
queue = Queue.new( '/queues/service_request' )
options = {
:service_class => MyDynamicService,
:foo => :bar,
:other_option => :bacon
}
queue.publish( options )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment