UNPKG

domotz-remote-pawn

Version:

Domotz Agent

110 lines (98 loc) 4.26 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/03/16. */ var assert = require('assert'); var setHandler = function (proc, log, status, resourceLocator) { var myConsole = log.decorateLogs(); var platform = resourceLocator.utils.platform; function flushLog() { if (log) { log.flush(); } } function initShutdown(reason, detail, errorCode) { if (status && status.isConfigured()) { var errorCounter = resourceLocator.utils.errorCounter; try { if ( reason === 'uncaughtException' && (errorCode === 'undefined' || errorCode === null || errorCounter.getShutdownErrorsCode().indexOf(errorCode) === -1) ) { signalUnhandledError(detail); } else { if (errorCounter.getShutdownErrorsCode().indexOf(errorCode) !== -1) { myConsole.warn('Ignoring %s shutdown', errorCode); errorCounter.addToIgnoredErrorsCounter(errorCode); } } } catch (e) { myConsole.error('An error happened while signaling unhandled error:'); myConsole.error(e.stack); } } proc.gracefulShutdown(); } function signalUnhandledError(detail) { if (!status || !status.isConfigured()) { return; } detail = detail || ''; detail = require('querystring').escape(detail.trim()).slice(0, 1000); var configuration = resourceLocator.configuration; var agentUrl = configuration.engine.api_endpoints.agent.replace(/\/(hub|agent)-api\//, '/agent-crash-report-api/') + 'agent/' + configuration.id; require('request').get(agentUrl + '?detail=' + detail + '&p=' + platform.getFullPlatformID()); } process.on('uncaughtException', function (error) { if (error.message && error.message.indexOf('addMembership') !== -1) { myConsole.warn('Ignoring addMembership errors: %s %s', error.message, error.code); } else if (error instanceof assert.AssertionError) { myConsole.warn('Ignoring failed Assertion: %s', error.message); } else { if (status && status.isConfigured()) { var errorCounter = resourceLocator.utils.errorCounter; if (errorCounter.getIgnoredErrorsCode().indexOf(error.code) !== -1) { myConsole.warn('Ignoring %s message: %s', error.code, error.message); errorCounter.addToIgnoredErrorsCounter(error.code); return; } } myConsole.error('Uncaught exception. Message: %s, code: %s', error.message, error.code); myConsole.error(error.stack); var traceback = error.stack.split('\n'); initShutdown('uncaughtException', traceback[0] + ' ' + traceback[1], error.code); } }); process.on('SIGHUP', function () { myConsole.warn('Catch signal SIGHUP'); flushLog(); }); process.on('SIGINT', function () { myConsole.warn('Catch signal SIGINT'); initShutdown('sigint'); }); process.on('SIGTERM', function () { myConsole.warn('Catch signal SIGTERM'); initShutdown('sigterm'); }); //for unit tests return { _signalUnhandledError: signalUnhandledError, }; }; module.exports.setHandler = setHandler;