From 8488dec3d3e678dfc10593c3da8d9b39bb423a7f Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:07:30 +1000 Subject: [PATCH] refactor(server/cron): allow getNextTime minute and hour "overflow" Per the Lua manual, "if sec is -10, it means 10 seconds before the time specified by the other fields". i.e. { min = 61, hour = 0 } is equal to { min = 1, hour = 1 } Should improve scheduling and help with #368. --- imports/cron/server.lua | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/imports/cron/server.lua b/imports/cron/server.lua index 4bfbb9c..8daa807 100644 --- a/imports/cron/server.lua +++ b/imports/cron/server.lua @@ -67,7 +67,7 @@ local function getTimeUnit(value, unit) if currentTime >= min and currentTime <= max then return currentTime end - return min + return min + unitMax end local list = string.match(value, '%d+,%d+') @@ -83,13 +83,17 @@ local function getTimeUnit(value, unit) end -- if iterator failed, return the first value in the list - return tonumber(string.match(value, '%d+')) + return tonumber(string.match(value, '%d+')) + unitMax end return false end - return value or currentTime + if value then + return value < currentTime and value + unitMax or value + end + + return currentTime end ---Get a timestamp for the next time to run the task today. @@ -124,8 +128,8 @@ function OxTask:getNextTime() if not hour then return end return os.time({ - min = minute < 60 and minute or 0, - hour = hour < 24 and hour or 0, + min = minute, + hour = hour, day = day or currentDate.day, month = month or currentDate.month, year = currentDate.year, @@ -192,23 +196,20 @@ function OxTask:scheduleTask() return self:stop() end - if self.hour then - sleep += 86400 - elseif self.minute then - sleep += 3600 - end - - if sleep < 0 then - sleep += 60 - runAt += 60 - end + sleep += 60 end if self.debug then - print(('running task %s in %d seconds (%0.2f minutes or %0.2f hours)'):format(self.id, sleep, sleep / 60, sleep / 60 / 60)) + print(('running task %s in %d seconds (%0.2f minutes or %0.2f hours)'):format(self.id, sleep, sleep / 60, + sleep / 60 / 60)) end - if sleep > 0 then Wait(sleep * 1000) end + if sleep > 0 then + Wait(sleep * 1000) + else -- will this even happen? + Wait(1000) + return true + end if self.isActive then self:job(currentDate)