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

Programmatically stop croner #26

Closed
davoam opened this issue Dec 22, 2021 · 6 comments
Closed

Programmatically stop croner #26

davoam opened this issue Dec 22, 2021 · 6 comments
Assignees
Labels

Comments

@davoam
Copy link

davoam commented Dec 22, 2021

Thanks for the library!

Is there any way to programmatically stop croner? My Jest tests hangs until I manually kill tests process

@Hexagon
Copy link
Owner

Hexagon commented Dec 22, 2021

Yeah, you can stop it from both "inside" and "outside".

const job = Cron('xxx', (_job) => { /* option 1 */ _job.stop();});

// Option 2
job.stop();

Works? :)

See internal test suite for examples of promise handling https://github.com/Hexagon/croner/blob/master/test/src/suite.cjs

@davoam
Copy link
Author

davoam commented Dec 22, 2021

Thanks for the quick response! Yeah, I stopped all the jobs this way, but the process itself keeps running. If there are no running jobs, croner stops the process?

It seems like there is a pending setTimeout or Promise which prevents the process from being killed

@Hexagon
Copy link
Owner

Hexagon commented Dec 22, 2021

Hmm, croner does not fork processes or anything, it's basically a setTimeout, where .stop() run clearTimeout. Are you sure your scheduled function does not leave anything running?

If croner causes this, it would be awesome if you could pinpoint the problem and paste some code here, so i can have a look at it :)

@davoam
Copy link
Author

davoam commented Dec 22, 2021

Hmm, croner does not fork processes or anything, it's basically a setTimeout, where .stop() run clearTimeout. Are you sure your scheduled function does not leave anything running?

If croner causes this, it would be awesome if you could pinpoint the problem and paste some code here, so i can have a look at it :)

I'll try, when I have time :) Thanks!

@Hexagon
Copy link
Owner

Hexagon commented Dec 22, 2021

Great, tried to reproduce it using different techniques, but this is the only one that finish the scheduled function, but keep node running "unexpectedly".

const Cron = require("croner");

async function runsToInfinity() { setTimeout(runsToInfinity, 1000); }

const job = Cron('* * * * * *', async (self) => {

    // No await
    runsToInfinity();

    // Option 1, will not stop execution
    console.log('Yay, done!');
    self.stop();
} );

// Option 2 (wait 2 seconds to make sure execution is started)
// ... will not stop execution either
setTimeout( () => { 
    console.log('Yay, done again!'); 
    job.stop(); 
}, 2000);

@Hexagon
Copy link
Owner

Hexagon commented Jan 5, 2022

Closing this, since there are no steps to reproduce.

Please reopen if you find and document a way to reproduce this

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

No branches or pull requests

2 participants