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

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

Closed
st860923 wants to merge 1 commit intooverextended:masterfrom
st860923:master
Closed

feat(server/cron): add allowExpiredTasks to execute overdue tasks#705
st860923 wants to merge 1 commit intooverextended:masterfrom
st860923:master

Conversation

@st860923
Copy link

@st860923 st860923 commented Feb 5, 2025

Introduces a new allowExpiredTasks option for cron jobs, allowing scheduled tasks
to execute even if their scheduled time has already passed.

Previously, tasks that were slightly overdue (due to server hitches or thread
delays) were skipped, leading to inconsistencies in scheduling. With this change,
if allowExpiredTasks = true, tasks can still execute when overdue.

Changes:

  • Added allowExpiredTasks as an optional parameter in lib.cron.new.

Introduces a new `allowExpiredTasks` option for cron jobs, allowing scheduled tasks
to execute even if their scheduled time has already passed.

Previously, tasks that were slightly overdue (due to server hitches or thread
delays) were skipped, leading to inconsistencies in scheduling. With this change,
if `allowExpiredTasks = true`, tasks can still execute when overdue.

Changes:
- Added `allowExpiredTasks` as an optional parameter in `lib.cron.new`.
@thelindat
Copy link
Member

Not really a suitable fix.

@juddisjudd Do you think we can remove this check outright now? It was there to cover for some edge cases where jobs were incorrectly scheduled.

@juddisjudd
Copy link
Collaborator

Not really a suitable fix.

@juddisjudd Do you think we can remove this check outright now? It was there to cover for some edge cases where jobs were incorrectly scheduled.

I will take a look and see what would be best for this.

@juddisjudd
Copy link
Collaborator

@thelindat I think maybe this would be a better approach:
Add a maxDelay option:
---@field maxDelay? number Maximum allowed delay in seconds before skipping (default: 0)

if sleep < 0 then
    if not self.maxDelay or -sleep > self.maxDelay then
        return self:stop(self.debug and ('scheduled time expired %s seconds ago'):format(-sleep))
    end
    
    if self.debug then
        print(('Task %s is delayed by %s seconds, executing now'):format(self.id, -sleep))
    end
    
    sleep = 0
end

Example usage then setting something like this:
maxDelay = 30 for a 30 second grace period or something

@thelindat
Copy link
Member

Sure we can go with that, maybe default to 1 second.

While we're at it, it would be good to swap to lib.print.debug and default debug to true if printLevel is set to debug. See #593

@juddisjudd
Copy link
Collaborator

@st860923 would you like to update your PR with my suggestion or would you prefer that I do this?

@st860923
Copy link
Author

You can proceed with your suggested approach. Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants