From 9838c2eadf06b6bfe3d6eb264c1b485b51ba1a06 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 2 Jan 2014 15:16:21 +0100 Subject: [PATCH] Filter destructors called upon daemon termination MP-505 When metaproxy daemon receives SIGTERM the stop method of each filter is called. The stop method existed before, but did not take any arguements (void).. The frontend_net filter can now distinguish between SIGUSR1 (stop bind, but keep sessions running) and SIGTERM (stop immediately). Note that this changes the API+ABI of Metaproxy filters. --- include/metaproxy/filter.hpp | 2 +- include/metaproxy/router.hpp | 2 +- include/metaproxy/router_chain.hpp | 2 +- include/metaproxy/router_xml.hpp | 2 +- src/filter.cpp | 2 +- src/filter_frontend_net.cpp | 17 ++++++++++------- src/filter_frontend_net.hpp | 2 +- src/metaproxy_prog.cpp | 20 ++++++++++++-------- src/router_chain.cpp | 4 ++-- src/router_flexml.cpp | 4 ++-- src/router_flexml.hpp | 2 +- src/router_xml.cpp | 4 ++-- 12 files changed, 35 insertions(+), 28 deletions(-) diff --git a/include/metaproxy/filter.hpp b/include/metaproxy/filter.hpp index 4887f65..23e38bd 100644 --- a/include/metaproxy/filter.hpp +++ b/include/metaproxy/filter.hpp @@ -42,7 +42,7 @@ namespace metaproxy_1 { virtual void start() const; - virtual void stop() const; + virtual void stop(int signo) const; }; class FilterException : public std::runtime_error { diff --git a/include/metaproxy/router.hpp b/include/metaproxy/router.hpp index 3031527..f540e5a 100644 --- a/include/metaproxy/router.hpp +++ b/include/metaproxy/router.hpp @@ -43,7 +43,7 @@ namespace metaproxy_1 virtual RoutePos *createpos() const = 0; virtual void start() = 0; - virtual void stop() = 0; + virtual void stop(int signo) = 0; }; class RoutePos : boost::noncopyable { diff --git a/include/metaproxy/router_chain.hpp b/include/metaproxy/router_chain.hpp index 2d83729..4639803 100644 --- a/include/metaproxy/router_chain.hpp +++ b/include/metaproxy/router_chain.hpp @@ -35,7 +35,7 @@ namespace metaproxy_1 { virtual RoutePos *createpos() const; RouterChain & append(const filter::Base &filter); void start(); - void stop(); + void stop(int signo); private: boost::scoped_ptr m_p; /// disabled because class is singleton diff --git a/include/metaproxy/router_xml.hpp b/include/metaproxy/router_xml.hpp index 2792ba7..e85632f 100644 --- a/include/metaproxy/router_xml.hpp +++ b/include/metaproxy/router_xml.hpp @@ -41,7 +41,7 @@ namespace metaproxy_1 virtual RoutePos *createpos() const; void start(); - void stop(); + void stop(int signo); private: boost::scoped_ptr m_p; }; diff --git a/src/filter.cpp b/src/filter.cpp index 3bc27a4..37a4698 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -27,7 +27,7 @@ void mp::filter::Base::start() const { } -void mp::filter::Base::stop() const +void mp::filter::Base::stop(int signo) const { } diff --git a/src/filter_frontend_net.cpp b/src/filter_frontend_net.cpp index ae2e9c6..ec0e78e 100644 --- a/src/filter_frontend_net.cpp +++ b/src/filter_frontend_net.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include "gduutil.hpp" +#include #include @@ -67,7 +68,7 @@ namespace metaproxy_1 { double m_duration_max; double m_duration_min; double m_duration_total; - bool m_stop; + int m_stop_signo; public: Rep(); ~Rep(); @@ -527,7 +528,7 @@ yf::FrontendNet::Rep::Rep() m_duration_max = 0.0; m_duration_min = 0.0; m_duration_total = 0.0; - m_stop = false; + m_stop_signo = 0; } yf::FrontendNet::Rep::~Rep() @@ -546,9 +547,9 @@ yf::FrontendNet::~FrontendNet() { } -void yf::FrontendNet::stop() const +void yf::FrontendNet::stop(int signo) const { - m_p->m_stop = true; + m_p->m_stop_signo = signo; } bool yf::FrontendNet::My_Timer_Thread::timeout() @@ -592,9 +593,11 @@ void yf::FrontendNet::process(mp::Package &package) const } while (m_p->mySocketManager.processEvent() > 0) { - if (m_p->m_stop) - { - m_p->m_stop = false; + if (m_p->m_stop_signo == SIGTERM) + break; /* stop right away */ + if (m_p->m_stop_signo == SIGUSR1) + { /* just stop listeners and cont till all sessions are done*/ + m_p->m_stop_signo = 0; if (m_p->az) { size_t i; diff --git a/src/filter_frontend_net.hpp b/src/filter_frontend_net.hpp index cfeed64..3ec1ad4 100644 --- a/src/filter_frontend_net.hpp +++ b/src/filter_frontend_net.hpp @@ -43,7 +43,7 @@ namespace metaproxy_1 { void process(metaproxy_1::Package & package) const; void configure(const xmlNode * ptr, bool test_only, const char *path); - void stop() const; + void stop(int signo) const; public: /// set ports void set_ports(std::vector &ports); diff --git a/src/metaproxy_prog.cpp b/src/metaproxy_prog.cpp index a4ec7f0..083ea29 100644 --- a/src/metaproxy_prog.cpp +++ b/src/metaproxy_prog.cpp @@ -59,18 +59,20 @@ static void set_log_prefix(void) #if HAVE_UNISTD_H static pid_t process_group = 0; -static void sig_usr1_handler(int s) +static void sig_usr1_handler(int signo) { yaz_log(YLOG_LOG, "metaproxy received SIGUSR1"); - routerp->stop(); + if (routerp) + routerp->stop(signo); } -static void sig_term_handler(int s) +static void sig_term_handler(int signo) { - yaz_log(YLOG_LOG, "metaproxy received SIGTERM"); - yaz_log(YLOG_LOG, "metaproxy stop"); - kill(-process_group, SIGTERM); /* kill all children processes as well */ - _exit(0); + if (routerp) + { + yaz_log(YLOG_LOG, "metaproxy received SIGTERM"); + routerp->stop(signo); + } } #endif @@ -88,7 +90,9 @@ static void work_common(void *data) mp::Package pack; pack.router(*routerp).move(); - yaz_log(YLOG_LOG, "metaproxy stop"); /* only for graceful stop */ + yaz_log(YLOG_LOG, "metaproxy stop"); + delete routerp; + routerp = 0; #if HAVE_UNISTD_H kill(-process_group, SIGTERM); /* kill all children processes as well */ #endif diff --git a/src/router_chain.cpp b/src/router_chain.cpp index dd80405..04bef61 100644 --- a/src/router_chain.cpp +++ b/src/router_chain.cpp @@ -58,12 +58,12 @@ void mp::RouterChain::start() (*it)->start(); } -void mp::RouterChain::stop() +void mp::RouterChain::stop(int signo) { std::list::const_iterator it; for (it = m_p->m_filter_list.begin(); it != m_p->m_filter_list.end(); it++) - (*it)->stop(); + (*it)->stop(signo); } const mp::filter::Base *mp::RouterChain::Pos::move(const char *route) diff --git a/src/router_flexml.cpp b/src/router_flexml.cpp index 6f15edc..8d6e2bb 100644 --- a/src/router_flexml.cpp +++ b/src/router_flexml.cpp @@ -504,7 +504,7 @@ void mp::RouterFleXML::start() } } -void mp::RouterFleXML::stop() +void mp::RouterFleXML::stop(int signo) { std::map::iterator route_it; @@ -516,7 +516,7 @@ void mp::RouterFleXML::stop() std::list >::iterator it; for (it = route.m_list.begin(); it != route.m_list.end(); it++) - (*it)->stop(); + (*it)->stop(signo); route_it++; } } diff --git a/src/router_flexml.hpp b/src/router_flexml.hpp index 07f3267..6fca612 100644 --- a/src/router_flexml.hpp +++ b/src/router_flexml.hpp @@ -44,7 +44,7 @@ namespace metaproxy_1 virtual RoutePos *createpos() const; void start(); - void stop(); + void stop(int signo); private: boost::scoped_ptr m_p; }; diff --git a/src/router_xml.cpp b/src/router_xml.cpp index 9abba65..2aac23d 100644 --- a/src/router_xml.cpp +++ b/src/router_xml.cpp @@ -76,9 +76,9 @@ void mp::RouterXML::start() m_p->m_flexml->start(); } -void mp::RouterXML::stop() +void mp::RouterXML::stop(int signo) { - m_p->m_flexml->stop(); + m_p->m_flexml->stop(signo); } /* -- 1.7.10.4