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

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
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.

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

@juddisjudd
Copy link
Member

@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
Member

@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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants