From 145e0da83fe1d7803737d6f134a5b4e401055c59 Mon Sep 17 00:00:00 2001 From: maroy Date: Thu, 25 Nov 2004 14:56:23 +0000 Subject: [PATCH] starting and stopping the scheduler daemon works now, like a charm see http://bugs.campware.org/view.php?id=469 --- .../products/scheduler/etc/Makefile.in | 3 +- .../scheduler/src/SchedulerDaemon.cxx | 16 ++++----- .../products/scheduler/src/SchedulerDaemon.h | 31 ++++++++-------- .../scheduler/src/SignalDispatcher.cxx | 7 +++- .../products/scheduler/src/XmlRpcDaemon.cxx | 18 ++++++++-- .../products/scheduler/src/XmlRpcDaemon.h | 27 +++++++++++--- livesupport/products/scheduler/src/main.cxx | 36 +++++++++++-------- 7 files changed, 91 insertions(+), 47 deletions(-) diff --git a/livesupport/products/scheduler/etc/Makefile.in b/livesupport/products/scheduler/etc/Makefile.in index a7b6613c3..2d6f3e71a 100644 --- a/livesupport/products/scheduler/etc/Makefile.in +++ b/livesupport/products/scheduler/etc/Makefile.in @@ -21,7 +21,7 @@ # # # Author : $Author: maroy $ -# Version : $Revision: 1.27 $ +# Version : $Revision: 1.28 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/etc/Makefile.in,v $ # # @configure_input@ @@ -265,6 +265,7 @@ start: ${SCHEDULER_EXE} stop: ${SCHEDULER_EXE} ${SCHEDULER_EXE} -c ${SCHEDULER_CFG} stop + sleep 2 status: ${SCHEDULER_EXE} ${SCHEDULER_EXE} -c ${SCHEDULER_CFG} status diff --git a/livesupport/products/scheduler/src/SchedulerDaemon.cxx b/livesupport/products/scheduler/src/SchedulerDaemon.cxx index 17c140828..731b288e9 100644 --- a/livesupport/products/scheduler/src/SchedulerDaemon.cxx +++ b/livesupport/products/scheduler/src/SchedulerDaemon.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.8 $ + Version : $Revision: 1.9 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/SchedulerDaemon.cxx,v $ ------------------------------------------------------------------------------*/ @@ -190,7 +190,7 @@ SchedulerDaemon :: configure(const xmlpp::Element & element) sf->getSchedule(), audioPlayer)); // TODO: read granularity from config file - granularity.reset(new time_duration(seconds(30))); + granularity.reset(new time_duration(seconds(1))); eventScheduler.reset( new LiveSupport::EventScheduler::EventScheduler(eventContainer, @@ -239,27 +239,27 @@ SchedulerDaemon :: uninstall(void) throw (std::exception) /*------------------------------------------------------------------------------ - * Start the scheduler daemon. + * Execute daemon startup functions. *----------------------------------------------------------------------------*/ void -SchedulerDaemon :: start(void) throw (std::logic_error) +SchedulerDaemon :: startup (void) throw () { - XmlRpcDaemon::start(); audioPlayer->initialize(); eventScheduler->start(); + XmlRpcDaemon::startup(); } /*------------------------------------------------------------------------------ - * Stop the scheduler daemon. + * Shut down the daemon *----------------------------------------------------------------------------*/ void -SchedulerDaemon :: stop(void) throw (std::logic_error) +SchedulerDaemon :: shutdown(void) throw (std::logic_error) { eventScheduler->stop(); audioPlayer->deInitialize(); - XmlRpcDaemon::stop(); + XmlRpcDaemon::shutdown(); } diff --git a/livesupport/products/scheduler/src/SchedulerDaemon.h b/livesupport/products/scheduler/src/SchedulerDaemon.h index 85a30247b..b28cfaa86 100644 --- a/livesupport/products/scheduler/src/SchedulerDaemon.h +++ b/livesupport/products/scheduler/src/SchedulerDaemon.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.7 $ + Version : $Revision: 1.8 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/SchedulerDaemon.h,v $ ------------------------------------------------------------------------------*/ @@ -124,7 +124,7 @@ using namespace LiveSupport::PlaylistExecutor; * * * @author $Author: maroy $ - * @version $Revision: 1.7 $ + * @version $Revision: 1.8 $ * @see ConnectionManagerFactory * @see StorageClientFactory * @see ScheduleFactory @@ -191,6 +191,16 @@ class SchedulerDaemon : public Installable, registerXmlRpcFunctions(Ptr::Ref xmlRpcServer) throw (std::logic_error); + /** + * Execute any calls when the daemon is starting up. + * All resources allocated here should be freed up in shutdown(). + * + * @see #shutdown + */ + virtual void + startup (void) throw (); + + public: /** @@ -246,23 +256,16 @@ class SchedulerDaemon : public Installable, uninstall(void) throw (std::exception); /** - * Start the daemon. + * Shut down the daemon. + * This function is public only because the signal handler + * needs visibility to this function, which will call it. + * A call to stop() will trigger a signal that will call shutdown(). * * @exception std::logic_error if the daemon has not * yet been configured. */ virtual void - start (void) throw (std::logic_error); - - /** - * Stop the daemon. - * - * @exception std::logic_error if the daemon has not - * yet been configured. - */ - virtual void - stop (void) throw (std::logic_error); - + shutdown (void) throw (std::logic_error); }; diff --git a/livesupport/products/scheduler/src/SignalDispatcher.cxx b/livesupport/products/scheduler/src/SignalDispatcher.cxx index 976b8fa1a..f83b89a26 100644 --- a/livesupport/products/scheduler/src/SignalDispatcher.cxx +++ b/livesupport/products/scheduler/src/SignalDispatcher.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/SignalDispatcher.cxx,v $ ------------------------------------------------------------------------------*/ @@ -131,12 +131,17 @@ SignalDispatcher :: removeHandler( ::signal(signal, SIG_DFL); } + +#include + /*------------------------------------------------------------------------------ * Our signal dispatcher *----------------------------------------------------------------------------*/ void SignalDispatcher :: dispatcher(int signal) throw () { +std::cerr << "dispatcher called with signal " << signal << std::endl; +std::cerr << "handler for this signal: " << handlers[signal] << std::endl; handlers[signal]->handleSignal(signal); } diff --git a/livesupport/products/scheduler/src/XmlRpcDaemon.cxx b/livesupport/products/scheduler/src/XmlRpcDaemon.cxx index cfeaa0894..5fff310d5 100644 --- a/livesupport/products/scheduler/src/XmlRpcDaemon.cxx +++ b/livesupport/products/scheduler/src/XmlRpcDaemon.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.2 $ + Version : $Revision: 1.3 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/XmlRpcDaemon.cxx,v $ ------------------------------------------------------------------------------*/ @@ -227,7 +227,7 @@ XmlRpcDaemon :: daemonize(void) throw (std::runtime_error) signalDispatcher->registerHandler(SIGHUP, handler); signalDispatcher->registerHandler(SIGTERM, handler); // FIXME: this signal handler will not be deleted by anyone, - // poddible memory leak + // possible memory leak return true; } @@ -281,6 +281,16 @@ XmlRpcDaemon :: start (void) throw (std::logic_error) } } + startup(); +} + + +/*------------------------------------------------------------------------------ + * Execute any daemon startup calls. + *----------------------------------------------------------------------------*/ +void +XmlRpcDaemon :: startup (void) throw () +{ // and now our own XML-RPC methods registerXmlRpcFunctions(xmlRpcServer); @@ -322,7 +332,9 @@ XmlRpcDaemon :: stop (void) throw (std::logic_error) checkForConfiguration(); pid_t pid = loadPid(); - kill(pid, SIGTERM); + if (pid) { + kill(pid, SIGTERM); + } } diff --git a/livesupport/products/scheduler/src/XmlRpcDaemon.h b/livesupport/products/scheduler/src/XmlRpcDaemon.h index eb3899dbb..b3f429612 100644 --- a/livesupport/products/scheduler/src/XmlRpcDaemon.h +++ b/livesupport/products/scheduler/src/XmlRpcDaemon.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.3 $ + Version : $Revision: 1.4 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/XmlRpcDaemon.h,v $ ------------------------------------------------------------------------------*/ @@ -119,7 +119,7 @@ using namespace LiveSupport::Core; * * * @author $Author: maroy $ - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ class XmlRpcDaemon { @@ -248,6 +248,18 @@ class XmlRpcDaemon throw (std::logic_error) = 0; + /** + * Execute any calls when the daemon is starting up. + * All resources allocated here should be freed up in shutdown(). + * This function will only return when the daemon ha stopped + * running. + * + * @see #shutdown + */ + virtual void + startup (void) throw (); + + public: /** * Return the name of the XML element this object expects @@ -371,7 +383,7 @@ class XmlRpcDaemon * @exception std::logic_error if the daemon has not * yet been configured. */ - virtual void + void start (void) throw (std::logic_error); /** @@ -388,22 +400,27 @@ class XmlRpcDaemon /** * Stop the daemon. + * This function just sends a signal, which will result in + * shutdown() to get called eventually. * * @exception std::logic_error if the daemon has not * yet been configured. + * @see #shutdown */ - virtual void + void stop (void) throw (std::logic_error); /** * Shut down the daemon. * This function is public only because the signal handler * needs visibility to this function, which will call it. + * A call to stop() will trigger a signal that will call shutdown(). * * @exception std::logic_error if the daemon has not * yet been configured. + * @see #stop */ - void + virtual void shutdown (void) throw (std::logic_error); }; diff --git a/livesupport/products/scheduler/src/main.cxx b/livesupport/products/scheduler/src/main.cxx index 9d5c434f1..60f7bd7c6 100644 --- a/livesupport/products/scheduler/src/main.cxx +++ b/livesupport/products/scheduler/src/main.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.3 $ + Version : $Revision: 1.4 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/main.cxx,v $ ------------------------------------------------------------------------------*/ @@ -199,20 +199,26 @@ int main ( int argc, daemon->setBackground(!debugMode); - if (installCommand == argv[optind]) { - daemon->install(); - } else if (startCommand == argv[optind]) { - daemon->start(); - } else if (statusCommand == argv[optind]) { - std::cout << "The Scheduler Daemon is " - << (daemon->isRunning() ? "" : "not ") - << "running" << std::endl; - } else if (stopCommand == argv[optind]) { - daemon->stop(); - } else if (uninstallCommand == argv[optind]) { - daemon->uninstall(); - } else { - printUsage(argv[0], std::cout); + try { + if (installCommand == argv[optind]) { + daemon->install(); + } else if (startCommand == argv[optind]) { + daemon->start(); + } else if (statusCommand == argv[optind]) { + std::cout << "The Scheduler Daemon is " + << (daemon->isRunning() ? "" : "not ") + << "running" << std::endl; + } else if (stopCommand == argv[optind]) { + daemon->stop(); + } else if (uninstallCommand == argv[optind]) { + daemon->uninstall(); + } else { + printUsage(argv[0], std::cout); + exit(EXIT_FAILURE); + } + } catch (std::exception &e) { + std::cerr << "error executing command " << argv[optind] << std::endl; + std::cerr << e.what() << std::endl; exit(EXIT_FAILURE); }