Reputation: 455
I want to develop a node.js program that will be executed at a specific time using a job scheduler (node-schedule). This program is running in the background using forever (node.js module). Here's the content of my app.js :
var schedule = require('node-schedule');
~ ~
var id = request.body.id;
var scheduled = schedule.scheduledJobs;
if(scheduled[id]!=null){
//Tasks
}
else{
scheduled[id].cancel;
delete scheduled[id];
}
But if app.js is killed by any reason,
the schedule
object is removed.
and sometimes app.js is restarted by forever.
how can I handle node-schedule objects ?
Upvotes: 2
Views: 2923
Reputation: 41
I have faced similar problem recently and there are two solutions: 1. Use actual cron 2. Use database
I solved my problem by using database. Each time you are creating some event save it to database. In your app.js file when the application is starting make function reading the database and creating scheduled events accordingly.
The first option is better if you do not have dynamic tasks, you do not create new tasks or if they are always the same.
Upvotes: 3