1

So, I found an old question about this (here: How to stop a node cron job) but it doesn't work for me (and I can't realize the reason why). I'm using cron-job-manager (since I plan to use more than one scheduled job at the same time) but as far as I know it should be built on node-cron (but I'm a newbie, so...)

So, I'm asking again: how do I deal with starting and stopping a cron job under certain conditions? I'm actually doing this for a discord bot: basically, the user input a command for starting and one for stopping.

First try was something like:

job = cron('keyTask','* * * * * *', ()=>{
  //Do the actual job
  },
  {
  start: false, 
  timeZone:"Europe/London",
  });

switch args[1]
case go: 
  job.start();
  break;
case stop:
  job.stop();
  break;

This start the job successfully when the user gives the 'go' command, but when it gives the 'stop' command, the scheduled job just won't stop.

Second try is:

  var x = args [1];
  new cron('keyTask' , '* * * * * *', job(doTheThing(x)) ,
  {
  start: false, 
  timeZone:"Europe/London",
  });

Where job() is a function defined beforehand that contains the actual job and DoTheThing() is something that gives true or false depending on what the user is saying in input. This executes the scheduled job once and then stops. Somehow I suspect that the issue here is related to the fact that I'm defining function externally, while in most examples I saw the function is always written within the cron(). So, I'm out of ideas: thanks in advance for any help!

0 Answers0