how to detect when NodeJs schedule's last job execution occurs?

2.2k views Asked by At

is there a way to check when the last job execution, i need to delete the job from the databse when the scheduled job ends.

thanks in advance.

here is the code of creating the job:


    const job = schedule.scheduleJob(
      {
        start: scheduledJob.startTime,
        end: scheduledJob.endTime,
        rule: cronExp,
      },
      () => {
        this.jobService.send(scheduledJob);
        console.log("sent notification");
      }
    );

2

There are 2 answers

0
JΛYDΞV On

_      I tried to write this inside of a comment but a comment was too small and I needed more MD support to show how to do what your asking. In truth you didn't give a whole lot to go on, but knowing the API gives me a lot more insight than before I knew what API you were working with._


      The NPM registered Node.js Module Node-Schedule is a pretty frequently used API, this is largely to the fact that it is written with decent code, and its author keeps it well maintained, but the most likley reason it is popular is due to its documentation, which is superb as far as NPM modules go, and this is important because, I will be able to use one of the examples to try and demonstrate how to emit an event at the end of a job that will queue a function that can preform the database operations you are asking for. The node-schedule documentation can be found in the software's registry. When you first open the documentation, you will see that it gives you several really nice examples. The first few examples you see on that page are basically all doing the same thing, where they differ is just in the syntax that they are using. In other words, you can Preform a function at a give particular date using any of the date formats/syntax's that you see here in these examples. When it comes down to it they are all pretty standard, but I am going to show you how to do it with the second example, it uses the Date() object, and everyone (who has written JavaScript for any length of time) will now what the Date() object is, and how it is used.





const schedule = require('node-schedule');
const date = new Date(2021, 5,  1,  13, 45,  0);
                   // ^Yr   ^Mo ^Dy ^Hr ^min ^sec

const EventEmitter = require('events');
class Ee extends EventEmitter {}

const jobStatus = new Ee();

jobStatus.on('finished', () => {
    console.log('JOB FINISHED');

    // Preform your DB Operations here (in your case: delete job from database)

});

const job = schedule.scheduleJob(date, function(){
    // Execute Your Jobs code Here
    
    jobStatus.emit('finished'); // Emit your job done emitter when you are done.
});


To Clarify:

  • Basically what new Date() object above says is this:
    • You want your function to execute at 1:30pm on June 1st, 2021.
    • Remember in the JS Date object 0 = January and, 23:59 = 11:59pm

  • Now to explain the EventEmitter part:
    • The const EventEmitter = require('events'); is the node library that gives developers access to the V8 Event Loop (thats a whole can of worms thats not going to be explained here), I am going to assume you know what the event loop is, if not you can look it up. In a nutshell this will let use event emitters which I will talk about next.

    • class MyEmitter extends EventEmitter {} is one of a small handful of different way you could write 'implementing a new instance of an event emitter', this is how the official node documentation shows it, so this is how I will show it, but in truth I don't do it this way. I used the node docs way, so you can refer to this page if you want, it is very helpful for your situation

    • Node.js Event Emitters docs page

    • The jobStatus constant is a newly created instance of the Ee class and it can be used to emit the status of your job at any time during your jobs execution. (Remember this code is all Asynchronous.)

    • If the order of writing stuff looks out of order that is because this code is asynchronous. Events work Asynchronously.


Try this example out, if you have issues you can let me know. You need to post your JS Script that you are working with though, remember this is your problem not mine, and look how much I wrote on this issue, and how much you wrote. There is a lot more you can be including to help me (or other developers) help you.

0
Victor Potes On

I had the same question as you because I have my schedulers also in a database.

The way to check when the last job execution ocurrs according with your code is:

    const job = schedule.scheduleJob(
      {
        start: scheduledJob.startTime,
        end: scheduledJob.endTime,
        rule: cronExp,
      },
      () => {
          this.jobService.send(scheduledJob);
          console.log("sent notification");

        //As there are not more next invocations, it means this invocation is the last one
        if (this.nextInvocation() === null) {
          //here you can code whatever you need to do in the last invocation
        }
      }
    );

Documentation: https://www.npmjs.com/package/node-schedule