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

Feature request: onComplete callback #130

Closed
cjohansen11 opened this issue Aug 17, 2022 · 5 comments
Closed

Feature request: onComplete callback #130

cjohansen11 opened this issue Aug 17, 2022 · 5 comments

Comments

@cjohansen11
Copy link

This is more of a question and/or possible feature request. Does Croner have an onComplete type callback ability? Essentially I'm looking to run this job x amount of times using maxRuns however on the last run if my tasks are not complete I will need to perform other tasks. So without keeping an internal count of the runs and then running if it hits its max I would like to use the maxRuns option and then pass another callback function to on if maxRuns has reached it's end. Thanks!

@Hexagon
Copy link
Owner

Hexagon commented Aug 17, 2022

There is no such functionality right now. But there is a simple solution to try, which might or might not work for your use case.

The job will be passed as the first parameter to the triggered function, use that and check for if(job.next()===null) // this is the last run.

Disclaimer: Totally untested 😄

@Hexagon Hexagon changed the title onComplete callback Feature request: onComplete callback Aug 17, 2022
@cjohansen11
Copy link
Author

Ah great, that seems like it should work. I was also going to try using job.options.maxRuns === 0 which could have worked too. Appreciate the quick feedback!!

@Hexagon
Copy link
Owner

Hexagon commented Aug 17, 2022

Cool! Keep us updated on which path you choose, and leave the issue open.

@Hexagon
Copy link
Owner

Hexagon commented Aug 23, 2022

I've looked into this a bit deeper now. I actually think one of the simplest and cleanest solutions would be to implement this where it's needed. Something like:

import Cron from "croner";

const job = new Cron("0/5 * * * * *", { maxRuns: 2 }, (job) => {
    
    // Do work
    console.log('Job Running');

    // Is this the last execution?
    if (!job.next()) {
        console.log('Last execution');
    }

});
 
// Will there be no executions?
if (!job.next() && !job.previous()) {
    console.log('No executions scheduled');
}

@Hexagon
Copy link
Owner

Hexagon commented Aug 24, 2022

Fixed by new example in docs at https://github.com/Hexagon/croner#act-at-completion

@Hexagon Hexagon closed this as completed Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants