Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server/cron): add allowExpiredTasks to execute overdue tasks #705

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions imports/cron/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ setmetatable(currentDate, {
---@field id number
---@field debug? boolean
---@field lastRun? number
---@field allowExpiredTasks? boolean

---@class OxTask : OxTaskProperties
---@field expression string
Expand Down Expand Up @@ -353,7 +354,14 @@ function OxTask:scheduleTask()
local sleep = runAt - currentTime

if sleep < 0 then
return self:stop(self.debug and ('scheduled time expired %s seconds ago'):format(-sleep))
if self.allowExpiredTasks then
if self.debug then
print(('Task %s is slightly overdue by %d seconds, but will execute due to allowExpiredTasks'):format(self.id, -sleep))
end
sleep = 1 -- hacky way to force the task to run
else
return self:stop(self.debug and ('scheduled time expired %s seconds ago'):format(-sleep))
end
end

local timeAsString = self:getTimeAsString(runAt)
Expand Down Expand Up @@ -409,7 +417,7 @@ end

---@param expression string A cron expression such as `* * * * *` representing minute, hour, day, month, and day of the week.
---@param job fun(task: OxTask, date: osdate)
---@param options? { debug?: boolean }
---@param options? { debug?: boolean, allowExpiredTasks?: boolean }
---Creates a new [cronjob](https://en.wikipedia.org/wiki/Cron), scheduling a task to run at fixed times or intervals.
---Supports numbers, any value `*`, lists `1,2,3`, ranges `1-3`, and steps `*/4`.
---Day of the week is a range of `1-7` starting from Sunday and allows short-names (i.e. sun, mon, tue).
Expand Down