@@ -27,6 +27,7 @@ setmetatable(currentDate, {
27
27
--- @field id number
28
28
--- @field debug ? boolean
29
29
--- @field lastRun ? number
30
+ --- @field maxDelay ? number Maximum allowed delay in seconds before skipping (0 to disable )
30
31
31
32
--- @class OxTask : OxTaskProperties
32
33
--- @field expression string
@@ -284,7 +285,7 @@ function OxTask:getNextTime()
284
285
285
286
if self .lastRun and nextTime - self .lastRun < 60 then
286
287
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 (
288
289
self .id ,
289
290
os.date (' %c' , self .lastRun ),
290
291
os.date (' %c' , nextTime )
@@ -353,27 +354,39 @@ function OxTask:scheduleTask()
353
354
local sleep = runAt - currentTime
354
355
355
356
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
357
370
end
358
371
359
372
local timeAsString = self :getTimeAsString (runAt )
360
373
361
374
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 ,
363
376
sleep / 60 ,
364
377
sleep / 60 / 60 ))
365
378
end
366
379
367
380
if sleep > 0 then
368
381
Wait (sleep * 1000 )
369
382
else
370
- Wait (1000 )
383
+ Wait (0 )
371
384
return true
372
385
end
373
386
374
387
if self .isActive then
375
388
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 ))
377
390
end
378
391
379
392
Citizen .CreateThreadNow (function ()
@@ -400,10 +413,10 @@ function OxTask:stop(msg)
400
413
401
414
if self .debug then
402
415
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 ))
404
417
end
405
418
406
- print ((' stopping task %s' ):format (self .id ))
419
+ lib . print . debug ((' stopping task %s' ):format (self .id ))
407
420
end
408
421
end
409
422
413
426
--- Creates a new [cronjob](https://en.wikipedia.org/wiki/Cron), scheduling a task to run at fixed times or intervals.
414
427
--- Supports numbers, any value `*`, lists `1,2,3`, ranges `1-3`, and steps `*/4`.
415
428
--- 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)
416
430
function lib .cron .new (expression , job , options )
417
431
if not job or type (job ) ~= ' function' then
418
432
error ((" expected job to have type 'function' (received %s)" ):format (type (job )))
@@ -431,6 +445,7 @@ function lib.cron.new(expression, job, options)
431
445
task .id = # tasks + 1
432
446
task .job = job
433
447
task .lastRun = nil
448
+ task .maxDelay = task .maxDelay or 1
434
449
tasks [task .id ] = task
435
450
task :run ()
436
451
0 commit comments