X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/c58ad4fb6f2c4c350a6aae01b100ebb4564ad8e9..5f23c8a7c56799750221a9c2d8e904e154f8c515:/bin/weathermon diff --git a/bin/weathermon b/bin/weathermon index d16f871..fdf092a 100755 --- a/bin/weathermon +++ b/bin/weathermon @@ -1,6 +1,7 @@ #!/usr/bin/lua local json = require("json") +local signal = require("posix.signal") require "wm_util" @@ -100,12 +101,7 @@ function submitValue(type,id,param,val) url = url:gsub("//","//"..web_user..":"..web_pass.."@",1) end - local result,code = http.request ({ - url=url, create=function() - local req_sock = socket.tcp() - req_sock:settimeout(web_timeout) - return req_sock - end}) + local result,code = http.request (url) if code ~= 200 and backlog_con then printLog("writing record to backlog...") @@ -186,7 +182,10 @@ function processJson(str) return '{'..name..'}' end end) - mqtt_client:publish(mqtt_path,v) + if not mqtt_client:socket() then + mqtt_client:reconnect() + end + mqtt_client:publish(mqtt_path,v,0,false) mqtt_client:loop() end end @@ -249,7 +248,10 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:publish(mqtt_path,v) + if not mqtt_client:socket() then + mqtt_client:reconnect() + end + mqtt_client:publish(mqtt_path,v,0,false) mqtt_client:loop() end end @@ -290,7 +292,10 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:publish(mqtt_path,msg_body) + if not mqtt_client:socket() then + mqtt_client:reconnect() + end + mqtt_client:publish(mqtt_path,msg_body,0,false) mqtt_client:loop() end if alarm_exec then @@ -309,6 +314,19 @@ end getConfig(arg[1]) +signal.signal(signal.SIGTERM, function(signum) + + printLog("Terminating...") + local pids = get_children() + for k,v in pairs(pids) do + printLog("Terminating subprocess "..tostring(v).."...") + signal.kill(v,signal.SIGTERM) + end + printLog("Exiting...") + os.exit(0) + +end) + if backlogdb or logdb then local dbdriver = require "luasql.sqlite3" env = assert(dbdriver.sqlite3()) @@ -333,7 +351,7 @@ end if web_url then http = require("socket.http") - socket = require("socket") + http.TIMEOUT = web_timeout end if mqtt_host then