domotz-remote-pawn
Version:
Domotz Agent
75 lines (72 loc) • 3.52 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 Iacopo Papalini <iacopo@domotz.com> on 07/12/16.
*
* This module spawns a process that checks periodically the responsiveness of the Domotz Agent and kills it
* if is not responding over HTTP over socket requests in a sensible time
*/
module.exports.init = function (os, childProcess, fs) {
var pollPeriod = parseInt(process.env.DOMOTZ_POLL_PERIOD || 60);
var retryPeriod = parseInt(process.env.DOMOTZ_RETRY_PERIOD || 5);
var retryNumber = parseInt(process.env.DOMOTZ_RETRY_NUMBER || 6);
var gracePeriod = parseInt(process.env.DOMOTZ_GRACE_PERIOD || 120) * 1000;
var tmpDir = os.tmpdir();
var processName = 'domotz_stall_watcher';
var fileName = tmpDir + '/' + processName + '.sh';
var logFileName = process.env.DOMOTZ_LOG_DIR + '/' + processName + '.log';
var contents = '#!/bin/sh\n' +
'. /etc/domotz.env;\n' +
'period=' + pollPeriod + '\n' +
'failure=0\n' +
'while true\ndo\n' +
'\tsleep ${period}\n' +
'\tdomotz_curl --fail --silent -X POST -A domotz-zabbix-proxy -d \'{"action": "listener_status_check"}\' --unix-socket ' +
process.env.DOMOTZ_HTTP_ON_UNIX_SOCKET_FILE + ' --max-time 2 http://localhost\n' +
'\tif [ $? -ne 0 ]; then\n' +
'\t\tfailure=$((failure+1))\n' +
'\t\tif [ $failure -eq ' + retryNumber + ' ]; then\n' +
'\t\t\techo "`date` Killing not responding listener process; pid: $(cat ${LISTENER_PID_FILE})" >> $1\n' +
'\t\t\tkill -9 $(cat ${LISTENER_PID_FILE})\n' +
'\t\t\texit 1\n' +
'\t\telse\n' +
'\t\t\techo "`date` Not responding listener process; pid: $(cat ${LISTENER_PID_FILE}) - tentative $failure" >> $1\n' +
'\t\t\tif [ $failure -eq 1 ]; then\n' +
'\t\t\t\tkill -1 $(cat ${LISTENER_PID_FILE})\n' +
'\t\t\tfi\n' +
'\t\t\tperiod=' + retryPeriod + '\n' +
'\t\tfi\n' +
'\telse\n' +
'\t\tperiod=' + pollPeriod + '\n' +
'\t\tfailure=0\n' +
'\tfi\n' +
'done\n';
return {
start: function () {
"use strict";
console.info('STALL WATCH - Start process. Grace Period: %d. ' +
'Poll Period: %d. Retry Period: %d. #Retry: %d', gracePeriod,
pollPeriod, retryPeriod, retryNumber);
setTimeout(function () {
fs.writeFileSync(fileName, contents, {mode: '0700'});
var watcherProcess = childProcess.spawn(fileName, [logFileName], {detached: false});
watcherProcess.on('exit', function (code, signal) {
console.info("STALL WATCH - Watcher process exited with %s", code || signal);
process.exit(1);
});
}, gracePeriod);
}
};
};