Commit 834f82e0 authored by Steven Murray's avatar Steven Murray
Browse files

Improved the clarity of the main event loop of the ProcessForker

parent 9f8b765d
......@@ -94,22 +94,44 @@ void castor::tape::tapeserver::daemon::ProcessForker::closeCmdReceiverSocket()
//------------------------------------------------------------------------------
// execute
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::ProcessForker::execute() {
try {
while(handleEvents()) {
reapZombies();
}
} catch(castor::exception::Exception &ne) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to handle events: " << ne.getMessage().str();
throw ex;
void castor::tape::tapeserver::daemon::ProcessForker::execute() throw() {
// The main event loop
while(handleEvents()) {
}
}
//------------------------------------------------------------------------------
// handleEvents
//------------------------------------------------------------------------------
bool castor::tape::tapeserver::daemon::ProcessForker::handleEvents() {
bool castor::tape::tapeserver::daemon::ProcessForker::handleEvents() throw() {
try {
return handlePendingMsgs() && handlePendingSignals();
} catch(castor::exception::Exception &ex) {
log::Param params[] = {log::Param("message", ex.getMessage().str())};
m_log(LOG_ERR, "ProcessForker failed to handle events", params);
// An exception should not stop the main event loop
return true; // The main event loop should continue
} catch(std::exception &se) {
log::Param params[] = {log::Param("message", se.what())};
m_log(LOG_ERR, "ProcessForker failed to handle events", params);
// An exception should not stop the main event loop
return true; // The main event loop should continue
} catch(...) {
log::Param params[] =
{log::Param("message", "Caught an unknown exception")};
m_log(LOG_ERR, "ProcessForker failed to handle events", params);
// An exception should not stop the main event loop
return true; // The main event loop should continue
}
}
//------------------------------------------------------------------------------
// handlePendingMsgs
//------------------------------------------------------------------------------
bool castor::tape::tapeserver::daemon::ProcessForker::handlePendingMsgs() {
if(thereIsAPendingMsg()) {
return handleMsg();
} else {
......@@ -624,10 +646,28 @@ void *castor::tape::tapeserver::daemon::ProcessForker::instantiateZmqContext(
return zmqContext;
}
//------------------------------------------------------------------------------
// handlePendingSignals
//------------------------------------------------------------------------------
bool castor::tape::tapeserver::daemon::ProcessForker::handlePendingSignals() {
try {
reapZombies();
// For now there are not signals that correspond to gracefully shutting the
// ProcessForker process
return true; // The main event loop should continue
} catch(castor::exception::Exception &ne) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to handle pending signals: " <<
ne.getMessage().str();
throw ex;
}
}
//------------------------------------------------------------------------------
// reapZombies
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::ProcessForker::reapZombies() throw() {
void castor::tape::tapeserver::daemon::ProcessForker::reapZombies() {
pid_t pid = 0;
int waitpidStat = 0;
while (0 < (pid = waitpid(-1, &waitpidStat, WNOHANG))) {
......@@ -639,20 +679,25 @@ void castor::tape::tapeserver::daemon::ProcessForker::reapZombies() throw() {
// handleReapedZombie
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::ProcessForker::handleReapedZombie(
const pid_t pid, const int waitpidStat) throw() {
const pid_t pid, const int waitpidStat) {
try {
logChildProcessTerminated(pid, waitpidStat);
notifyTapeDaemonOfTerminatedProcess(pid, waitpidStat);
} catch(castor::exception::Exception &ex) {
log::Param params[] = {log::Param("message", ex.getMessage().str())};
m_log(LOG_ERR, "Failed to handle reaped zombie", params);
} catch(std::exception &se) {
log::Param params[] = {log::Param("message", se.what())};
m_log(LOG_ERR, "Failed to handle reaped zombie", params);
} catch(castor::exception::Exception &ne) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to handle reaped zombie: pid=" << pid <<
ne.getMessage().str();
throw ex;
} catch(std::exception &ne) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to handle reaped zombie: pid=" << pid <<
ne.what();
throw ex;
} catch(...) {
log::Param params[] = {
log::Param("message", "Caught an unknown exception")};
m_log(LOG_ERR, "Failed to handle reaped zombie", params);
castor::exception::Exception ex;
ex.getMessage() << "Failed to handle reaped zombie: pid=" << pid <<
": Caught an unknown exception";
throw ex;
}
}
......
......@@ -73,7 +73,7 @@ public:
/**
* Executes the main event loop of the ProcessForker.
*/
void execute();
void execute() throw();
private:
......@@ -138,7 +138,21 @@ private:
*
* @return true if the main event loop should continue.
*/
bool handleEvents();
bool handleEvents() throw ();
/**
* Handles a pending message if there is one.
*
* @return true if the main event loop should continue.
*/
bool handlePendingMsgs();
/**
* Handles any pending signals.
*
* @return true if the main event loop should continue.
*/
bool handlePendingSignals();
/**
* Return strue if there is a pending message from the ProcessForker proxy.
......@@ -252,7 +266,7 @@ private:
/**
* Reaps any zombie processes.
*/
void reapZombies() throw();
void reapZombies();
/**
* Handles the specified reaped zombie.
......@@ -260,7 +274,7 @@ private:
* @param pid The process ID of the reaped zombie.
* @param waitpidStat The status information given by a call to waitpid().
*/
void handleReapedZombie(const pid_t pid, const int waitpidStat) throw();
void handleReapedZombie(const pid_t pid, const int waitpidStat);
/**
* Logs the fact that the specified child process has terminated.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment