[Groonga-commit] droonga/droonga-engine at b79daac [master] Extract as a class

Back to archive index

Kouhei Sutou null+****@clear*****
Tue May 27 12:54:12 JST 2014


Kouhei Sutou	2014-05-27 12:54:12 +0900 (Tue, 27 May 2014)

  New Revision: b79daacad9457d301a2623e02b26403e87b2bb09
  https://github.com/droonga/droonga-engine/commit/b79daacad9457d301a2623e02b26403e87b2bb09

  Message:
    Extract as a class

  Modified files:
    lib/droonga/command/droonga_engine.rb

  Modified: lib/droonga/command/droonga_engine.rb (+78 -51)
===================================================================
--- lib/droonga/command/droonga_engine.rb    2014-05-27 12:38:24 +0900 (7d62cce)
+++ lib/droonga/command/droonga_engine.rb    2014-05-27 12:54:12 +0900 (378a08b)
@@ -30,10 +30,6 @@ require "droonga/plugin_loader"
 module Droonga
   module Command
     module DroongaEngine
-      module Signals
-        include ServerEngine::Daemon::Signals
-      end
-
       class SupervisorConfiguration
         DEFAULT_HOST = Socket.gethostname
         DEFAULT_PORT = 10031
@@ -194,54 +190,9 @@ module Droonga
           Droonga::Path.base
         end
 
-        def run_service(loop)
-          service_runner = ServiceRunner.new(loop, @configuration)
-          service_runner.run
-          service_runner
-        end
-
-        def run_serf(loop)
-          serf = Serf.new(loop, @configuration.engine_name)
-          serf.start
-          serf
-        end
-
         def run_main_loop
-          raw_loop = Coolio::Loop.default
-
-          serf = nil
-          service_runner = nil
-          trap(:INT) do
-            serf.shutdown if serf
-            service_runner.stop_immediately if service_runner
-          end
-          trap(Signals::GRACEFUL_STOP) do
-            serf.shutdown if serf
-            service_runner.stop_graceful if service_runner
-          end
-          trap(Signals::IMMEDIATE_STOP) do
-            serf.shutdown if serf
-            service_runner.stop_immediately if service_runner
-          end
-          trap(Signals::GRACEFUL_RESTART) do
-            old_service_runner = service_runner
-            service_runner = run_service(raw_loop)
-            service_runner.on_ready = lambda do
-              old_service_runner.stop_graceful
-            end
-          end
-          trap(Signals::IMMEDIATE_RESTART) do
-            old_service_runner = service_runner
-            service_runner = run_service(raw_loop)
-            old_service_runner.stop_immediately
-          end
-
-          serf = run_serf(raw_loop)
-          service_runner = run_service(raw_loop)
-          raw_loop.run
-          serf.shutdown if serf.running?
-
-          service_runner.success?
+          main_loop = MainLoop.new(@configuration)
+          main_loop.run
         end
 
         def open_log_file
@@ -271,6 +222,82 @@ module Droonga
         end
       end
 
+      class MainLoop
+        def initialize(configuration)
+          @configuration = configuration
+          @loop = Coolio::Loop.default
+        end
+
+        def run
+          @serf = run_serf
+          @service_runner = run_service
+
+          trap_signals
+          @loop.run
+          @serf.shutdown if****@serf*****?
+
+          @service_runner.success?
+        end
+
+        private
+        def trap_signals
+          trap(:TERM) do
+            stop_graceful
+            trap(:TERM, "DEFAULT")
+          end
+          trap(:INT) do
+            stop_immediately
+            trap(:INT, "DEFAULT")
+          end
+          trap(:QUIT) do
+            stop_immediately
+            trap(:QUIT, "DEFAULT")
+          end
+          trap(:USR1) do
+            restart_graceful
+          end
+          trap(:HUP) do
+            restart_immediately
+          end
+        end
+
+        def stop_graceful
+          @serf.shutdown
+          @service_runner.stop_graceful
+        end
+
+        def stop_immediately
+          @serf.shutdown
+          @service_runner.stop_immediately
+        end
+
+        def restart_graceful
+          old_service_runner = @service_runner
+          @service_runner = run_service
+          @service_runner.on_ready = lambda do
+            old_service_runner.stop_graceful
+          end
+        end
+
+        def restart_immediately
+          old_service_runner = @service_runner
+          @service_runner = run_service
+          old_service_runner.stop_immediately
+        end
+
+        def run_service
+          service_runner = ServiceRunner.new(@loop, @configuration)
+          service_runner.run
+          service_runner
+        end
+
+        def run_serf
+          serf = Serf.new(@loop, @configuration.engine_name)
+          serf.start
+          serf
+        end
+      end
+
       class ServiceRunner
         def initialize(raw_loop, configuration)
           @raw_loop = raw_loop
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index