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

Cancel job whose object the script does not have access to #544

Open
Huy-Ngo opened this issue Aug 31, 2020 · 6 comments
Open

Cancel job whose object the script does not have access to #544

Huy-Ngo opened this issue Aug 31, 2020 · 6 comments

Comments

@Huy-Ngo
Copy link

Huy-Ngo commented Aug 31, 2020

Let's say I ran this script to schedule a job:

var j = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function(){
  console.log('Time for tea!');
});

Now, I write a new script to cancel the job above, which doesn't have access to the j that I created above, so I can't just j.cancel(). Is there a way that I can cancel the job above?

@Huy-Ngo
Copy link
Author

Huy-Ngo commented Aug 31, 2020

I was thinking about some solution like having j.id saved somewhere then get it back by calling j = schedule.getById('job id here') but I don't know if it's an available feature.

@iiagodias
Copy link

I had that in mind, it really is the solution. We could save the ID in the database and cancel it whenever we want.

@peterdanis
Copy link

You can access all registered jobs via scheduledJobs object. In my case I use an id as name passed as first parameter to scheduleJob function.

@zoom2009
Copy link

zoom2009 commented Mar 2, 2021

You can access all registered jobs via scheduledJobs object. In my case I use an id as name passed as first parameter to scheduleJob function.

You save my life 👯

@Sidahmed02
Copy link

This way worked for me. by passing the scheduled job to cancelJob function.

const updateReports = schedule.scheduleJob("*/5 * * * * *", function() {
console.log("Job is working .......");
});

function myFunc(arg) {
console.log("job is going to be canceled..");
schedule.cancelJob(updateReports); //passing job as an argument
}
setTimeout(myFunc, 15000, "funky");

@gustavogialim
Copy link

You can access all registered jobs via scheduledJobs object. In my case I use an id as name passed as first parameter to scheduleJob function.

Worked well here, I think the information about the scheduledJobs array should be present on the repository README/Docs.

So other people can save time trying to access the jobs that was already scheduled, what do you guys think?
If you agree I can collaborate on creating a PR changing the README.md

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

6 participants