From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 27 Dec 2018 16:14:47 +0300 From: Vladimir Davydov Subject: Re: [tarantool-patches] [PATCH v3 3/3] box: introduce on_shutdown triggers to lua Message-ID: <20181227131447.mgeedrgtt2bp4f3i@esperanza> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: To: Serge Petrenko Cc: kostja@tarantool.org, tarantool-patches@freelists.org List-ID: On Tue, Dec 11, 2018 at 04:24:40PM +0300, Serge Petrenko wrote: > 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. I don't think so. Let's instead remove this placeholder and add a check for push_event != NULL to lbox_trigger_run(). > + */ > +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.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 Let's test something more complicated, e.g. inserting a tuple into a space. > +_ = 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})