domotz-remote-pawn
Version:
Domotz Agent
71 lines (60 loc) • 2.15 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 tom on 09/03/16.
*/
var setHandler = function(proc, log, status, eventUtils, engineRequest) {
function flushLog() {
if (log) {
log.flush();
}
}
function shutdown() {
proc.gracefulShutdown();
flushLog();
}
process.on('uncaughtException', function (error) {
console.error('Error uncaught: ' + error.code);
console.error('Message: ' + error.message);
console.error(error.stack);
proc.gracefulShutdown();
flushLog();
});
process.on('SIGHUP', function () {
console.warn('Catch signal SIGHUP');
flushLog();
});
process.on('SIGINT', function () {
console.warn('Catch signal SIGINT');
proc.gracefulShutdown();
flushLog();
});
process.on('SIGTERM', function () {
console.warn('Catch signal SIGTERM');
if (status && status.get() === status.CONFIGURED) {
console.warn('The agent is in configured mode. Send down signal');
var body = eventUtils.getBodyForEvent("down", "sigterm");
var resource = '/event';
var method = 'POST';
engineRequest.sendWithPromise(resource, method, body, undefined, undefined, {timeout: 5000})
.finally(shutdown);
} else {
shutdown();
}
});
};
module.exports.setHandler = setHandler;