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

Question: Is it possible to target a specific job to stop rather than to stop the whole scheduler? #12

Closed
mattiaslvv opened this issue May 16, 2021 · 11 comments

Comments

@mattiaslvv
Copy link

const { ToadScheduler, SimpleIntervalJob, AsyncTask } = require('toad-scheduler')

const scheduler = new ToadScheduler()

const task = new AsyncTask(
    'simple task', 
    () => { return db.pollForSomeData().then((result) => { /* continue the promise chain */ }) },
    (err: Error) => { /* handle error here */ }
)
const job = new SimpleIntervalJob({ seconds: 20, }, task)

scheduler.addSimpleIntervalJob(job)

// when stopping your app
scheduler.stop()

New to toad-scheduler. Is there a way to target a specific job, for termination? scheduler.stop() seems to end all running jobs.

@kibertoad
Copy link
Owner

@jenseneducation-mattias-von-vogelsang There is a .stop() method on job itself, would that suffice?

@mattiaslvv
Copy link
Author

mattiaslvv commented May 16, 2021

Sounds promising. However, ideally I want to be able to terminate the job from the tasks error handler. For example via scheduler.stop('job1').

@kibertoad
Copy link
Owner

kibertoad commented May 16, 2021

E. g.:

const { ToadScheduler, SimpleIntervalJob, AsyncTask } = require('toad-scheduler')

const scheduler = new ToadScheduler()

const task = new AsyncTask(
    'simple task', 
    () => { return db.pollForSomeData().then((result) => { /* continue the promise chain */ }) },
    (err: Error) => { /* handle error here */ }
)
const job = new SimpleIntervalJob({ seconds: 20, }, task)

scheduler.addSimpleIntervalJob(job)

// stopping specific job
job.stop()

It is not possible to stop job from the task, by design, because task is a child of job, and as such is not aware of its existence.
Your suggested option seems to be the way to go, I'll add support for that.

@mattiaslvv
Copy link
Author

mattiaslvv commented May 16, 2021

Thank you kindly for your example. Unfortunately It would not help me in my use case.

I would like that very much, it would fit my specific use case perfectly. Thank you!

@kibertoad
Copy link
Owner

will release a new version later tonight

@mattiaslvv
Copy link
Author

Thank you kindly for your implementation. I'll try it out tomorrow. Closing this.

@kibertoad
Copy link
Owner

kibertoad commented May 16, 2021

@jenseneducation-mattias-von-vogelsang Just published 1.2.0 with the stopping/starting functionality.

@mattiaslvv
Copy link
Author

@kibertoad Awesome. I just got home, I'll implement it in my code.

@mattiaslvv
Copy link
Author

@kibertoad I tried to push a branch for a PR on fastify-schedule to bump toad-scheduler to 1.2 but got a 403. Could you bump it or give me temporary write access to push the branch?

@kibertoad
Copy link
Owner

@jenseneducation-mattias-von-vogelsang Isn't usual GitHub flow to fork a repository and then create a PR from that fork? Also since fastify-schedule doesn't come with package-lock, I believe updating locally should be sufficient.

@mattiaslvv
Copy link
Author

Ah! My bad, I'm new to "contributing" to open source.
I'll do it locally. Thanks!

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

No branches or pull requests

2 participants