Skip to content

Commit

Permalink
handle system clock change
Browse files Browse the repository at this point in the history
- refresh schedules
  • Loading branch information
Steve-Mcl committed Aug 22, 2020
1 parent 2b990b7 commit 2127805
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
47 changes: 45 additions & 2 deletions cronplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,20 @@ module.exports = function (RED) {
node.commandResponseMsgOutput = config.commandResponseMsgOutput || "output1";
node.outputs = config.commandResponseMsgOutput === "output2" ? 2 : 1;//1 output pins (all messages), 2 outputs (schedules out of pin1, command responses out of pin2)
node.statusUpdatePending = false;


const MAX_CLOCK_DIFF = 5000;
var clockMonitor = setInterval(function timeChecker() {
var oldTime = timeChecker.oldTime || new Date();
var newTime = new Date();
var timeDiff = newTime - oldTime;
timeChecker.oldTime = newTime;
if (Math.abs(timeDiff) >= MAX_CLOCK_DIFF) {
node.log("System Time Change Detected!");
refreshTasks(node);
}
}, 1000);

const setProperty = function (msg, field, value) {
const set = (obj, path, val) => {
const keys = path.split('.');
Expand Down Expand Up @@ -896,7 +910,36 @@ module.exports = function (RED) {
}
return r;
}

function refreshTasks(node) {
let tasks = node.tasks;
node.log("Refreshing running schedules");
if(tasks){
try {
let now = new Date();
if(!tasks || !tasks.length)
return null;
let tasksToRefresh = tasks.filter(function(task) {
return task._sequence || (task.isRunning && task._expression && !isTaskFinished(task));
});
if(!tasksToRefresh || !tasksToRefresh.length){
return null;
}
for (let index = 0; index < node.tasks.length; index++) {
let task = node.tasks[index];
if(task.node_expressionType == "cron") {
task.stop();
task.start();
} else {
updateTask(node,task.node_opt,null);
}
task.runScheduledTasks();
index--;
}
}
catch(e){ }
updateNextStatus(node);
}
}
function stopTask(node,name,resetCounter){
let task = getTask(node,name);
if(task){
Expand Down Expand Up @@ -1097,7 +1140,7 @@ module.exports = function (RED) {
return;
}
task.node_count = task.node_count + 1;//++ stops at 2147483647
sendMsg(node, task, timestamp);
sendMsg(node, task, timestamp);
process.nextTick(function(){
if( task.node_expressionType === "solar" ){
updateTask(node,task.node_opt,null);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-cron-plus",
"version": "1.0.7",
"version": "1.1.0",
"description": "A flexible scheduler (cron, solar events, simple dates) node for Node-RED with full dynamic control and Timezone support",
"main": "cronplus.js",
"scripts": {
Expand Down

0 comments on commit 2127805

Please sign in to comment.