From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 258A121B87 for ; Tue, 11 Dec 2018 08:26:09 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1_gAkcSnMt1p for ; Tue, 11 Dec 2018 08:26:08 -0500 (EST) Received: from smtp56.i.mail.ru (smtp56.i.mail.ru [217.69.128.36]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 793A021B82 for ; Tue, 11 Dec 2018 08:24:49 -0500 (EST) From: Serge Petrenko Subject: [tarantool-patches] [PATCH v3 3/3] box: introduce on_shutdown triggers to lua Date: Tue, 11 Dec 2018 16:24:40 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: kostja@tarantool.org Cc: tarantool-patches@freelists.org, Serge Petrenko Make it possible to register on_shutdown triggers from lua via box.ctl.on_shutdown() Closes #1607 @TarantoolBot document Title: Document box.ctl.on_shutdown triggers on_shutdown triggers may be set similar to space:on_replace triggers: ``` box.ctl.on_shutdown(new_trigger, old_trigger) ``` The triggers will be run when tarantool exits due to receiving one of the signals: `SIGTERM`, `SIGINT`, `SIGHUP`. Note that the triggers will not be run if you exit tarantool by typing `os.exit()` to the lua console or if tarantool receives a fatal signal: `SIGSEGV`, `SIGABORT` or any signal causing immediate program termination. --- src/box/lua/ctl.c | 21 ++++++++++++++++++ test/box/misc.result | 48 ++++++++++++++++++++++++++++++++++++++++++ test/box/misc.test.lua | 19 +++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/src/box/lua/ctl.c b/src/box/lua/ctl.c index 9a105ed5c..aa835d6d5 100644 --- a/src/box/lua/ctl.c +++ b/src/box/lua/ctl.c @@ -37,6 +37,7 @@ #include #include "lua/utils.h" +#include "lua/trigger.h" #include "box/box.h" @@ -64,9 +65,29 @@ lbox_ctl_wait_rw(struct lua_State *L) return 0; } +/* + * This is a placeholder. + * We may decide to pass some arguments to on_shutdown triggers. + */ +static int +lbox_push_on_shutdown(struct lua_State *L, void *event) +{ + (void)L; + (void)event; + return 0; +} + +static int +lbox_ctl_on_shutdown(struct lua_State *L) +{ + return lbox_trigger_reset(L, 2, &on_shutdown, lbox_push_on_shutdown, + NULL); +} + static const struct luaL_Reg lbox_ctl_lib[] = { {"wait_ro", lbox_ctl_wait_ro}, {"wait_rw", lbox_ctl_wait_rw}, + {"on_shutdown", lbox_ctl_on_shutdown}, {NULL, NULL} }; diff --git a/test/box/misc.result b/test/box/misc.result index d266bb334..981e20157 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -1248,3 +1248,51 @@ box.cfg{too_long_threshold = too_long_threshold} s:drop() --- ... +-- +-- gh-1607: on_shutdown triggers. +-- +f = function() print('on_shutdown 1') end +--- +... +g = function() print('on_shutdown 2') end +--- +... +h = function() print('on_shutdown 3') end +--- +... +-- check that on_shutdown triggers may yield and sleep. +fiber = require('fiber') +--- +... +trig = function() fiber.sleep(0.01) fiber.yield() print('on_shutdown 4') end +--- +... +_ = box.ctl.on_shutdown(f) +--- +... +_ = box.ctl.on_shutdown(g) +--- +... +_ = box.ctl.on_shutdown(h, g) +--- +... +_ = box.ctl.on_shutdown(trig) +--- +... +test_run:cmd('restart server default') +test_run:grep_log('default', 'on_shutdown 1', nil, {noreset=true}) +--- +- on_shutdown 1 +... +test_run:grep_log('default', 'on_shutdown 2', nil, {noreset=true}) +--- +- null +... +test_run:grep_log('default', 'on_shutdown 3', nil, {noreset=true}) +--- +- on_shutdown 3 +... +test_run:grep_log('default', 'on_shutdown 4', nil, {noreset=true}) +--- +- on_shutdown 4 +... diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua index cc6cb34fb..f9d0f53f2 100644 --- a/test/box/misc.test.lua +++ b/test/box/misc.test.lua @@ -352,3 +352,22 @@ rows == expected_rows lsn == expected_lsn box.cfg{too_long_threshold = too_long_threshold} s:drop() + +-- +-- gh-1607: on_shutdown triggers. +-- +f = function() print('on_shutdown 1') end +g = function() print('on_shutdown 2') end +h = function() print('on_shutdown 3') end +-- check that on_shutdown triggers may yield and sleep. +fiber = require('fiber') +trig = function() fiber.sleep(0.01) fiber.yield() print('on_shutdown 4') end +_ = box.ctl.on_shutdown(f) +_ = box.ctl.on_shutdown(g) +_ = box.ctl.on_shutdown(h, g) +_ = box.ctl.on_shutdown(trig) +test_run:cmd('restart server default') +test_run:grep_log('default', 'on_shutdown 1', nil, {noreset=true}) +test_run:grep_log('default', 'on_shutdown 2', nil, {noreset=true}) +test_run:grep_log('default', 'on_shutdown 3', nil, {noreset=true}) +test_run:grep_log('default', 'on_shutdown 4', nil, {noreset=true}) -- 2.17.2 (Apple Git-113)