Add OnServerReload to replace /.reload.lua with a bit more flexibility (#851)

This commit is contained in:
Paul Kulchenko 2023-08-17 00:42:23 -07:00 committed by GitHub
parent 3a9cac4892
commit d0d027810a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View file

@ -499,7 +499,7 @@ SPECIAL PATHS
this path is a hidden file so that it can't be unintentionally run this path is a hidden file so that it can't be unintentionally run
by the network client. by the network client.
/.reload.lua /.reload.lua (deprecated; use OnServerReload instead)
This script is run from the main process when SIGHUP is received. This script is run from the main process when SIGHUP is received.
This only applies to redbean when running in daemon mode. Any This only applies to redbean when running in daemon mode. Any
changes that are made to the Lua interpreter state will be changes that are made to the Lua interpreter state will be
@ -612,6 +612,12 @@ HOOKS
to modify socket configuration to set `SO_REUSEPORT`, for example. to modify socket configuration to set `SO_REUSEPORT`, for example.
If it returns `true`, redbean will not listen to that ip/port. If it returns `true`, redbean will not listen to that ip/port.
OnServerReload(reindex:bool)
If this function is defined it'll be called from the main process
on each server reload triggered by SIGHUP (for deamonized) and
SIGUSR1 (for all) redbean instances. reindex indicates if redbean
assets have been re-indexed following the signal.
OnServerStart() OnServerStart()
If this function is defined it'll be called from the main process If this function is defined it'll be called from the main process
right before the main event loop starts. right before the main event loop starts.

View file

@ -5560,11 +5560,20 @@ static void LuaInit(void) {
#endif #endif
} }
static void LuaReload(void) { static void LuaOnServerReload(bool reindex) {
#ifndef STATIC #ifndef STATIC
if (!LuaRunAsset("/.reload.lua", false)) { if (!LuaRunAsset("/.reload.lua", false)) {
DEBUGF("(srvr) no /.reload.lua defined"); DEBUGF("(srvr) no /.reload.lua defined");
} }
lua_State *L = GL;
lua_getglobal(L, "OnServerReload");
lua_pushboolean(L, reindex);
if (LuaCallWithTrace(L, 1, 0, NULL) != LUA_OK) {
LogLuaError("OnServerReload", lua_tostring(L, -1));
lua_pop(L, 1); // pop error
}
AssertLuaStackIsAt(L, 0);
#endif #endif
} }
@ -5754,8 +5763,8 @@ static void HandleFrag(size_t got) {
static void HandleReload(void) { static void HandleReload(void) {
LockInc(&shared->c.reloads); LockInc(&shared->c.reloads);
Reindex(); LuaOnServerReload(Reindex());
LuaReload(); invalidated = false;
} }
static void HandleHeartbeat(void) { static void HandleHeartbeat(void) {
@ -6506,7 +6515,6 @@ static void HandleMessages(void) {
} }
if (invalidated) { if (invalidated) {
HandleReload(); HandleReload();
invalidated = false;
} }
} }
if (cpm.msgsize == amtread) { if (cpm.msgsize == amtread) {
@ -6537,7 +6545,6 @@ static void HandleMessages(void) {
CollectGarbage(); CollectGarbage();
if (invalidated) { if (invalidated) {
HandleReload(); HandleReload();
invalidated = false;
} }
} }
} }
@ -7135,7 +7142,6 @@ int EventLoop(int ms) {
lua_repl_lock(); lua_repl_lock();
HandleReload(); HandleReload();
lua_repl_unlock(); lua_repl_unlock();
invalidated = false;
} else if (meltdown) { } else if (meltdown) {
lua_repl_lock(); lua_repl_lock();
EnterMeltdownMode(); EnterMeltdownMode();