Skip to content

Commit ab4ce25

Browse files
committed
Remove existing jobs
1 parent bda73c6 commit ab4ce25

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

MMM-ModuleScheduler.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ Module.register("MMM-ModuleScheduler",{
2121
notificationReceived: function(notification, payload, sender) {
2222
var self = this;
2323
if (notification === 'DOM_OBJECTS_CREATED') {
24+
// Reset
25+
self.sendSocketNotification('REMOVE_ALL_SCHEDULES');
26+
27+
// Create schedules
2428
MM.getModules().exceptModule(this).withClass(this.config.schedulerClass).enumerate(function(module) {
2529
Log.log(self.name + ' wants to schedule the display of ' + module.name );
2630
if (typeof module.config.module_schedule === 'object') {
27-
self.sendSocketNotification('SET_SCHEDULE_MODULE', {name: module.name, id: module.identifier, schedule: module.config.module_schedule});
31+
self.sendSocketNotification('CREATE_MODULE_SCHEDULE', {name: module.name, id: module.identifier, schedule: module.config.module_schedule});
2832
} else {
2933
Log.error( module.name + ' is configured to be scheduled, but the module_schedule option is undefined' );
3034
}

node_helper.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ module.exports = NodeHelper.create({
2727
return true;
2828
}
2929

30-
if (notification === "SET_SCHEDULE_MODULE") {
30+
if (notification === "REMOVE_ALL_SCHEDULES") {
31+
console.log(this.name + ' is removing all schedules');
32+
this.stopAllCronJobs();
33+
}
34+
35+
if (notification === "CREATE_MODULE_SCHEDULE") {
3136
module = payload;
3237
if (!module.schedule.hasOwnProperty('from') || !module.schedule.hasOwnProperty('from')) {
3338
console.log(this.name + ' cannot schedule the module ' + module.name + ' - check module_schedule');
@@ -46,8 +51,9 @@ module.exports = NodeHelper.create({
4651
return false;
4752
}
4853

54+
this.cronJobs.push({module: module, showJob: showJob, hideJob: hideJob});
55+
4956
var now = new Date();
50-
console.log('Current date: ' + now);
5157
if (showJob.nextDate().toDate() > now && hideJob.nextDate().toDate() > showJob.nextDate().toDate()) {
5258
console.log(this.name + ' is hiding ' + module.name);
5359
this.sendSocketNotification('HIDE_MODULE', module.id);
@@ -60,6 +66,27 @@ module.exports = NodeHelper.create({
6066

6167
}
6268
},
69+
70+
stopAllCronJobs: function() {
71+
for (var i = 0; i < this.cronJobs.length; i++) {
72+
var cronJob = this.cronJobs[i];
73+
if( typeof cronJob.showJob === 'object') {
74+
this.stopCronJob(cronJob.showJob);
75+
}
76+
if( typeof cronJob.hideJob === 'object') {
77+
this.stopCronJob(cronJob.hideJob);
78+
}
79+
}
80+
this.cronJobs.length = 0;
81+
},
82+
83+
stopCronJob: function(cronJob){
84+
try {
85+
cronJob.stop();
86+
} catch(ex) {
87+
console.log(this.name + ' could not stop cronJob');
88+
}
89+
},
6390

6491
createCronJobForModule: function(module, type) {
6592
var self = this;

0 commit comments

Comments
 (0)