From ef8d9d6145d9d695f34ef789a96596bd27ff893b Mon Sep 17 00:00:00 2001 From: Unbit Date: Fri, 16 May 2014 05:27:52 +0200 Subject: [PATCH] added api for emperor attributes --- core/emperor.c | 51 ++++++++++++++++++---- plugins/emperor_mongodb/emperor_mongodb.cc | 12 ++++- uwsgi.h | 1 + 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/core/emperor.c b/core/emperor.c index 78ae70c2..8a1dc2aa 100644 --- a/core/emperor.c +++ b/core/emperor.c @@ -804,7 +804,6 @@ void emperor_del(struct uwsgi_instance *c_ui) { uwsgi.emperor_broodlord_count--; } - uwsgi_log("%s socket_name\n", c_ui->socket_name); if (c_ui->socket_name) { free(c_ui->socket_name); } @@ -812,13 +811,14 @@ void emperor_del(struct uwsgi_instance *c_ui) { if (c_ui->on_demand_fd > -1) { close(c_ui->on_demand_fd); } - uwsgi_log("%s %p\n", c_ui->config, c_ui->config); if (c_ui->config) free(c_ui->config); struct uwsgi_dyn_dict *attr = c_ui->attrs; while(attr) { struct uwsgi_dyn_dict *tmp = attr; attr = attr->next; + if (tmp->key) free(tmp->key); + if (tmp->value) free(tmp->value); free(tmp); } @@ -2004,16 +2004,12 @@ recheck: } } - if (diedpid > 0) { - uwsgi_log("DIEDPID = %d\n", diedpid); - } ui_current = ui; while (ui_current->ui_next) { ui_current = ui_current->ui_next; time_t now = uwsgi_now(); if (diedpid > 0 && ui_current->pid == diedpid) { if (ui_current->status == 0) { - uwsgi_log("OOOOPS\n"); // respawn an accidentally dead instance if its exit code is not UWSGI_EXILE_CODE if (WIFEXITED(waitpid_status) && WEXITSTATUS(waitpid_status) == UWSGI_EXILE_CODE) { // SAFE @@ -2210,6 +2206,31 @@ void emperor_send_stats(int fd) { if (uwsgi_stats_keyval_comma(us, "monitor", c_ui->scanner->arg)) goto end0; + if (uwsgi_stats_key(us, "attrs")) + goto end0; + + if (uwsgi_stats_list_open(us)) + goto end0; + + struct uwsgi_dyn_dict *attrs = c_ui->attrs; + while(attrs) { + if (attrs->next) { + if (uwsgi_stats_keyval_comma(us, attrs->key, attrs->value)) + goto end0; + } + else { + if (uwsgi_stats_keyval(us, attrs->key, attrs->value)) + goto end0; + } + attrs = attrs->next; + } + + if (uwsgi_stats_list_close(us)) + goto end0; + + if (uwsgi_stats_comma(us)) + goto end0; + if (uwsgi_stats_keylong(us, "respawns", (unsigned long long) c_ui->respawns)) goto end0; @@ -2407,10 +2428,21 @@ next: } -int uwsgi_emperor_simple_do_with_attrs(struct uwsgi_emperor_scanner *ues, char *name, char *config, time_t ts, uid_t uid, gid_t gid, char *socket_name, struct uwsgi_dyn_dict *attrs) { +void uwsgi_emperor_simple_do_with_attrs(struct uwsgi_emperor_scanner *ues, char *name, char *config, time_t ts, uid_t uid, gid_t gid, char *socket_name, struct uwsgi_dyn_dict *attrs) { uwsgi_emperor_simple_do(ues, name, config, ts, uid, gid, socket_name); struct uwsgi_instance *ui_current = emperor_get(name); - if (!ui_current) return -1; + // free attrs ? + if (!ui_current) { + struct uwsgi_dyn_dict *attr = attrs; + while(attr) { + struct uwsgi_dyn_dict *tmp = attr; + attr = attr->next; + if (tmp->key) free(tmp->key); + if (tmp->value) free(tmp->value); + free(tmp); + } + return; + } // if the instance has attrs mapped, let's free them if (ui_current->attrs) { @@ -2418,11 +2450,12 @@ int uwsgi_emperor_simple_do_with_attrs(struct uwsgi_emperor_scanner *ues, char * while(attr) { struct uwsgi_dyn_dict *tmp = attr; attr = attr->next; + if (tmp->key) free(tmp->key); + if (tmp->value) free(tmp->value); free(tmp); } } ui_current->attrs = attrs; - return 0; } void uwsgi_emperor_simple_do(struct uwsgi_emperor_scanner *ues, char *name, char *config, time_t ts, uid_t uid, gid_t gid, char *socket_name) { diff --git a/plugins/emperor_mongodb/emperor_mongodb.cc b/plugins/emperor_mongodb/emperor_mongodb.cc index 449f5d34..f6ef1ccb 100644 --- a/plugins/emperor_mongodb/emperor_mongodb.cc +++ b/plugins/emperor_mongodb/emperor_mongodb.cc @@ -76,7 +76,17 @@ extern "C" void uwsgi_imperial_monitor_mongodb(struct uwsgi_emperor_scanner *ues const char *socket_name = p.getStringField("socket"); if (strlen(socket_name) == 0) socket_name = NULL; - uwsgi_emperor_simple_do(ues, (char *) name, (char *) config, vassal_ts/1000, vassal_uid, vassal_gid, (char *) socket_name); + struct uwsgi_dyn_dict *attrs = NULL; + char *attr_key = NULL; + char *attr_value = NULL; + + if (attrs) { + // attrs will be freed in case of error + uwsgi_emperor_simple_do_with_attrs(ues, (char *) name, (char *) config, vassal_ts/1000, vassal_uid, vassal_gid, (char *) socket_name, attrs); + } + else { + uwsgi_emperor_simple_do(ues, (char *) name, (char *) config, vassal_ts/1000, vassal_uid, vassal_gid, (char *) socket_name); + } } diff --git a/uwsgi.h b/uwsgi.h index 78524968..33f1e9b5 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -4167,6 +4167,7 @@ void uwsgi_master_cleanup_hooks(void); pid_t uwsgi_daemonize2(); void uwsgi_emperor_simple_do(struct uwsgi_emperor_scanner *, char *, char *, time_t, uid_t, gid_t, char *); +void uwsgi_emperor_simple_do_with_attrs(struct uwsgi_emperor_scanner *, char *, char *, time_t, uid_t, gid_t, char *, struct uwsgi_dyn_dict *); #if defined(__linux__) #define UWSGI_ELF