server: Add "reason" parameter to interface::Server::shutdown() which is printed and logged as the last thing before the server exits

This commit is contained in:
Perttu Ahola 2014-09-29 10:44:08 +03:00
parent 0f6900d669
commit 0c98f5ab4a
4 changed files with 29 additions and 9 deletions

View File

@ -40,7 +40,7 @@ namespace interface
{
virtual ~Server(){}
virtual void shutdown(int exit_status = 0) = 0;
virtual void shutdown(int exit_status = 0, const ss_ &reason="") = 0;
virtual bool load_module(const ss_ &module_name, const ss_ &path) = 0;
virtual void unload_module(const ss_ &module_name) = 0;

View File

@ -11,6 +11,7 @@
#include <unistd.h>
#include <signal.h>
#include <string.h> // strerror()
#define MODULE "main"
server::Config g_server_config;
@ -117,6 +118,7 @@ int main(int argc, char *argv[])
// Main loop
int exit_status = 0;
ss_ shutdown_reason;
uint64_t next_tick_us = get_timeofday_us();
uint64_t t_per_tick = 1000 * 100;
set_<int> attempt_bad_fds;
@ -214,10 +216,20 @@ int main(int argc, char *argv[])
state->handle_events();
if(state->is_shutdown_requested(&exit_status))
if(state->is_shutdown_requested(&exit_status, &shutdown_reason))
break;
}
// Destruct server state here
state.reset(nullptr);
if(shutdown_reason != ""){
if(exit_status != 0)
log_w(MODULE, "Shutdown: %s", cs(shutdown_reason));
else
log_v(MODULE, "Shutdown: %s", cs(shutdown_reason));
}
return exit_status;
}

View File

@ -107,6 +107,7 @@ struct CState: public State, public interface::Server
bool m_shutdown_requested = false;
int m_shutdown_exit_status = 0;
ss_ m_shutdown_reason;
up_<rccpp::Compiler> m_compiler;
ss_ m_modules_path;
@ -150,21 +151,27 @@ struct CState: public State, public interface::Server
}
}
void shutdown(int exit_status)
void shutdown(int exit_status, const ss_ &reason)
{
if(m_shutdown_requested && exit_status == 0){
// Only reset these values for exit values indicating failure
return;
}
log_i(MODULE, "Server shutdown requested; exit_status=%i", exit_status);
log_i(MODULE, "Server shutdown requested; exit_status=%i, reason=\"%s\"",
exit_status, cs(reason));
m_shutdown_requested = true;
m_shutdown_exit_status = exit_status;
m_shutdown_reason = reason;
}
bool is_shutdown_requested(int *exit_status = nullptr)
bool is_shutdown_requested(int *exit_status = nullptr, ss_ *reason = nullptr)
{
if(m_shutdown_requested && exit_status)
*exit_status = m_shutdown_exit_status;
if(m_shutdown_requested){
if(exit_status)
*exit_status = m_shutdown_exit_status;
if(reason)
*reason = m_shutdown_reason;
}
return m_shutdown_requested;
}

View File

@ -24,8 +24,9 @@ namespace server
struct State
{
virtual ~State(){}
virtual void shutdown(int exit_status = 0) = 0;
virtual bool is_shutdown_requested(int *exit_status = nullptr) = 0;
virtual void shutdown(int exit_status = 0, const ss_ &reason="") = 0;
virtual bool is_shutdown_requested(int *exit_status = nullptr,
ss_ *reason = nullptr) = 0;
virtual bool load_module(const ss_ &module_name, const ss_ &path) = 0;
virtual void load_modules(const ss_ &path) = 0;
virtual interface::Module* get_module(const ss_ &module_name) = 0;