mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
Merge pull request #1850 from seltonmt012/fix/cron-midnight-catchup
fix(cron): catch up jobs that were missed across midnight
This commit is contained in:
@@ -26,17 +26,25 @@ end
|
|||||||
|
|
||||||
---@param timestamp number
|
---@param timestamp number
|
||||||
function OnTime(timestamp)
|
function OnTime(timestamp)
|
||||||
for i = 1, #cronJobs, 1 do
|
local function scheduledAt(job, dayOffset)
|
||||||
local scheduledTimestamp = os.time({
|
return os.time({
|
||||||
hour = cronJobs[i].h,
|
hour = job.h,
|
||||||
min = cronJobs[i].m,
|
min = job.m,
|
||||||
sec = 0, -- Assuming tasks run at the start of the minute
|
sec = 0, -- Assuming tasks run at the start of the minute
|
||||||
day = os.date("%d", timestamp),
|
day = os.date("%d", timestamp) + dayOffset,
|
||||||
month = os.date("%m", timestamp),
|
month = os.date("%m", timestamp),
|
||||||
year = os.date("%Y", timestamp),
|
year = os.date("%Y", timestamp),
|
||||||
})
|
})
|
||||||
|
end
|
||||||
|
|
||||||
if timestamp >= scheduledTimestamp and (not lastTimestamp or lastTimestamp < scheduledTimestamp) then
|
for i = 1, #cronJobs, 1 do
|
||||||
|
local scheduledTimestamp = scheduledAt(cronJobs[i], 0)
|
||||||
|
|
||||||
|
if scheduledTimestamp > timestamp then
|
||||||
|
scheduledTimestamp = scheduledAt(cronJobs[i], -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not lastTimestamp or lastTimestamp < scheduledTimestamp then
|
||||||
local d = os.date('*t', scheduledTimestamp).wday
|
local d = os.date('*t', scheduledTimestamp).wday
|
||||||
cronJobs[i].cb(d, cronJobs[i].h, cronJobs[i].m)
|
cronJobs[i].cb(d, cronJobs[i].h, cronJobs[i].m)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user