From 5cea1de1bac82d49fa37d7dbfc9b581c6abaebd2 Mon Sep 17 00:00:00 2001 From: "roberto@quantal64" Date: Wed, 12 Sep 2012 19:03:22 +0200 Subject: [PATCH] added master_cleanup hook and carbon flush on stop/reload --- core/master.c | 2 ++ core/master_utils.c | 24 ++++++++++++++++++++++++ plugins/carbon/carbon.c | 5 ++++- uwsgi.h | 5 +++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/core/master.c b/core/master.c index 083f7475..c4d5a6c5 100644 --- a/core/master.c +++ b/core/master.c @@ -608,6 +608,8 @@ int master_loop(char **argv, char **environ) { uwsgi_unix_signal(SIGURG, uwsgi_restore_auto_snapshot); } + atexit(uwsgi_master_cleanup_hooks); + uwsgi.master_queue = event_queue_init(); /* route signals to workers... */ diff --git a/core/master_utils.c b/core/master_utils.c index 073c28cf..da155f99 100644 --- a/core/master_utils.c +++ b/core/master_utils.c @@ -5,6 +5,27 @@ extern struct uwsgi_server uwsgi; void worker_wakeup() { } +void uwsgi_master_cleanup_hooks(void) { + + int j; + + uwsgi.cleaning = 1; + + for (j = 0; j < uwsgi.gp_cnt; j++) { + if (uwsgi.gp[j]->master_cleanup) { + uwsgi.gp[j]->master_cleanup(); + } + } + + for (j = 0; j < 256; j++) { + if (uwsgi.p[j]->master_cleanup) { + uwsgi.p[j]->master_cleanup(); + } + } + +} + + int uwsgi_calc_cheaper(void) { int i; @@ -240,6 +261,9 @@ void uwsgi_reload(char **argv) { waitpid(WAIT_ANY, &waitpid_status, WNOHANG); } + // call master cleanup hooks + uwsgi_master_cleanup_hooks(); + // call atexit user exec uwsgi_exec_atexit(); diff --git a/plugins/carbon/carbon.c b/plugins/carbon/carbon.c index dd0e6304..c6159ad9 100644 --- a/plugins/carbon/carbon.c +++ b/plugins/carbon/carbon.c @@ -68,7 +68,7 @@ void carbon_master_cycle() { if (last_update == 0) last_update = uwsgi_now(); // update - if (uwsgi.current_time - last_update >= u_carbon.freq) { + if (uwsgi.current_time - last_update >= u_carbon.freq || uwsgi.cleaning) { for (i = 0; i < uwsgi.numproc; i++) { u_carbon.current_busyness_values[i] = uwsgi.workers[i+1].running_time - u_carbon.last_busyness_values[i]; @@ -203,10 +203,13 @@ nxt: } } + struct uwsgi_plugin carbon_plugin = { .name = "carbon", + .master_cleanup = carbon_master_cycle, + .options = carbon_options, .master_cycle = carbon_master_cycle, .post_init = carbon_post_init, diff --git a/uwsgi.h b/uwsgi.h index 3c84b7fc..40096911 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -713,6 +713,8 @@ struct uwsgi_plugin { int (*mule)(char *); int (*mule_msg)(char *, size_t); + void (*master_cleanup) (void); + }; #ifdef UWSGI_PCRE @@ -1519,6 +1521,8 @@ struct uwsgi_server { int to_hell; int to_outworld; + int cleaning; + int ready_to_die; int ready_to_reload; @@ -3241,6 +3245,7 @@ ssize_t uwsgi_pipe(int, int, int); ssize_t uwsgi_pipe_sized(int, int, size_t, int); int uwsgi_buffer_send(struct uwsgi_buffer *, int); +void uwsgi_master_cleanup_hooks(void); void uwsgi_check_emperor(void); #ifdef UWSGI_AS_SHARED_LIBRARY