History

have AssignmentClientMonitor catch SIGTERM and cleanup

Stephen Birarda committed 11 years ago

Commit dac980d1 parent 3a8e89f2 files · +43 −0Browse files

Showing the changes to assignment-client/src/AssignmentClientMonitor.cpp (1 of 2 files). Show all

Unified · Split

assignment-client/src/AssignmentClientMonitor.cpp+39−0
@@ -9,6 +9,8 @@ · 8 unmodified lines
9// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html9// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10//10//
11 11
12#include <signal.h>
13
12#include <LogHandler.h>14#include <LogHandler.h>
13 15
14#include "AssignmentClientMonitor.h"16#include "AssignmentClientMonitor.h"
@@ -17,9 +19,21 @@ const char* NUM_FORKS_PARAMETER = "-n"; · 2 unmodified lines
17 19
18const QString ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME = "assignment-client-monitor";20const QString ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME = "assignment-client-monitor";
19 21
22void signalHandler(int param){
23 // get the qApp and cast it to an AssignmentClientMonitor
24 AssignmentClientMonitor* app = qobject_cast<AssignmentClientMonitor*>(qApp);
25
26 // tell it to stop the child processes and then go down
27 app->stopChildProcesses();
28 app->quit();
29}
30
20AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int numAssignmentClientForks) :31AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int numAssignmentClientForks) :
21 QCoreApplication(argc, argv)32 QCoreApplication(argc, argv)
22{33{
34 // be a signal handler for SIGTERM so we can stop our children when we get it
35 signal(SIGTERM, signalHandler);
36
23 // start the Logging class with the parent's target name37 // start the Logging class with the parent's target name
24 LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME);38 LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME);
25 39
@@ -38,9 +52,29 @@ AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int num · 12 unmodified lines
38 }52 }
39}53}
40 54
55void AssignmentClientMonitor::stopChildProcesses() {
56
57 QList<QPointer<QProcess> >::Iterator it = _childProcesses.begin();
58 while (it != _childProcesses.end()) {
59 if (!it->isNull()) {
60 qDebug() << "Monitor is terminating child process" << it->data();
61
62 // don't re-spawn this child when it goes down
63 disconnect(it->data(), 0, this, 0);
64
65 it->data()->terminate();
66 it->data()->waitForFinished();
67 }
68
69 it = _childProcesses.erase(it);
70 }
71}
72
41void AssignmentClientMonitor::spawnChildClient() {73void AssignmentClientMonitor::spawnChildClient() {
42 QProcess *assignmentClient = new QProcess(this);74 QProcess *assignmentClient = new QProcess(this);
43 75
76 _childProcesses.append(QPointer<QProcess>(assignmentClient));
77
44 // make sure that the output from the child process appears in our output78 // make sure that the output from the child process appears in our output
45 assignmentClient->setProcessChannelMode(QProcess::ForwardedChannels);79 assignmentClient->setProcessChannelMode(QProcess::ForwardedChannels);
46 80
@@ -55,5 +89,10 @@ void AssignmentClientMonitor::spawnChildClient() { · 8 unmodified lines
55 89
56void AssignmentClientMonitor::childProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {90void AssignmentClientMonitor::childProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
57 qDebug("Replacing dead child assignment client with a new one");91 qDebug("Replacing dead child assignment client with a new one");
92
93 // remove the old process from our list of child processes
94 qDebug() << "need to remove" << QPointer<QProcess>(qobject_cast<QProcess*>(sender()));
95 _childProcesses.removeOne(QPointer<QProcess>(qobject_cast<QProcess*>(sender())));
96
58 spawnChildClient();97 spawnChildClient();
59}98}