diff --git a/.gitignore b/.gitignore index 5e3c3eb..ca9e6ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.log *.servers.lua examples/*/itte*.lua !hellobot.servers.lua diff --git a/examples/ramenkan/ramenkan.config.lua b/examples/ramenkan/ramenkan.config.lua index aa75da4..6c8b8f2 100644 --- a/examples/ramenkan/ramenkan.config.lua +++ b/examples/ramenkan/ramenkan.config.lua @@ -369,7 +369,7 @@ end -- --------------------------------------------------------------------------- itte_config = { - debug = false, + debug = true, messages = { help = "一、二、三、らーめん缶! Hello, I am a ramen vending machine. " .. "Please type a code for service: {{codes}} " .. diff --git a/itte.lua b/itte.lua index de80eb2..1788b32 100644 --- a/itte.lua +++ b/itte.lua @@ -58,7 +58,7 @@ itte.config = { send = { "send" }, svrs_not_found = { "config", "Error: servers not found." }, 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] -- 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() util.debug(itte.config.debugs.listen[1], str, itte.config.debug) 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) end end + + -- Output to stdout anyway if debug is enabled + else + util.debug(itte.config.debugs.listen[1], str, itte.config.debug) end end @@ -886,13 +890,6 @@ function itte.add_listener(cxt) if data ~= nil then 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 itte.disconnect_server(cxt.name) end @@ -914,7 +911,6 @@ function itte.add_tasks(cxt) 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) - task_str = string.gsub(task_str, "{{ts}}", ts) util.debug(itte.config.debugs.task_activated[1], task_str, itte.config.debug) 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())) -- 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 + if task.time == nil then task.time = "00:00" end local task_min = string.sub(task.time, string.find(task.time, ":") + 1, string.find(task.time, ":") + 2) 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.time == dt[5] .. ":" .. dt[6]) 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) + + 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 + task.co = coroutine.create(co) end + else -- Reset the done flag after running task task.done = false diff --git a/itteutil.lua b/itteutil.lua index 63b7b9e..f3f30e9 100644 --- a/itteutil.lua +++ b/itteutil.lua @@ -256,7 +256,8 @@ itteutil.docs.debug = [[ (tag_str, debug_str [, enabled_bool]) ]] function itteutil.debug(tag, str, enabled) if enabled then - print("[" .. tag .. "] " .. str) + local ts = os.date("%Y-%m-%d %H:%M:%S", os.time()) + print("[" .. ts .. "][" .. tag .. "] " .. str) end end