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 4F06C2D5C4 for ; Mon, 26 Nov 2018 09:25:36 -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 13ThklfFYiwS for ; Mon, 26 Nov 2018 09:25:36 -0500 (EST) Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [94.100.177.104]) (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 10CB02B1A4 for ; Mon, 26 Nov 2018 09:25:36 -0500 (EST) From: Serge Petrenko Subject: [tarantool-patches] [PATCH v2 2/3] box: introduce on_shutdown triggers to lua Date: Mon, 26 Nov 2018 17:25:05 +0300 Message-Id: <73fbfaf8aa7b9676fdc1694b27963d3c21014fd8.1543241956.git.sergepetrenko@tarantool.org> 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, tarantool-patches@freelists.org Cc: Serge Petrenko Make it possible to register on_shutdown triggers from lua via box.ctl.on_shutdown() Part of #1607 --- src/box/lua/ctl.c | 21 +++++++++++++++++++++ test/box/misc.result | 34 ++++++++++++++++++++++++++++++++++ test/box/misc.test.lua | 14 ++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/box/lua/ctl.c b/src/box/lua/ctl.c index 9a105ed5c..dad39515c 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. It will be used when we decide to pass + * a signal which caused termination to the trigger. + */ +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 4ee4797d0..fd55b8cf1 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -1205,3 +1205,37 @@ 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 +--- +... +_ = box.ctl.on_shutdown(f) +--- +... +_ = box.ctl.on_shutdown(g) +--- +... +_ = box.ctl.on_shutdown(h, g) +--- +... +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 +... diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua index ee81c7be1..a4b38b403 100644 --- a/test/box/misc.test.lua +++ b/test/box/misc.test.lua @@ -342,3 +342,17 @@ 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 +_ = box.ctl.on_shutdown(f) +_ = box.ctl.on_shutdown(g) +_ = box.ctl.on_shutdown(h, g) +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}) -- 2.17.2 (Apple Git-113)