UNPKG

domotz-remote-pawn

Version:

Domotz Agent

175 lines (155 loc) 5.76 kB
/** 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. var TASK_REQUIRING_HOST_INFO_START_TIME = 10000; const TASK_IN_RESTRICTED_MODE = { updatePkgInfo: { frequency: 60000, start_time: TASK_REQUIRING_HOST_INFO_START_TIME }, updateAgent: { frequency: 3600000, start_time: 0 } }; /** * 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) { var toMs = time.toMs, msecs2Human = time.msecs2Human, nextStartTime = time.getNextStartTime, randomFromString = random.randomNumberFromString; var cronJobs = {}, list = {}, tasks = events; if (status.get() === status.RESTRICTED) { list = TASK_IN_RESTRICTED_MODE; } else { 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 speedTestPause = es.speed_test_start_time || nextStartTime(randomStartTIme + toMs(0, 15), speedTestPeriod); var autoWakeOnLanPeriod = es.auto_wol_frequency || toMs(0, 2); var autoWakeOnLanPause = es.auto_wol_start_time || nextStartTime(toMs(0, 4, 50), autoWakeOnLanPeriod); list = { fastDeviceDiscovery: { frequency: toMs(0, 0, 30), start_time: TASK_REQUIRING_HOST_INFO_START_TIME }, heartBeat: { frequency: toMs(0, 0, 58), start_time: 0 }, deviceStatusUpdate: { frequency: toMs(0, 1), start_time: toMs(0, 1) }, 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, 30), start_time: 0 }, mtr: { frequency: mtrPeriod, start_time: mtrPause }, speedTest: { frequency: speedTestPeriod, start_time: speedTestPause }, autoWakeOnLan: { frequency: autoWakeOnLanPeriod, start_time: autoWakeOnLanPause }, invalidateTcpMonitoredServicesList:{ frequency: toMs(2), start_time: 0 } }; } function unsetPeriodic(task) { var id = cronJobs[task]; console.info('Unset periodic task: [%s]. Id: %s', task, id); 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){ console.error("Undefined task %s! Ignoring", task); return; } console.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 }; }; module.exports.factory = factory;