Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit a497880

Browse files
authored
feat(cron): add maxDelay property to control task execution delay (#708)
1 parent cbca5b7 commit a497880

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

imports/cron/server.lua

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ setmetatable(currentDate, {
2727
---@field id number
2828
---@field debug? boolean
2929
---@field lastRun? number
30+
---@field maxDelay? number Maximum allowed delay in seconds before skipping (0 to disable)
3031

3132
---@class OxTask : OxTaskProperties
3233
---@field expression string
@@ -284,7 +285,7 @@ function OxTask:getNextTime()
284285

285286
if self.lastRun and nextTime - self.lastRun < 60 then
286287
if self.debug then
287-
print(('Preventing duplicate execution of task %s - Last run: %s, Next scheduled: %s'):format(
288+
lib.print.debug(('Preventing duplicate execution of task %s - Last run: %s, Next scheduled: %s'):format(
288289
self.id,
289290
os.date('%c', self.lastRun),
290291
os.date('%c', nextTime)
@@ -353,27 +354,39 @@ function OxTask:scheduleTask()
353354
local sleep = runAt - currentTime
354355

355356
if sleep < 0 then
356-
return self:stop(self.debug and ('scheduled time expired %s seconds ago'):format(-sleep))
357+
if not self.maxDelay or -sleep > self.maxDelay then
358+
return self:stop(self.debug and ('scheduled time expired %s seconds ago'):format(-sleep))
359+
end
360+
361+
if self.debug then
362+
lib.print.debug(('Task %s is %s seconds overdue, executing now due to maxDelay=%s'):format(
363+
self.id,
364+
-sleep,
365+
self.maxDelay
366+
))
367+
end
368+
369+
sleep = 0
357370
end
358371

359372
local timeAsString = self:getTimeAsString(runAt)
360373

361374
if self.debug then
362-
print(('(%s) task %s will run in %d seconds (%0.2f minutes / %0.2f hours)'):format(timeAsString, self.id, sleep,
375+
lib.print.debug(('(%s) task %s will run in %d seconds (%0.2f minutes / %0.2f hours)'):format(timeAsString, self.id, sleep,
363376
sleep / 60,
364377
sleep / 60 / 60))
365378
end
366379

367380
if sleep > 0 then
368381
Wait(sleep * 1000)
369382
else
370-
Wait(1000)
383+
Wait(0)
371384
return true
372385
end
373386

374387
if self.isActive then
375388
if self.debug then
376-
print(('(%s) running task %s'):format(timeAsString, self.id))
389+
lib.print.debug(('(%s) running task %s'):format(timeAsString, self.id))
377390
end
378391

379392
Citizen.CreateThreadNow(function()
@@ -400,10 +413,10 @@ function OxTask:stop(msg)
400413

401414
if self.debug then
402415
if msg then
403-
return print(('stopping task %s (%s)'):format(self.id, msg))
416+
return lib.print.debug(('stopping task %s (%s)'):format(self.id, msg))
404417
end
405418

406-
print(('stopping task %s'):format(self.id))
419+
lib.print.debug(('stopping task %s'):format(self.id))
407420
end
408421
end
409422

@@ -413,6 +426,7 @@ end
413426
---Creates a new [cronjob](https://en.wikipedia.org/wiki/Cron), scheduling a task to run at fixed times or intervals.
414427
---Supports numbers, any value `*`, lists `1,2,3`, ranges `1-3`, and steps `*/4`.
415428
---Day of the week is a range of `1-7` starting from Sunday and allows short-names (i.e. sun, mon, tue).
429+
---@note maxDelay: Maximum allowed delay in seconds before skipping (0 to disable)
416430
function lib.cron.new(expression, job, options)
417431
if not job or type(job) ~= 'function' then
418432
error(("expected job to have type 'function' (received %s)"):format(type(job)))
@@ -431,6 +445,7 @@ function lib.cron.new(expression, job, options)
431445
task.id = #tasks + 1
432446
task.job = job
433447
task.lastRun = nil
448+
task.maxDelay = task.maxDelay or 1
434449
tasks[task.id] = task
435450
task:run()
436451

0 commit comments

Comments
 (0)