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");
}
);
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 theDate()
object is, and how it is used.To Clarify:
new Date()
object above says is this:0 = January
and,23:59 = 11:59pm
The
const EventEmitter = require('events');
is the node library that gives developers access to theV8 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 useevent 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 situationNode.js Event Emitters docs page
The
jobStatus
constant is a newly created instance of theEe
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.