From ed694436a41255f8c22942c2b5b216a09a6f440a Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Fri, 26 May 2017 10:29:13 +0100 Subject: [PATCH] Break out scripted service as a separate class --- src/load_service.cc | 6 ++- src/service.cc | 89 +++++++++++++++++++++------------------------ src/service.h | 19 ++++++++++ 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/load_service.cc b/src/load_service.cc index cdd5b8a..7cf862e 100644 --- a/src/load_service.cc +++ b/src/load_service.cc @@ -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); diff --git a/src/service.cc b/src/service.cc index 350ae82..25b6294 100644 --- a/src/service.cc +++ b/src/service.cc @@ -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; diff --git a/src/service.h b/src/service.h index 5e12bb6..eefd4c5 100644 --- a/src/service.h +++ b/src/service.h @@ -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> &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. * -- 2.25.1