domotz-remote-pawn
Version:
Domotz Agent
252 lines (217 loc) • 9.93 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
* Created by Tommaso Latini <tommaso@domotz.com> on 09/11/15.
*
* Module that handle cron jobs.
*
* A cron is an entry <key> : <value> where:
* --> <key>: Event to Emit (the name is the same of the module in events/)
* --> <value>: Dict with 2 parameters:
* ** frequency: Rate
* ** start_time: First Execution
* We have some jobs running in restricted mode:
*/
// Give 10 seconds to recover host info.
const TASK_REQUIRING_HOST_INFO_START_TIME = 10000;
/**
* And other running in configured mode.
* Some tasks might have a CUSTOM CONFIGURATION available in:
* configuration --> external_script --> <event>_<'frequency'/'start_time'>
*
*/
var factory = function (time, status, events, configuration, random, resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var toMs = time.toMs,
msecs2Human = time.msecs2Human,
nextStartTime = time.getNextStartTime,
randomFromString = random.randomNumberFromString;
var cronJobs = {},
list = {},
tasks = events;
if (status.isRestricted()) {
list = {
updatePkgInfo: {
frequency: 60000,
start_time: TASK_REQUIRING_HOST_INFO_START_TIME,
},
sanityCheck: {
frequency: time.MINUTE,
start_time: time.MINUTE,
},
};
} else if (status.isConfigured()) {
var code = configuration.licence.code;
var es = configuration.external_scripts;
var randomStartTIme = randomFromString(code, 5 * time.MINUTE, time.HOUR - 1);
// For custom configuration. This field are not present in
// the standard configuration. KEEP THE DEFAULT!!!
var mtrPeriod = es.mtr_frequency || toMs(0, 30);
var mtrPause = es.mtr_start_time || nextStartTime(randomStartTIme, mtrPeriod);
var speedTestPeriod = es.speed_test_frequency || toMs(6);
var speedTestStartTime = randomFromString(code, 1, speedTestPeriod);
var speedTestPause = es.speed_test_start_time || nextStartTime(speedTestStartTime, speedTestPeriod);
var autoWakeOnLanPeriod = es.auto_wol_frequency || toMs(0, 2);
var autoWakeOnLanPause = es.auto_wol_start_time || nextStartTime(toMs(0, 4, 50), autoWakeOnLanPeriod);
var settingsManager = resourceLocator.settingsManager;
var discoveryPeriod = settingsManager.getSetting('configuration.settings.discovery.nmap_scan.period', 30);
myConsole.info('Discovery period is %ds', discoveryPeriod);
var heartbeatPeriodSeconds = settingsManager.getSetting('configuration.settings.heartbeat_period_s', 60);
myConsole.info('Heartbeat period is %ds', heartbeatPeriodSeconds);
var dhcpReportPeriodSeconds = settingsManager.getSetting('configuration.settings.dhcp_report_period_s', 300);
var dhcpReportStartSeconds = settingsManager.getSetting('configuration.settings.dhcp_report_start_s', 900);
myConsole.info('DHCP report period is %ds, start %ds', dhcpReportPeriodSeconds, dhcpReportStartSeconds);
var ipConflictDetectionPeriodSeconds = settingsManager.getSetting('configuration.settings.ip_conflict.period_s', 1800);
var ipConflictDetectionStartSeconds = settingsManager.getSetting('configuration.settings.ip_conflict.start_s', 300);
var baselineLatencyMeasurementPeriodSeconds = settingsManager.getSetting('configuration.settings.latency_test.period_s', 3600);
var baselineLatencyMeasurementStartSeconds = settingsManager.getSetting('configuration.settings.latency_test.start_s', 300);
var updateJammedDevicesStartSeconds = settingsManager.getSetting('configuration.settings.device_jam_start_s', 0);
var updateJammedDevicesPeriodSeconds = settingsManager.getSetting('configuration.settings.device_jam_period_s', 1800);
var osInfoCalculationPeriodHours = settingsManager.getSetting('configuration.settings.os_info.period_h', 26);
var osInfoCalculationStartSeconds = settingsManager.getSetting('configuration.settings.os_info.start_s', 300);
myConsole.info('Ip Conflict Detection period is %ds, start %ds', ipConflictDetectionPeriodSeconds, ipConflictDetectionStartSeconds);
var networkInfoStartSeconds = settingsManager.getSetting('configuration.settings.network_info_start_s', 0);
var networkInfoPeriodSeconds = settingsManager.getSetting('configuration.settings.network_info_period_s', 3600);
var heartBeatStartTimeSeconds = Math.floor(60 * Math.random());
list = {
fastDeviceDiscovery: {
frequency: toMs(0, 0, discoveryPeriod),
start_time: TASK_REQUIRING_HOST_INFO_START_TIME,
},
heartBeat: {
frequency: toMs(0, 0, heartbeatPeriodSeconds),
// Distribute random the start time of the heartbeat
start_time: toMs(0, 0, heartBeatStartTimeSeconds),
},
flushLog: {
frequency: time.HOUR,
start_time: time.HOUR,
},
logFolderCleaner: {
frequency: toMs(12),
start_time: toMs(3, 12, 48),
},
updatePkgInfo: {
frequency: time.HOUR,
start_time: TASK_REQUIRING_HOST_INFO_START_TIME,
},
sanityCheck: {
frequency: time.MINUTE,
start_time: time.MINUTE,
},
updateJammedDevicesList: {
frequency: toMs(0, 0, updateJammedDevicesPeriodSeconds),
start_time: toMs(0, 0, updateJammedDevicesStartSeconds),
},
updateSettings: {
frequency: 3 * time.HOUR,
start_time: 3 * time.HOUR,
},
mtr: {
frequency: mtrPeriod,
start_time: mtrPause,
},
speedTest: {
frequency: speedTestPeriod,
start_time: speedTestPause,
},
autoWakeOnLan: {
frequency: autoWakeOnLanPeriod,
start_time: autoWakeOnLanPause,
},
invalidateEyesList: {
frequency: toMs(2),
start_time: 0,
},
networkInfo: {
frequency: toMs(0, 0, networkInfoPeriodSeconds),
start_time: toMs(0, 0, networkInfoStartSeconds),
},
osInfo: {
frequency: toMs(osInfoCalculationPeriodHours),
start_time: toMs(0, 0, osInfoCalculationStartSeconds),
},
bigNetworksScan: {
frequency: 10 * time.MINUTE,
start_time: time.MINUTE,
},
dhcpRequestsReport: {
frequency: dhcpReportPeriodSeconds * time.SECOND,
start_time: dhcpReportStartSeconds * time.SECOND,
},
ipConflictDetection: {
frequency: ipConflictDetectionPeriodSeconds * time.SECOND,
start_time: ipConflictDetectionStartSeconds * time.SECOND,
},
baselineLatencyMeasurement: {
frequency: baselineLatencyMeasurementPeriodSeconds * time.SECOND,
start_time: baselineLatencyMeasurementStartSeconds * time.SECOND,
},
};
if (process.env.DPLATFORM === 'win') {
list.processCleaner = {
frequency: time.MINUTE,
start_time: 0,
};
}
}
function resetPeriodicTask(task) {
var action = tasks[task];
if (action === undefined) {
myConsole.error('Undefined task %s! Ignoring', task);
return;
}
action();
unsetPeriodic(task);
cronJobs[task] = setInterval(action, list[task].frequency);
}
function unsetPeriodic(task) {
var id = cronJobs[task];
myConsole.info('Unset periodic task: [%s]', task);
clearInterval(id);
}
function setPeriodic(task) {
var now = new Date();
var startTime = list[task].start_time;
var frequency = list[task].frequency;
var first = msecs2Human(now.getTime() + startTime);
var rate = msecs2Human(frequency);
var action = tasks[task];
if (action === undefined) {
myConsole.error('Undefined task %s! Ignoring', task);
return;
}
myConsole.info('CRON - Set periodic task: [%s]. ' + 'First Execution at %s. ' + 'Repeated each: %s', task, first, rate);
setTimeout(function () {
action();
cronJobs[task] = setInterval(action, frequency);
}, startTime);
}
function setPeriodicTasks() {
Object.keys(list).forEach(setPeriodic);
}
function unsetPeriodicTasks() {
Object.keys(list).forEach(unsetPeriodic);
}
return {
start: setPeriodicTasks,
stop: unsetPeriodicTasks,
resetPeriodicTask: resetPeriodicTask,
stopTask: unsetPeriodic,
};
};
module.exports.factory = factory;