[tarantool-patches] [PATCH 2/2] box: introduce on_shutdown triggers to lua
Serge Petrenko
sergepetrenko at tarantool.org
Thu Nov 22 20:20:55 MSK 2018
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 <lualib.h>
#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)
More information about the Tarantool-patches
mailing list