OSDN Git Service

* [shogi-server] Support a graceful shutdown. (Closes #38544)
authorDaigo Moriwaki <daigo@debian.org>
Sat, 25 Aug 2018 09:41:38 +0000 (18:41 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sat, 25 Aug 2018 09:41:38 +0000 (18:41 +0900)
A file named "STOP" in the base directory prevents the server from
starting new games including Floodgate matches.

changelog
shogi-server
shogi_server.rb
shogi_server/command.rb
shogi_server/league/floodgate_thread.rb

index 707b575..b3df157 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,10 @@
+2018-08-25  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server] Support a graceful shutdown.
+         A file named "STOP" in the base directory prevents the server from
+         starting new games including Floodgate matches.
+         (Closes #38544)
+
 2018-04-07  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server] Give more penalty on assigning matches with same
 2018-04-07  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server] Give more penalty on assigning matches with same
index 530dbb1..21bc9d1 100755 (executable)
@@ -123,6 +123,21 @@ EXAMPLES
            floodgate-0-240.conf.sample or shogi_server/league/floodgate.rb 
            for format details.
 
            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
 FLOODGATE SCHEDULE CONFIGURATIONS
 
            You need to set starting times of floodgate groups in
@@ -458,6 +473,11 @@ def main
   $stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"] 
   log_message("server started [Revision: #{ShogiServer::Revision}]")
 
   $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 
   server.start do |client|
     begin
       # client.sync = true # this is already set in WEBrick 
index 69dae89..6a8c21f 100644 (file)
@@ -28,6 +28,7 @@ require 'digest/md5'
 require 'webrick'
 require 'fileutils'
 require 'logger'
 require 'webrick'
 require 'fileutils'
 require 'logger'
+require 'pathname'
 
 require 'shogi_server/compatible'
 require 'shogi_server/board'
 
 require 'shogi_server/compatible'
 require 'shogi_server/board'
@@ -58,6 +59,7 @@ RELOAD_FILES = ["shogi_server/league/floodgate.rb",
                 "shogi_server/league/persistent.rb",
                 "shogi_server/pairing.rb"]
 BASE_DIR = File.expand_path(File.dirname(__FILE__))
                 "shogi_server/league/persistent.rb",
                 "shogi_server/pairing.rb"]
 BASE_DIR = File.expand_path(File.dirname(__FILE__))
+STOP_FILE = Pathname.new(BASE_DIR).join("STOP")
 
 def reload
   RELOAD_FILES.each do |f|
 
 def reload
   RELOAD_FILES.each do |f|
@@ -66,6 +68,13 @@ def reload
 end
 module_function :reload
 
 end
 module_function :reload
 
+##
+# When the STOP file exists, starting a new game is not allowed.
+def available?
+   return !STOP_FILE.exist?
+end
+module_function :available?
+
 class Logger < ::Logger
 
   def initialize(logdev, shift_age = 0, shift_size = 1048576)
 class Logger < ::Logger
 
   def initialize(logdev, shift_age = 0, shift_size = 1048576)
index dedbaae..ad8684d 100644 (file)
@@ -497,7 +497,10 @@ module ShogiServer
     end
 
     def call
     end
 
     def call
-      if (! Login::good_game_name?(@game_name))
+      if (!ShogiServer::available?)
+       @player.write_safe("##[ERROR] As the service is going to shutdown shortly, starting new games is not allowed now.\n")
+        return :continue
+      elsif (! Login::good_game_name?(@game_name))
         @player.write_safe(sprintf("##[ERROR] bad game name: %s.\n", @game_name))
         if (/^(.+)-\d+-\d+F?$/ =~ @game_name)
           if Login::good_identifier?($1)
         @player.write_safe(sprintf("##[ERROR] bad game name: %s.\n", @game_name))
         if (/^(.+)-\d+-\d+F?$/ =~ @game_name)
           if Login::good_identifier?($1)
index 0c1759b..a665704 100644 (file)
@@ -103,7 +103,11 @@ module ShogiServer
 
             next_instances = leagues.collect do |fg|
               unless (fg.next_time - Time.now) > 0
 
             next_instances = leagues.collect do |fg|
               unless (fg.next_time - Time.now) > 0
-                start_games(fg)
+                if ShogiServer::available?
+                  start_games(fg)
+               else
+                  log_message("The STOP file prevents from starting new Floodgate matches")
+               end
                 fg.charge # updates next_time
               end
               fg
                 fg.charge # updates next_time
               end
               fg