Unable to modify cron schedule without redeploying
What problem are you trying to solve?
I am working on a project where I would like to use the Deno Cron functionality, but I encountered a limitation. You cannot modify or stop a cron job once it has been set. There may be a way to do this, but I couldn't figure it out.
Describe the solution you'd like
It would be really useful if you could at least stop a cron job, allowing you to create a new one. I have two examples here of how this could look like.
Example 1
const task = Deno.cron("Cron job", "*/30 * * * * *", () => { ... });
task.stop()
Example 2
const task = Deno.stopCron("Cron job")
Describe alternatives you've considered
I tried changing the schedule by setting one with the same name, but then resulted in this error: Uncaught TypeError: Cron with this name already exists. I will also be checking out Croner to see if it gets the job done for me.
Deno.cron("Cron job", "*/30 * * * * *", () => {
console.log("Cron job ran, every 30 seconds");
});
const changeCron = () => {
Deno.cron("Cron job, "*/45 * * * *", () => {
console.log("Cron job ran, every 45 seconds");
});
};
Documentation, Adoption, Migration Strategy
No response
According to documentation https://docs.deno.com/api/deno/~/Deno.cron#parameters the Overload 2 version of parameters is accepted abort signal. So may be solution to stop somewhen the cron will be something like:
const cronController = new AbortController();
Deno.cron(
'tik-tak until boom',
{ minute: { every: 1 } },
{ signal: cronController.signal }, // possible solution
() => console.log('tik-tak'),
);
function boom() {
cronController.abort('boom!');
console.info('cron is off now');
}
setTimeout(
boom,
1000 * 60 * 5,
);