2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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'
|
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 = []
|
2016-08-30 02:22:30 -04:00
|
|
|
@queue = Thread::Queue.new
|
2003-06-18 11:45:12 -04:00
|
|
|
@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]
|
2015-12-14 03:05:35 -05:00
|
|
|
return server if server&.alive?
|
2007-11-19 13:30:18 -05:00
|
|
|
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
|
2012-04-22 07:35:03 -04:00
|
|
|
if command.respond_to? :to_ary
|
|
|
|
command = command.to_ary + [uri, name]
|
|
|
|
pid = spawn(*command)
|
|
|
|
else
|
|
|
|
pid = spawn("#{command} #{uri} #{name}")
|
|
|
|
end
|
|
|
|
th = Process.detach(pid)
|
|
|
|
th[:drb_service] = name
|
|
|
|
th
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|