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
99 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1010 //
1111
12+#include <signal.h>
13+
1214 #include <LogHandler.h>
1315
1416 #include "AssignmentClientMonitor.h"
@@ -17,9 +19,21 @@ const char* NUM_FORKS_PARAMETER = "-n"; · 2 unmodified lines
1719
1820 const QString ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME = "assignment-client-monitor";
1921
22+void 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+
2031 AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int numAssignmentClientForks) :
2132 QCoreApplication(argc, argv)
2233 {
34+ // be a signal handler for SIGTERM so we can stop our children when we get it
35+ signal(SIGTERM, signalHandler);
36+
2337 // start the Logging class with the parent's target name
2438 LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME);
2539
@@ -38,9 +52,29 @@ AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int num · 12 unmodified lines
3852 }
3953 }
4054
55+void 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+
4173 void AssignmentClientMonitor::spawnChildClient() {
4274 QProcess *assignmentClient = new QProcess(this);
4375
76+ _childProcesses.append(QPointer<QProcess>(assignmentClient));
77+
4478 // make sure that the output from the child process appears in our output
4579 assignmentClient->setProcessChannelMode(QProcess::ForwardedChannels);
4680
@@ -55,5 +89,10 @@ void AssignmentClientMonitor::spawnChildClient() { · 8 unmodified lines
5589
5690 void AssignmentClientMonitor::childProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
5791 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+
5897 spawnChildClient();
5998 }