Fix task repetition
- Fix task scheduling not repeating and resulting in high cpu usage - Fix pong response triggered by user message - Add timestamp to debug outputtrunk
parent
971b758524
commit
de2643183f
|
@ -1,3 +1,4 @@
|
||||||
|
*.log
|
||||||
*.servers.lua
|
*.servers.lua
|
||||||
examples/*/itte*.lua
|
examples/*/itte*.lua
|
||||||
!hellobot.servers.lua
|
!hellobot.servers.lua
|
||||||
|
|
|
@ -369,7 +369,7 @@ end
|
||||||
-- ---------------------------------------------------------------------------
|
-- ---------------------------------------------------------------------------
|
||||||
|
|
||||||
itte_config = {
|
itte_config = {
|
||||||
debug = false,
|
debug = true,
|
||||||
messages = {
|
messages = {
|
||||||
help = "一、二、三、らーめん缶! Hello, I am a ramen vending machine. " ..
|
help = "一、二、三、らーめん缶! Hello, I am a ramen vending machine. " ..
|
||||||
"Please type a code for service: {{codes}} " ..
|
"Please type a code for service: {{codes}} " ..
|
||||||
|
|
42
itte.lua
42
itte.lua
|
@ -58,7 +58,7 @@ itte.config = {
|
||||||
send = { "send" },
|
send = { "send" },
|
||||||
svrs_not_found = { "config", "Error: servers not found." },
|
svrs_not_found = { "config", "Error: servers not found." },
|
||||||
task_added = { "task", "Task `{{name}}` added." },
|
task_added = { "task", "Task `{{name}}` added." },
|
||||||
task_activated = { "task", "Task `{{name}}` activated at {{ts}}." },
|
task_activated = { "task", "Task `{{name}}` activated." },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -836,7 +836,7 @@ function itte.listen(name, str)
|
||||||
local cxt = itte.contexts[name]
|
local cxt = itte.contexts[name]
|
||||||
|
|
||||||
-- Respond to server ping
|
-- Respond to server ping
|
||||||
if util.is_substr(str, cxt.cmds.ping.check) then
|
if string.find(str, cxt.cmds.ping.check) == 1 then
|
||||||
cxt.state.ping_time = os.time()
|
cxt.state.ping_time = os.time()
|
||||||
util.debug(itte.config.debugs.listen[1], str, itte.config.debug)
|
util.debug(itte.config.debugs.listen[1], str, itte.config.debug)
|
||||||
itte.send_command(cxt.con, string.gsub(str, cxt.cmds.ping.check,
|
itte.send_command(cxt.con, string.gsub(str, cxt.cmds.ping.check,
|
||||||
|
@ -871,6 +871,10 @@ function itte.listen(name, str)
|
||||||
itte.message(cxt, { msg.reply_to }, itte.config.errors.unknown_code)
|
itte.message(cxt, { msg.reply_to }, itte.config.errors.unknown_code)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Output to stdout anyway if debug is enabled
|
||||||
|
else
|
||||||
|
util.debug(itte.config.debugs.listen[1], str, itte.config.debug)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -886,13 +890,6 @@ function itte.add_listener(cxt)
|
||||||
if data ~= nil then
|
if data ~= nil then
|
||||||
itte.listen(cxt.name, data)
|
itte.listen(cxt.name, data)
|
||||||
|
|
||||||
-- Send a pong every 120s in case the ping request was lost, e.g. during high
|
|
||||||
-- system load. Servers may send a ping request every ~90s. Disconnects from
|
|
||||||
-- ping timeouts occur at ~2m30s.
|
|
||||||
elseif (cxt.state.ping_time > 0) and (delta > 120) then
|
|
||||||
itte.send_command(cxt.con, cxt.cmds.ping.resp)
|
|
||||||
cxt.state.ping_time = os.time()
|
|
||||||
|
|
||||||
elseif status == "closed" then
|
elseif status == "closed" then
|
||||||
itte.disconnect_server(cxt.name)
|
itte.disconnect_server(cxt.name)
|
||||||
end
|
end
|
||||||
|
@ -914,7 +911,6 @@ function itte.add_tasks(cxt)
|
||||||
local ts = os.date("%Y-%m-%d %H:%M:%S", os.time())
|
local ts = os.date("%Y-%m-%d %H:%M:%S", os.time())
|
||||||
local task_str = string.gsub(itte.config.debugs.task_activated[2],
|
local task_str = string.gsub(itte.config.debugs.task_activated[2],
|
||||||
"{{name}}", name)
|
"{{name}}", name)
|
||||||
task_str = string.gsub(task_str, "{{ts}}", ts)
|
|
||||||
util.debug(itte.config.debugs.task_activated[1], task_str,
|
util.debug(itte.config.debugs.task_activated[1], task_str,
|
||||||
itte.config.debug)
|
itte.config.debug)
|
||||||
itte.handlers[task.handler](cxt, task)
|
itte.handlers[task.handler](cxt, task)
|
||||||
|
@ -947,8 +943,9 @@ function itte.check_tasks(cxt)
|
||||||
|
|
||||||
local dt = util.split_str(os.date("%w %Y %m %d %H %M", os.time()))
|
local dt = util.split_str(os.date("%w %Y %m %d %H %M", os.time()))
|
||||||
-- Support preset minute intervals
|
-- Support preset minute intervals
|
||||||
local interval_min = { 5, 10, 15, 20, 30 }
|
local interval_min = { 2, 5, 10, 15, 20, 30 }
|
||||||
for name, task in pairs(cxt.tasks) do
|
for name, task in pairs(cxt.tasks) do
|
||||||
|
if task.time == nil then task.time = "00:00" end
|
||||||
local task_min = string.sub(task.time, string.find(task.time, ":") + 1,
|
local task_min = string.sub(task.time, string.find(task.time, ":") + 1,
|
||||||
string.find(task.time, ":") + 2)
|
string.find(task.time, ":") + 2)
|
||||||
if (util.has_key(interval_min, tonumber(task.interval:sub(1, -2))) and
|
if (util.has_key(interval_min, tonumber(task.interval:sub(1, -2))) and
|
||||||
|
@ -960,10 +957,29 @@ function itte.check_tasks(cxt)
|
||||||
(task.interval == "monthly" and task.day == dt[4] and
|
(task.interval == "monthly" and task.day == dt[4] and
|
||||||
task.time == dt[5] .. ":" .. dt[6])
|
task.time == dt[5] .. ":" .. dt[6])
|
||||||
then
|
then
|
||||||
-- Run only once per interval
|
|
||||||
while not task.done do
|
-- Activate task if not already done in the current interval
|
||||||
|
if not task.done then
|
||||||
coroutine.resume(task.co, name, task)
|
coroutine.resume(task.co, name, task)
|
||||||
|
|
||||||
|
elseif (task.done) and (coroutine.status(task.co) == "dead") then
|
||||||
|
-- Reconstruct the task coroutine after it has been done
|
||||||
|
local co = function(name, task)
|
||||||
|
if util.has_key(itte.handlers, task.handler) then
|
||||||
|
local ts = os.date("%Y-%m-%d %H:%M:%S", os.time())
|
||||||
|
local task_str = string.gsub(itte.config.debugs.task_activated[2],
|
||||||
|
"{{name}}", name)
|
||||||
|
util.debug(itte.config.debugs.task_activated[1], task_str,
|
||||||
|
itte.config.debug)
|
||||||
|
itte.handlers[task.handler](cxt, task)
|
||||||
|
-- Suspend coroutine to be reactivated later
|
||||||
|
coroutine.yield(co)
|
||||||
|
task.done = true
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
task.co = coroutine.create(co)
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
-- Reset the done flag after running task
|
-- Reset the done flag after running task
|
||||||
task.done = false
|
task.done = false
|
||||||
|
|
|
@ -256,7 +256,8 @@ itteutil.docs.debug = [[ (tag_str, debug_str [, enabled_bool])
|
||||||
]]
|
]]
|
||||||
function itteutil.debug(tag, str, enabled)
|
function itteutil.debug(tag, str, enabled)
|
||||||
if enabled then
|
if enabled then
|
||||||
print("[" .. tag .. "] " .. str)
|
local ts = os.date("%Y-%m-%d %H:%M:%S", os.time())
|
||||||
|
print("[" .. ts .. "][" .. tag .. "] " .. str)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue