From 5b3291e6e6e2e2b6fd27e83368fba6a26bfed7a6 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Wed, 27 Sep 2017 16:44:10 +0100 Subject: [PATCH] Re-work dependency specification for service_record constructors. Dependencies of different types are now given together in a single list, wth each dependency tagged by its type, rather than as two lists (one for regular and one for soft dependencies). This opens up the possibility of supporting additional dependency types. --- src/load_service.cc | 15 +++++----- src/service.cc | 4 +-- src/service.h | 61 ++++++++++++++++++++-------------------- src/tests/test_service.h | 6 ++-- src/tests/tests.cc | 47 ++++++++++++++++--------------- 5 files changed, 68 insertions(+), 65 deletions(-) diff --git a/src/load_service.cc b/src/load_service.cc index 5c8a200..0110476 100644 --- a/src/load_service.cc +++ b/src/load_service.cc @@ -385,8 +385,7 @@ service_record * dirload_service_set::load_service(const char * name) string pid_file; service_type service_type = service_type::PROCESS; - std::list depends_on; - std::list depends_soft; + std::list depends; string logfile; onstart_flags_t onstart_flags; int term_signal = -1; // additional termination signal @@ -475,11 +474,11 @@ service_record * dirload_service_set::load_service(const char * name) } else if (setting == "depends-on") { string dependency_name = read_setting_value(i, end); - depends_on.push_back(load_service(dependency_name.c_str())); + depends.emplace_back(load_service(dependency_name.c_str()), dependency_type::REGULAR); } else if (setting == "waits-for") { string dependency_name = read_setting_value(i, end); - depends_soft.push_back(load_service(dependency_name.c_str())); + depends.emplace_back(load_service(dependency_name.c_str()), dependency_type::SOFT); } else if (setting == "logfile") { logfile = read_setting_value(i, end); @@ -588,7 +587,7 @@ service_record * dirload_service_set::load_service(const char * name) delete rval; if (service_type == service_type::PROCESS) { auto rvalps = new process_service(this, string(name), std::move(command), - command_offsets, std::move(depends_on), depends_soft); + command_offsets, depends); rvalps->set_restart_interval(restart_interval, max_restarts); rvalps->set_restart_delay(restart_delay); rvalps->set_stop_timeout(stop_timeout); @@ -596,7 +595,7 @@ service_record * dirload_service_set::load_service(const char * name) } else if (service_type == service_type::BGPROCESS) { auto rvalps = new bgproc_service(this, string(name), std::move(command), - command_offsets, std::move(depends_on), depends_soft); + command_offsets, depends); rvalps->set_pid_file(std::move(pid_file)); rvalps->set_restart_interval(restart_interval, max_restarts); rvalps->set_restart_delay(restart_delay); @@ -605,7 +604,7 @@ service_record * dirload_service_set::load_service(const char * name) } else if (service_type == service_type::SCRIPTED) { auto rvalps = new scripted_service(this, string(name), std::move(command), - command_offsets, std::move(depends_on), depends_soft); + command_offsets, depends); rvalps->set_stop_command(stop_command, stop_command_offsets); rvalps->set_stop_timeout(stop_timeout); rval = rvalps; @@ -613,7 +612,7 @@ service_record * dirload_service_set::load_service(const char * name) else { rval = new service_record(this, string(name), service_type, std::move(command), command_offsets, - std::move(depends_on), depends_soft); + depends); } rval->set_log_file(logfile); rval->set_auto_restart(auto_restart); diff --git a/src/service.cc b/src/service.cc index e2ba251..3b0c4ef 100644 --- a/src/service.cc +++ b/src/service.cc @@ -1326,9 +1326,9 @@ void service_set::service_inactive(service_record *sr) noexcept base_process_service::base_process_service(service_set *sset, string name, service_type service_type_p, string &&command, std::list> &command_offsets, - sr_list &&pdepends_on, const sr_list &pdepends_soft) + const std::list &deplist_p) : service_record(sset, name, service_type_p, std::move(command), command_offsets, - std::move(pdepends_on), pdepends_soft), child_listener(this), child_status_listener(this) + deplist_p), child_listener(this), child_status_listener(this) { restart_interval_count = 0; restart_interval_time = {0, 0}; diff --git a/src/service.h b/src/service.h index 2040fe5..b9c8471 100644 --- a/src/service.h +++ b/src/service.h @@ -200,6 +200,19 @@ class service_dep } }; +/* preliminary service dependency information */ +class prelim_dep +{ + public: + service_record * const to; + dependency_type const dep_type; + + prelim_dep(service_record *to_p, dependency_type dep_type_p) : to(to_p), dep_type(dep_type_p) + { + // + } +}; + // Given a string and a list of pairs of (start,end) indices for each argument in that string, // store a null terminator for the argument. Return a `char *` vector containing the beginning // of each argument and a trailing nullptr. (The returned array is invalidated if the string is later modified). @@ -243,6 +256,8 @@ class exec_status_pipe_watcher : public eventloop_t::fd_watcher_impl dpt_list; - //sr_list depends_on; // services this one depends on - //sr_list dependents; // services depending on this one - dep_list depends_on; // services this one depends on via a soft dependency - dpt_list dependents; // services depending on this one via a soft dependency - - // unsigned wait_count; /* if we are waiting for dependents/dependencies to - // start/stop, this is how many we're waiting for */ + dep_list depends_on; // services this one depends on + dpt_list dependents; // services depending on this one service_set *services; // the set this service belongs to @@ -325,7 +335,6 @@ class service_record int socket_fd = -1; // For socket-activation services, this is the file // descriptor for the socket. - // Data for use by service_set public: @@ -444,31 +453,23 @@ class service_record } service_record(service_set *set, string name, service_type record_type_p, - sr_list &&pdepends_on, const sr_list &pdepends_soft) + const std::list &deplist_p) : service_record(set, name) { services = set; service_name = name; this->record_type = record_type_p; - for (auto pdep : pdepends_on) { - auto b = depends_on.emplace(depends_on.end(), this, pdep, dependency_type::REGULAR); - pdep->dependents.push_back(&(*b)); - } - - // Soft dependencies - auto b_iter = depends_on.end(); - for (auto i = pdepends_soft.begin(); i != pdepends_soft.end(); ++i) { - b_iter = depends_on.emplace(b_iter, this, *i, dependency_type::SOFT); - (*i)->dependents.push_back(&(*b_iter)); - ++b_iter; + for (auto & pdep : deplist_p) { + auto b = depends_on.emplace(depends_on.end(), this, pdep.to, pdep.dep_type); + pdep.to->dependents.push_back(&(*b)); } } service_record(service_set *set, string name, service_type record_type_p, string &&command, std::list> &command_offsets, - sr_list &&pdepends_on, const sr_list &pdepends_soft) - : service_record(set, name, record_type_p, std::move(pdepends_on), pdepends_soft) + const std::list &deplist_p) + : service_record(set, name, record_type_p, deplist_p) { program_name = std::move(command); exec_arg_parts = separate_args(program_name, command_offsets); @@ -669,7 +670,7 @@ class base_process_service : public service_record public: base_process_service(service_set *sset, string name, service_type record_type_p, string &&command, std::list> &command_offsets, - sr_list &&pdepends_on, const sr_list &pdepends_soft); + const std::list &deplist_p); ~base_process_service() noexcept { @@ -701,9 +702,9 @@ class process_service : public base_process_service public: process_service(service_set *sset, string name, string &&command, std::list> &command_offsets, - sr_list &&pdepends_on, const sr_list &pdepends_soft) + std::list depends_p) : base_process_service(sset, name, service_type::PROCESS, std::move(command), command_offsets, - std::move(pdepends_on), pdepends_soft) + depends_p) { } @@ -728,9 +729,9 @@ class bgproc_service : public base_process_service public: bgproc_service(service_set *sset, string name, string &&command, std::list> &command_offsets, - sr_list &&pdepends_on, const sr_list &pdepends_soft) + std::list depends_p) : base_process_service(sset, name, service_type::BGPROCESS, std::move(command), command_offsets, - std::move(pdepends_on), pdepends_soft) + depends_p) { tracking_child = false; reserved_child_watch = false; @@ -749,9 +750,9 @@ class scripted_service : public base_process_service public: scripted_service(service_set *sset, string name, string &&command, std::list> &command_offsets, - sr_list &&pdepends_on, const sr_list &pdepends_soft) + std::list depends_p) : base_process_service(sset, name, service_type::SCRIPTED, std::move(command), command_offsets, - std::move(pdepends_on), pdepends_soft) + depends_p) { } diff --git a/src/tests/test_service.h b/src/tests/test_service.h index be05db9..48557fe 100644 --- a/src/tests/test_service.h +++ b/src/tests/test_service.h @@ -3,9 +3,9 @@ class test_service : public service_record { public: - test_service(service_set *set, std::string name, service_type type_p, sr_list &&pdepends_on, - const sr_list &pdepends_soft) - : service_record(set, name, type_p, std::move(pdepends_on), pdepends_soft) + test_service(service_set *set, std::string name, service_type type_p, + const std::list &deplist_p) + : service_record(set, name, type_p, deplist_p) { } diff --git a/src/tests/tests.cc b/src/tests/tests.cc index 26f5613..a759bc9 100644 --- a/src/tests/tests.cc +++ b/src/tests/tests.cc @@ -4,15 +4,18 @@ #include "service.h" #include "test_service.h" +constexpr static auto REG = dependency_type::REGULAR; +constexpr static auto SFT = dependency_type::SOFT; + // Test 1: starting a service starts dependencies; stopping the service releases and // stops dependencies. void test1() { service_set sset; - service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {s2}, {}); + service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}); + service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {{s2, REG}}); sset.add_service(s1); sset.add_service(s2); sset.add_service(s3); @@ -42,10 +45,10 @@ void test2() { service_set sset; - service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {s2}, {}); - service_record *s4 = new service_record(&sset, "test-service-4", service_type::INTERNAL, {s2}, {}); + service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}); + service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {{s2, REG}}); + service_record *s4 = new service_record(&sset, "test-service-4", service_type::INTERNAL, {{s2, REG}}); sset.add_service(s1); sset.add_service(s2); sset.add_service(s3); @@ -83,9 +86,9 @@ void test3() { service_set sset; - service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {s2}, {}); + service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}); + service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {{s2, REG}}); sset.add_service(s1); sset.add_service(s2); sset.add_service(s3); @@ -111,9 +114,9 @@ void test4() { service_set sset; - service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {s2}, {}); + service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}); + service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {{s2, REG}}); s2->set_auto_restart(true); sset.add_service(s1); sset.add_service(s2); @@ -144,9 +147,9 @@ void test5() { service_set sset; - test_service *s1 = new test_service(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - test_service *s2 = new test_service(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - test_service *s3 = new test_service(&sset, "test-service-3", service_type::INTERNAL, {s2}, {}); + test_service *s1 = new test_service(&sset, "test-service-1", service_type::INTERNAL, {}); + test_service *s2 = new test_service(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + test_service *s3 = new test_service(&sset, "test-service-3", service_type::INTERNAL, {{s2, REG}}); sset.add_service(s1); sset.add_service(s2); @@ -183,9 +186,9 @@ void test6() { service_set sset; - service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {s2}, {}); + service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}); + service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {{s2, REG}}); s2->set_auto_restart(true); sset.add_service(s1); sset.add_service(s2); @@ -225,9 +228,9 @@ void test7() { service_set sset; - service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}, {}); - service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {s1}, {}); - service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {}, {s2}); + service_record *s1 = new service_record(&sset, "test-service-1", service_type::INTERNAL, {}); + service_record *s2 = new service_record(&sset, "test-service-2", service_type::INTERNAL, {{s1, REG}}); + service_record *s3 = new service_record(&sset, "test-service-3", service_type::INTERNAL, {{s2, SFT}}); sset.add_service(s1); sset.add_service(s2); sset.add_service(s3); -- 2.25.1