OSDN Git Service

[shogi-server] Bump up the revision to 20201206
[shogi-server/shogi-server.git] / shogi-server
index 530dbb1..e78d421 100755 (executable)
@@ -34,6 +34,8 @@ require 'shogi_server'
 require 'shogi_server/config'
 require 'shogi_server/util'
 require 'shogi_server/league/floodgate_thread.rb'
+require 'pathname'
+require 'set'
 require 'tempfile'
 
 #################################################
@@ -123,6 +125,21 @@ EXAMPLES
            floodgate-0-240.conf.sample or shogi_server/league/floodgate.rb 
            for format details.
 
+GRACEFUL SHUTDOWN
+
+       A file named "STOP" in the base directory prevents the server from
+       starting new games including Floodgate matches.
+       When you want to stop the server gracefully, first, create a STOP file
+
+          $ touch STOP
+
+       then wait for a while until all the running games complete.
+       Now you can stop the process with no game interruptted by the 'kill'
+       command.
+
+       Note that when a server gets started, a STOP file, if any, will be
+       deleted automatically.
+
 FLOODGATE SCHEDULE CONFIGURATIONS
 
            You need to set starting times of floodgate groups in
@@ -256,6 +273,8 @@ def check_command_line
 
   if $options["pid-file"] 
     $options["pid-file"] = File.expand_path($options["pid-file"], $topdir)
+    path = Pathname.new($options["pid-file"])
+    path.dirname().mkpath()
     unless ShogiServer::is_writable_file? $options["pid-file"]
       usage
       $stderr.puts "Can not create the pid file: %s" % [$options["pid-file"]]
@@ -417,8 +436,11 @@ def main
 
   $league.dir = $topdir
 
+  # Set of connected players
+  $players = Set.new
+
   config = {}
-  config[:BindAddress] = "0.0.0.0"
+  config[:BindAddress] = nil # both IPv4 and IPv6
   config[:Port]       = port
   config[:ServerType] = WEBrick::Daemon if $options["daemon"]
   config[:Logger]     = $logger
@@ -444,8 +466,9 @@ def main
 
   srand
   server = WEBrick::GenericServer.new(config)
-  ["INT", "TERM"].each do |signal| 
+  ["INT", "TERM"].each do |signal|
     trap(signal) do
+      $players.each {|p| p.kill}
       server.shutdown
       setup_floodgate.kill
     end
@@ -458,6 +481,11 @@ def main
   $stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"] 
   log_message("server started [Revision: #{ShogiServer::Revision}]")
 
+  if ShogiServer::STOP_FILE.exist?
+    log_message("Deleted the STOP file")
+    ShogiServer::STOP_FILE.delete
+  end
+
   server.start do |client|
     begin
       # client.sync = true # this is already set in WEBrick 
@@ -472,6 +500,14 @@ def main
       log_message(sprintf("user %s login", player.name))
       login.process
       player.setup_logger($options["player-log-dir"]) if $options["player-log-dir"]
+
+      $mutex.lock
+      begin
+       $players.add(player)
+      ensure
+        $mutex.unlock
+      end
+
       player.run(login.csa_1st_str) # loop
       $mutex.lock
       begin
@@ -481,6 +517,7 @@ def main
         player.finish
         $league.delete(player)
         log_message(sprintf("user %s logout", player.name))
+       $players.delete(player)
       ensure
         $mutex.unlock
       end