From 85662578e465336d0b7674e0afef9c5a4ad95bef Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Wed, 12 Oct 2011 01:48:30 +0200 Subject: [PATCH] implemented --mule-harakiri --- master.c | 9 +++++++++ master_utils.c | 7 ++++++- plugins/python/uwsgi_pymodule.c | 5 ++++- signal.c | 36 ++++++++++++++++++++++++++++++++- utils.c | 27 +++++++++++++++++-------- uwsgi.c | 12 +++++++++++ uwsgi.h | 14 +++++++++++++ 7 files changed, 99 insertions(+), 11 deletions(-) diff --git a/master.c b/master.c index 94bd0bb2..c1b11391 100644 --- a/master.c +++ b/master.c @@ -1339,6 +1339,15 @@ healthy: // need to find a better way //uwsgi.workers[i].last_running_time = uwsgi.workers[i].running_time; } + for (i = 0; i < uwsgi.mules_cnt; i++) { + if (uwsgi.mules[i].harakiri > 0) { + if (uwsgi.mules[i].harakiri < (time_t) uwsgi.current_time) { + uwsgi_log("*** HARAKIRI ON MULE %d HANDLING SIGNAL %d (pid: %d) ***\n", i+1, uwsgi.mules[i].signum, uwsgi.mules[i].pid ); + kill(uwsgi.mules[i].pid, SIGKILL); + uwsgi.mules[i].harakiri = 0; + } + } + } #ifdef UWSGI_UDP // check for cluster nodes diff --git a/master_utils.c b/master_utils.c index b9d51c2f..9bbedbaa 100644 --- a/master_utils.c +++ b/master_utils.c @@ -94,6 +94,8 @@ int uwsgi_respawn_worker(int wid) { uwsgi.workers[uwsgi.mywid].last_spawn = uwsgi.current_time; uwsgi.workers[uwsgi.mywid].manage_next_request = 1; uwsgi.workers[uwsgi.mywid].cheaped = 0; + uwsgi.workers[uwsgi.mywid].busy = 0; + uwsgi.workers[uwsgi.mywid].sig = 0; // reset the apps count with a copy from the master uwsgi.workers[uwsgi.mywid].apps_cnt = uwsgi.workers[0].apps_cnt; @@ -322,7 +324,10 @@ void uwsgi_send_stats(int fd) { fprintf(output,"\"status\": \"cheap\", "); } else { - if (uwsgi.workers[i + 1].busy) { + if (uwsgi.workers[i + 1].sig) { + fprintf(output,"\"status\": \"sig%d\", ", uwsgi.workers[i + 1].signum); + } + else if (uwsgi.workers[i + 1].busy) { fprintf(output,"\"status\": \"busy\", "); } else { diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index b44ba67c..4f900963 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -2282,7 +2282,10 @@ PyObject *py_uwsgi_workers(PyObject * self, PyObject * args) { zero = PyString_FromString("cheap"); } else { - if (uwsgi.workers[i + 1].busy) { + if (uwsgi.workers[i + 1].sig) { + zero = PyString_FromFormat("sig%d",uwsgi.workers[i + 1].signum); + } + else if (uwsgi.workers[i + 1].busy) { zero = PyString_FromString("busy"); } else { diff --git a/signal.c b/signal.c index d70bc4c1..d1a2f712 100644 --- a/signal.c +++ b/signal.c @@ -12,7 +12,41 @@ int uwsgi_signal_handler(uint8_t sig) { return -1; } - return uwsgi.p[use->modifier1]->signal_handler(sig, use->handler); + // set harakiri here (if required and if i am a worker) + + if (uwsgi.mywid > 0) { + uwsgi.workers[uwsgi.mywid].sig = 1; + uwsgi.workers[uwsgi.mywid].signum = sig; + uwsgi.workers[uwsgi.mywid].signals++; + if(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) { + set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]); + } + } + else if (uwsgi.muleid > 0) { + uwsgi.mules[uwsgi.muleid-1].sig = 1; + uwsgi.mules[uwsgi.muleid-1].signum = sig; + uwsgi.mules[uwsgi.muleid-1].signals++; + if(uwsgi.shared->options[UWSGI_OPTION_MULE_HARAKIRI] > 0) { + set_mule_harakiri(uwsgi.shared->options[UWSGI_OPTION_MULE_HARAKIRI]); + } + } + + int ret = uwsgi.p[use->modifier1]->signal_handler(sig, use->handler); + + if (uwsgi.mywid > 0) { + uwsgi.workers[uwsgi.mywid].sig = 0; + if(uwsgi.workers[uwsgi.mywid].harakiri > 0) { + set_harakiri(0); + } + } + if (uwsgi.muleid > 0) { + uwsgi.mules[uwsgi.muleid-1].sig = 0; + if(uwsgi.mules[uwsgi.muleid-1].harakiri > 0) { + set_mule_harakiri(0); + } + } + + return ret; } int uwsgi_signal_registered(uint8_t sig) { diff --git a/utils.c b/utils.c index d1bb5238..a9942d4d 100644 --- a/utils.c +++ b/utils.c @@ -98,19 +98,30 @@ void inc_harakiri(int sec) { } void set_harakiri(int sec) { - if (uwsgi.master_process) { - if (sec == 0) { - uwsgi.workers[uwsgi.mywid].harakiri = 0; - } - else { - uwsgi.workers[uwsgi.mywid].harakiri = time(NULL) + sec; - } + if (sec == 0) { + uwsgi.workers[uwsgi.mywid].harakiri = 0; } else { + uwsgi.workers[uwsgi.mywid].harakiri = time(NULL) + sec; + } + if (!uwsgi.master_process) { alarm(sec); } } +void set_mule_harakiri(int sec) { + if (sec == 0) { + uwsgi.mules[uwsgi.muleid-1].harakiri = 0; + } + else { + uwsgi.mules[uwsgi.muleid-1].harakiri = time(NULL) + sec; + } + if (!uwsgi.master_process) { + alarm(sec); + } +} + + void daemonize(char *logfile) { pid_t pid; int fdin; @@ -760,7 +771,7 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) { #endif // leave harakiri mode - if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) { + if (uwsgi.workers[uwsgi.mywid].harakiri > 0) { set_harakiri(0); } diff --git a/uwsgi.c b/uwsgi.c index a643e8f8..122d3408 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -50,6 +50,8 @@ static struct option long_base_options[] = { {"workers", required_argument, 0, 'p'}, {"harakiri", required_argument, 0, 't'}, {"harakiri-verbose", no_argument, &uwsgi.harakiri_verbose, 1}, + {"spooler-harakiri", required_argument, 0, LONG_ARGS_SPOOLER_HARAKIRI}, + {"mule-harakiri", required_argument, 0, LONG_ARGS_MULE_HARAKIRI}, #ifdef UWSGI_XML {"xmlconfig", required_argument, 0, 'x'}, {"xml", required_argument, 0, 'x'}, @@ -754,6 +756,9 @@ void what_i_am_doing() { uwsgi_log("SIGUSR2: --- uWSGI worker %d (pid: %d) is managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) &wsgi_req->start_of_request.tv_sec)); } } + else if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < time(NULL) && uwsgi.workers[uwsgi.mywid].sig) { + uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS handling signal %d ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, uwsgi.workers[uwsgi.mywid].signum); + } } } @@ -3490,6 +3495,13 @@ static int manage_base_opt(int i, char *optarg) { case 't': uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] = atoi(optarg); return 1; + case LONG_ARGS_SPOOLER_HARAKIRI: + uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] = atoi(optarg); + return 1; + case LONG_ARGS_MULE_HARAKIRI: + uwsgi.shared->options[UWSGI_OPTION_MULE_HARAKIRI] = atoi(optarg); + return 1; + case 'b': uwsgi.buffer_size = atoi(optarg); return 1; diff --git a/uwsgi.h b/uwsgi.h index b0bac002..80d001c3 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -523,6 +523,8 @@ struct uwsgi_opt { #define LONG_ARGS_LOG_BACKUPNAME 17145 #define LONG_ARGS_EVIL_RELOAD_ON_AS 17146 #define LONG_ARGS_EVIL_RELOAD_ON_RSS 17147 +#define LONG_ARGS_SPOOLER_HARAKIRI 17148 +#define LONG_ARGS_MULE_HARAKIRI 17149 #define UWSGI_OK 0 @@ -561,6 +563,8 @@ struct uwsgi_opt { #define UWSGI_OPTION_LOG_SENDFILE 14 #define UWSGI_OPTION_BACKLOG_STATUS 15 #define UWSGI_OPTION_BACKLOG_ERRORS 16 +#define UWSGI_OPTION_SPOOLER_HARAKIRI 17 +#define UWSGI_OPTION_MULE_HARAKIRI 18 #define UWSGI_MODIFIER_ADMIN_REQUEST 10 #define UWSGI_MODIFIER_SPOOL_REQUEST 17 @@ -1687,6 +1691,11 @@ struct uwsgi_worker { int busy; int cheaped; + int sig; + uint8_t signum; + + // signals managed by this worker + uint64_t signals; int signal_pipe[2]; @@ -1708,6 +1717,10 @@ struct uwsgi_mule { // signals managed by this mule uint64_t signals; + int sig; + uint8_t signum; + + time_t harakiri; char name[0xff]; }; @@ -1762,6 +1775,7 @@ pid_t spooler_start(void); #endif void set_harakiri(int); +void set_mule_harakiri(int); void inc_harakiri(int); #ifdef __BIG_ENDIAN__