Skip to content

Commit

Permalink
refactor(server/cron): allow getNextTime minute and hour "overflow"
Browse files Browse the repository at this point in the history
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 overextended#368.
  • Loading branch information
thelindat committed Jul 17, 2023
1 parent 9e2076f commit 8488dec
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions imports/cron/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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+')
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8488dec

Please sign in to comment.