diff options
author | Alexander Borisov <alexander.borisov@nginx.com> | 2018-11-15 15:39:34 +0300 |
---|---|---|
committer | Alexander Borisov <alexander.borisov@nginx.com> | 2018-11-15 15:39:34 +0300 |
commit | 06b404feb17dec9e3a84da31e213f21a0a9b4846 (patch) | |
tree | 248c1a6bec69d3eb358a680a561d48f792b51893 | |
parent | 71f4cb91340b656397eaaa356755b4a1e31c8010 (diff) | |
download | unit-06b404feb17dec9e3a84da31e213f21a0a9b4846.tar.gz unit-06b404feb17dec9e3a84da31e213f21a0a9b4846.tar.bz2 |
Node.js: added correct exit processing.
Node.js processes didn't exit after the changes in b9f7635e6be2,
as the quit command from port wasn't handled by the module.
-rw-r--r-- | src/nodejs/unit-http/unit.cpp | 60 | ||||
-rw-r--r-- | src/nodejs/unit-http/unit.h | 1 |
2 files changed, 43 insertions, 18 deletions
diff --git a/src/nodejs/unit-http/unit.cpp b/src/nodejs/unit-http/unit.cpp index bc6fc7db..788a5cc8 100644 --- a/src/nodejs/unit-http/unit.cpp +++ b/src/nodejs/unit-http/unit.cpp @@ -14,6 +14,12 @@ napi_ref Unit::constructor_; +struct nxt_nodejs_ctx_t { + nxt_unit_port_id_t port_id; + uv_poll_t poll; +}; + + Unit::Unit(napi_env env): env_(env), wrapper_(nullptr), @@ -197,6 +203,7 @@ Unit::create_server(napi_env env, napi_callback_info info) unit_init.callbacks.request_handler = request_handler; unit_init.callbacks.add_port = add_port; unit_init.callbacks.remove_port = remove_port; + unit_init.callbacks.quit = quit; obj->unit_ctx_ = nxt_unit_init(&unit_init); if (obj->unit_ctx_ == NULL) { @@ -355,11 +362,11 @@ nxt_uv_read_callback(uv_poll_t *handle, int status, int events) int Unit::add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { - int err; - Unit *obj; - uv_loop_t *loop; - uv_poll_t *uv_handle; - napi_status status; + int err; + Unit *obj; + uv_loop_t *loop; + napi_status status; + nxt_nodejs_ctx_t *node_ctx; if (port->in_fd != -1) { obj = reinterpret_cast<Unit *>(ctx->unit->data); @@ -376,48 +383,65 @@ Unit::add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) return NXT_UNIT_ERROR; } - uv_handle = new uv_poll_t; + node_ctx = new nxt_nodejs_ctx_t; - err = uv_poll_init(loop, uv_handle, port->in_fd); + err = uv_poll_init(loop, &node_ctx->poll, port->in_fd); if (err < 0) { napi_throw_error(obj->env_, NULL, "Failed to init uv.poll"); return NXT_UNIT_ERROR; } - err = uv_poll_start(uv_handle, UV_READABLE, nxt_uv_read_callback); + err = uv_poll_start(&node_ctx->poll, UV_READABLE, nxt_uv_read_callback); if (err < 0) { napi_throw_error(obj->env_, NULL, "Failed to start uv.poll"); return NXT_UNIT_ERROR; } - port->data = uv_handle; - uv_handle->data = ctx; + ctx->data = node_ctx; + + node_ctx->port_id = port->id; + node_ctx->poll.data = ctx; } return nxt_unit_add_port(ctx, port); } +inline bool +operator == (const nxt_unit_port_id_t &p1, const nxt_unit_port_id_t &p2) +{ + return p1.pid == p2.pid && p1.id == p2.id; +} + + void Unit::remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id) { - nxt_unit_port_t *port; + nxt_nodejs_ctx_t *node_ctx; - port = nxt_unit_find_port(ctx, port_id); - if (port == NULL) { - return; - } + if (ctx->data != NULL) { + node_ctx = (nxt_nodejs_ctx_t *) ctx->data; + + if (node_ctx->port_id == *port_id) { + uv_poll_stop(&node_ctx->poll); - if (port->in_fd != -1 && port->data != NULL) { - uv_poll_stop((uv_poll_t *) port->data); + delete node_ctx; - delete (uv_poll_t *) port->data; + ctx->data = NULL; + } } nxt_unit_remove_port(ctx, port_id); } +void +Unit::quit(nxt_unit_ctx_t *ctx) +{ + nxt_unit_done(ctx); +} + + napi_value Unit::get_server_object() { diff --git a/src/nodejs/unit-http/unit.h b/src/nodejs/unit-http/unit.h index 90c67efc..5f541cc4 100644 --- a/src/nodejs/unit-http/unit.h +++ b/src/nodejs/unit-http/unit.h @@ -40,6 +40,7 @@ private: static void request_handler(nxt_unit_request_info_t *req); static int add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); static void remove_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id); + static void quit(nxt_unit_ctx_t *ctx); napi_value get_server_object(); |