mqttagent

MQTT Lua Agent
git clone https://git.instinctive.eu/mqttagent.git
Log | Files | Refs | README | LICENSE

commit 1facc09d29ab5558c924481f06c3ebaae6433e6d
parent 63efe0a8f50534d7a50a26b3e2dc1b0854d0e66e
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Mon, 21 Apr 2025 18:19:23 +0000

SQL objects are re-used acress reloads
Diffstat:
Mcmd/mqttagent-full/main.go | 41++++++++++++++++++++++++++++++++++++++++-
1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/cmd/mqttagent-full/main.go b/cmd/mqttagent-full/main.go @@ -29,11 +29,13 @@ import ( ) type fullMqttAgent struct { - loggers map[string]*sqlogger + loggers map[string]*sqlogger + oldLoggers map[string]*sqlogger } func (agent *fullMqttAgent) Setup(L *lua.LState) { luajson.Preload(L) + agent.loggers = make(map[string]*sqlogger) mt := L.NewTypeMetatable("sqlogger") L.SetGlobal("sqlogger", mt) @@ -44,12 +46,42 @@ func (agent *fullMqttAgent) Setup(L *lua.LState) { } func (agent *fullMqttAgent) Teardown(L *lua.LState) { + if agent.oldLoggers != nil { + panic("Unexpected state") + } for _, logger := range agent.loggers { logger.Close() } agent.loggers = nil } +func (agent *fullMqttAgent) ReloadBegin(oldL, newL *lua.LState) { + if agent.oldLoggers != nil { + panic("Unexpected state") + } + agent.oldLoggers = agent.loggers + agent.Setup(newL) +} + +func (agent *fullMqttAgent) ReloadAbort(oldL, newL *lua.LState) { + for key, logger := range agent.loggers { + if _, found := agent.oldLoggers[key]; !found { + logger.Close() + } + } + agent.loggers = agent.oldLoggers + agent.oldLoggers = nil +} + +func (agent *fullMqttAgent) ReloadEnd(oldL, newL *lua.LState) { + for key, logger := range agent.oldLoggers { + if _, found := agent.loggers[key]; !found { + logger.Close() + } + } + agent.oldLoggers = nil +} + func (logger *sqlogger) Received(msg *mqttagent.MqttMessage) { if logger.insertTopic == nil || logger.insertReceived == nil { return @@ -151,6 +183,13 @@ func luaSqloggerNew(L *lua.LState, agent *fullMqttAgent) int { L.SetMetatable(ud, L.GetTypeMetatable("sqlogger")) L.Push(ud) return 1 + } else if logger, found := agent.oldLoggers[arg]; found { + agent.loggers[arg] = logger + ud := L.NewUserData() + ud.Value = logger + L.SetMetatable(ud, L.GetTypeMetatable("sqlogger")) + L.Push(ud) + return 1 } else if logger, err := connect(arg); err != nil { log.Println(err) L.Push(lua.LNil)