Issue
I'm trying to write a discord bot in discord.js that lets the user schedule payouts to occur every X(1-730) hours from a given start hour of the day(0-23). I wanted to use cron times via node-schedule, but what if the user wanted something like every 77 hours?
Currently, the object i'm saving from the commands to the db is this:
const jobParams =
{ name: 'Job Title',
currencyName: 'Credits',
interval: '77',
amount: '30',
startHour: 5,
member: '119351283999047682',
role: null };
The end goal here is to have a system that will schedule the job on startup to give the supplied member, 30 "Credits" every 77 hours (every 3 days 5 hours) with the counting from the next 0500. The payment transaction is ready to go.
The users will also need to be able to cancel jobs on demand.
Solution
You can use steps in cron times. */77
is analogous to "every 77th." With this concept, you can start a cron job to run every 77th hour through another job that fires at the next 5 am (only once).
const { CronJob } = require('cron');
const foo = () => console.log('Hello, world!');
const job = new CronJob('0 */77 * * *', foo);
new CronJob('0 5 * * *', () => {
job.start();
this.stop();
}, null, true);
Alternatively, you could have a cron job fire at every 5 am, and set a timeout to execute your code in 77 hours.
const job = new CronJob('0 5 * * *', () => setTimeout(foo, 1000 * 60 * 60 * 77), null, true);
Answered By - slothiful