Break out scripted service as a separate class
authorDavin McCall <davmac@davmac.org>
Fri, 26 May 2017 09:29:13 +0000 (10:29 +0100)
committerDavin McCall <davmac@davmac.org>
Mon, 29 May 2017 13:33:41 +0000 (14:33 +0100)
src/load_service.cc
src/service.cc
src/service.h

index cdd5b8a3469f588f94b162b963e1f0999ae46acc..7cf862e77fe011533b0bba7a11c4d0ed22350173 100644 (file)
@@ -514,11 +514,15 @@ ServiceRecord * ServiceSet::loadServiceRecord(const char * name)
                     rval = new bgproc_service(this, string(name), std::move(command),
                         command_offsets, &depends_on, &depends_soft);
                 }
+                else if (service_type == ServiceType::SCRIPTED) {
+                    rval = new scripted_service(this, string(name), std::move(command),
+                                                command_offsets, &depends_on, &depends_soft);
+                    rval->setStopCommand(stop_command, stop_command_offsets);
+                }
                 else {
                     rval = new ServiceRecord(this, string(name), service_type, std::move(command), command_offsets,
                             &depends_on, &depends_soft);
                 }
-                rval->setStopCommand(stop_command, stop_command_offsets);
                 rval->setLogfile(logfile);
                 rval->setAutoRestart(auto_restart);
                 rval->setSmoothRecovery(smooth_recovery);
index 350ae82f725ff50ba965eb1861e44029edde3f24..25b62948cf51b6cc4c70f54a5a61c64ec0a6d6e2 100644 (file)
@@ -157,53 +157,7 @@ bool ServiceRecord::do_auto_restart() noexcept
 
 void ServiceRecord::handle_exit_status(int exit_status) noexcept
 {
-    bool did_exit = WIFEXITED(exit_status);
-    bool was_signalled = WIFSIGNALED(exit_status);
-
-    if (service_type != ServiceType::SCRIPTED && exit_status != 0 && service_state != ServiceState::STOPPING) {
-        if (did_exit) {
-            log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", WEXITSTATUS(exit_status));
-        }
-        else if (was_signalled) {
-            log(LogLevel::ERROR, "Service ", service_name, " terminated due to signal ", WTERMSIG(exit_status));
-        }
-    }
-    
-    // BGPROCESS and PROCESS override this method; we must be a SCRIPTED service.
-    if (service_state == ServiceState::STOPPING) {
-        if (did_exit && WEXITSTATUS(exit_status) == 0) {
-            stopped();
-        }
-        else {
-            // ??? failed to stop! Let's log it as info:
-            if (did_exit) {
-                log(LogLevel::INFO, "Service ", service_name, " stop command failed with exit code ", WEXITSTATUS(exit_status));
-            }
-            else if (was_signalled) {
-                log(LogLevel::INFO, "Serivice ", service_name, " stop command terminated due to signal ", WTERMSIG(exit_status));
-            }
-            // Just assume that we stopped, so that any dependencies
-            // can be stopped:
-            stopped();
-        }
-        service_set->processQueues(false);
-    }
-    else { // STARTING
-        if (exit_status == 0) {
-            started();
-        }
-        else {
-            // failed to start
-            if (did_exit) {
-                log(LogLevel::ERROR, "Service ", service_name, " command failed with exit code ", WEXITSTATUS(exit_status));
-            }
-            else if (was_signalled) {
-                log(LogLevel::ERROR, "Service ", service_name, " command terminated due to signal ", WTERMSIG(exit_status));
-            }
-            failed_to_start();
-        }
-        service_set->processQueues(true);
-    }
+    // TODO make abstract
 }
 
 void process_service::handle_exit_status(int exit_status) noexcept
@@ -319,6 +273,47 @@ void bgproc_service::handle_exit_status(int exit_status) noexcept
     service_set->processQueues(false);
 }
 
+void scripted_service::handle_exit_status(int exit_status) noexcept
+{
+    bool did_exit = WIFEXITED(exit_status);
+    bool was_signalled = WIFSIGNALED(exit_status);
+
+    if (service_state == ServiceState::STOPPING) {
+        if (did_exit && WEXITSTATUS(exit_status) == 0) {
+            stopped();
+        }
+        else {
+            // ??? failed to stop! Let's log it as info:
+            if (did_exit) {
+                log(LogLevel::INFO, "Service ", service_name, " stop command failed with exit code ", WEXITSTATUS(exit_status));
+            }
+            else if (was_signalled) {
+                log(LogLevel::INFO, "Serivice ", service_name, " stop command terminated due to signal ", WTERMSIG(exit_status));
+            }
+            // Just assume that we stopped, so that any dependencies
+            // can be stopped:
+            stopped();
+        }
+        service_set->processQueues(false);
+    }
+    else { // STARTING
+        if (exit_status == 0) {
+            started();
+        }
+        else {
+            // failed to start
+            if (did_exit) {
+                log(LogLevel::ERROR, "Service ", service_name, " command failed with exit code ", WEXITSTATUS(exit_status));
+            }
+            else if (was_signalled) {
+                log(LogLevel::ERROR, "Service ", service_name, " command terminated due to signal ", WTERMSIG(exit_status));
+            }
+            failed_to_start();
+        }
+        service_set->processQueues(true);
+    }
+}
+
 rearm ServiceIoWatcher::fd_event(EventLoop_t &loop, int fd, int flags) noexcept
 {
     ServiceRecord *sr = service;
index 5e12bb64607759649c12a63f15265a21d5c14e21..eefd4c556f8c09873f8184b3ad925bd105e81f82 100644 (file)
@@ -596,6 +596,25 @@ class bgproc_service : public ServiceRecord
     }
 };
 
+class scripted_service : public ServiceRecord
+{
+    virtual void handle_exit_status(int exit_status) noexcept override;
+
+    public:
+    scripted_service(ServiceSet *sset, string name, string &&command,
+            std::list<std::pair<unsigned,unsigned>> &command_offsets,
+            sr_list * pdepends_on, sr_list * pdepends_soft)
+         : ServiceRecord(sset, name, ServiceType::SCRIPTED, std::move(command), command_offsets,
+             pdepends_on, pdepends_soft)
+    {
+    }
+
+    ~scripted_service() noexcept
+    {
+    }
+};
+
+
 /*
  * A ServiceSet, as the name suggests, manages a set of services.
  *