X-Git-Url: http://git.sourceforge.jp/view?p=shogi-server%2Fshogi-server.git;a=blobdiff_plain;f=webserver;h=edaf51e7b8695b36eb032f9e78fe56e0b7ce50c8;hp=1458c157b1962154c79577fd59af584683bd3933;hb=69acd0f281d560156cdd64609f5b309a8292f8c9;hpb=04e890d4453ab65b8574b70da09c10a738c1eeb1 diff --git a/webserver b/webserver index 1458c15..edaf51e 100755 --- a/webserver +++ b/webserver @@ -21,6 +21,13 @@ require 'timeout' require 'thread' require 'webrick' require 'socket' +require 'fileutils' + +def write_pid_file(file) + open(file, "w") do |fh| + fh.puts "#{$$}" + end +end class ShogiClient CLIENT_NAME = "web" @@ -98,16 +105,18 @@ end def usage $stderr.puts <<-EOF -USAGE: #{$0} [--daemon dir] [--port port] +USAGE: #{$0} [--daemon dir] [--port port] [--pid-file file] --daemon dir Run as a daemon. Log files are put in dir --port port Listening port for HTTP server (default 4080) + --pid-file file Write the process id to the file EOF end def parse_command_line options = Hash::new - parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT], - ["--port", GetoptLong::REQUIRED_ARGUMENT]) + parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT], + ["--port", GetoptLong::REQUIRED_ARGUMENT], + ["--pid-file", GetoptLong::REQUIRED_ARGUMENT]) parser.quiet = true begin parser.each_option do |name, arg| @@ -119,15 +128,16 @@ def parse_command_line raise parser.error_message end - dir = options["daemon"] || nil + dir = options["daemon"] + dir = File.expand_path(dir) if dir if dir && ! File.exist?(dir) FileUtils.mkdir(dir) end options["dir"] = dir || File.dirname(__FILE__) - options["port"] ||= 4080 options["port"] = options["port"].to_i - + options["pid-file"] = File.expand_path(options["pid-file"]) if options["pid-file"] + return options end @@ -146,6 +156,15 @@ def main http_config[:Logger] = WEBrick::Log.new(http_log_file) http_config[:AccessLog] = [[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]] + if $options["pid-file"] + http_config[:StartCallback] = Proc.new do + write_pid_file($options["pid-file"]) + end + http_config[:StopCallback] = Proc.new do + FileUtils.rm($options["pid-file"], :force => true) + end + end + server = WEBrick::HTTPServer.new(http_config)