2003-06-18 11:45:12 -04:00
|
|
|
=begin
|
|
|
|
external service manager
|
2011-05-18 20:07:25 -04:00
|
|
|
Copyright (c) 2000 Masatoshi SEKI
|
2003-06-18 11:45:12 -04:00
|
|
|
=end
|
|
|
|
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'thread'
|
2007-11-19 13:30:18 -05:00
|
|
|
require 'monitor'
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
module DRb
|
|
|
|
class ExtServManager
|
|
|
|
include DRbUndumped
|
2007-11-19 13:30:18 -05:00
|
|
|
include MonitorMixin
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
@@command = {}
|
|
|
|
|
|
|
|
def self.command
|
|
|
|
@@command
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.command=(cmd)
|
|
|
|
@@command = cmd
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize
|
2007-11-19 13:30:18 -05:00
|
|
|
super()
|
|
|
|
@cond = new_cond
|
2003-06-18 11:45:12 -04:00
|
|
|
@servers = {}
|
|
|
|
@waiting = []
|
|
|
|
@queue = Queue.new
|
|
|
|
@thread = invoke_thread
|
2004-11-08 10:45:31 -05:00
|
|
|
@uri = nil
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2004-11-08 10:45:31 -05:00
|
|
|
attr_accessor :uri
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
def service(name)
|
2007-11-19 13:30:18 -05:00
|
|
|
synchronize do
|
|
|
|
while true
|
|
|
|
server = @servers[name]
|
|
|
|
return server if server && server.alive?
|
|
|
|
invoke_service(name)
|
|
|
|
@cond.wait
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def regist(name, ro)
|
2007-11-19 13:30:18 -05:00
|
|
|
synchronize do
|
|
|
|
@servers[name] = ro
|
|
|
|
@cond.signal
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def unregist(name)
|
2007-11-19 13:30:18 -05:00
|
|
|
synchronize do
|
2011-05-18 17:19:18 -04:00
|
|
|
@servers.delete(name)
|
2007-11-19 13:30:18 -05:00
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def invoke_thread
|
|
|
|
Thread.new do
|
2011-05-18 17:19:18 -04:00
|
|
|
while true
|
|
|
|
name = @queue.pop
|
|
|
|
invoke_service_command(name, @@command[name])
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invoke_service(name)
|
2007-11-19 13:30:18 -05:00
|
|
|
@queue.push(name)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def invoke_service_command(name, command)
|
|
|
|
raise "invalid command. name: #{name}" unless command
|
2007-11-19 13:30:18 -05:00
|
|
|
synchronize do
|
2011-05-18 17:19:18 -04:00
|
|
|
return if @servers.include?(name)
|
|
|
|
@servers[name] = false
|
2007-11-19 13:30:18 -05:00
|
|
|
end
|
2004-11-08 10:45:31 -05:00
|
|
|
uri = @uri || DRb.uri
|
2009-02-08 07:06:42 -05:00
|
|
|
Process.detach spawn("#{command} #{uri} #{name}")
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|