mango/elee
mango/elee6mo ago

Programmatically invoking and cancelling a cron

So... What I was wondering was whether we could have a way to programmatically schedule a cron when needed and then cancel it and clean it up when done for example let's say at 6 am I want a function to start/have it's first invocation via an api call and then it checks something a db value or calls another api every 4 minutes, does this for 30 minutes or until desired result is achieved and then is cancelled one potential alternative is to from a function schedule a recursive action which checks if time is over 30 minutes or desired result has been achieved if not it schedules itself for after 4 minutes and does this recursively➕ so while it can be solved for some reason the cron approach seems better especially for example if someone wants to run a function each 5 seconds but not infinitely and only when triggered what do others feel?
2 Replies
lee
lee6mo ago
I would suggest using a recursively scheduled action/mutation for this, since it offers the most flexibility. Crons are essentially a control layer on top of the scheduler, providing more ease of configuration while restricting options. The ease of configuration is mostly about starting the initial job, and making sure it continues running forever. It sounds like you don't need either of those. For an example of implementing cron-like semantics with scheduling, check out https://www.cronvex.com/
mango/elee
mango/eleeOP6mo ago
Yeaa I agree! And cronvex looks cool I didn't know about it, went through the code and it's mostly what I had in mind My thought was more around having something like this in built where the heavy lifting is already done of scheduling and cancelling Thanks!!