Fixed issue with reloadable libraries

This commit is contained in:
jmaa 2018-06-16 13:10:23 +02:00
parent a491765d59
commit 34a24fad4e

View File

@ -331,17 +331,30 @@ local function init_memebot ()
shutdown_memebot()
os.exit(128 + signum)
end)
---
local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' }
local ORIG_TABLES = {}
for _, module_name in ipairs(RELOADABLE_LIBS) do
ORIG_TABLES[module_name] = require(module_name)
end
--
signal.signal(signal.SIGUSR1, function (signum)
io.write ' !! Received SIGUSR1, will reload modules...\n'
for _, module_name in ipairs { 'internet', 'memes', 'curb_your_enthusiasm' } do
local old_module = require(module_name)
for _, module_name in ipairs(RELOADABLE_LIBS) do
io.write(' - '..module_name)
package.loaded[module_name] = nil
local new_module = require(module_name)
for k, v in pairs(new_module) do
old_module[k] = v
end
io.write(' - '..module_name..'\n')
if type(new_module) == 'table' then
local old_module = ORIG_TABLES[module_name]
for k, v in pairs(new_module) do
old_module[k] = v
end
else
io.write(' SKIP: Got '..tostring(new_module)..' but expected table.')
end
io.write '\n'
end
io.write ' Done\n'
end)
else
io.write ' !! Module "posix.signal" was missing, so CTRL+C will hard-crash.\n'