diff --git a/crashnotifierservice.js b/crashnotifierservice.js index 38085c9d2..cf6002646 100755 --- a/crashnotifierservice.js +++ b/crashnotifierservice.js @@ -2,27 +2,21 @@ 'use strict'; -var database = require('./src/database.js'); +const database = require('./src/database.js'); -var crashNotifier = require('./src/crashnotifier.js'); +const crashNotifier = require('./src/crashnotifier.js'); // This is triggered by systemd with the crashed unit name as argument -function main() { +async function main() { if (process.argv.length !== 3) return console.error('Usage: crashnotifier.js '); - var unitName = process.argv[2]; + const unitName = process.argv[2]; console.log('Started crash notifier for', unitName); // eventlog api needs the db - database.initialize(function (error) { - if (error) return console.error('Cannot connect to database. Unable to send crash log.', error); + await database.initialize(); - crashNotifier.sendFailureLogs(unitName, function (error) { - if (error) console.error(error); - - process.exit(); - }); - }); + await crashNotifier.sendFailureLogs(unitName); } main(); diff --git a/src/crashnotifier.js b/src/crashnotifier.js index 19f8876e6..bcc6ce56a 100644 --- a/src/crashnotifier.js +++ b/src/crashnotifier.js @@ -6,6 +6,7 @@ exports = module.exports = { const assert = require('assert'), auditSource = require('./auditsource.js'), + child_process = require('child_process'), eventlog = require('./eventlog.js'), safe = require('safetydance'), path = require('path'), @@ -17,43 +18,36 @@ const COLLECT_LOGS_CMD = path.join(__dirname, 'scripts/collectlogs.sh'); const CRASH_LOG_TIMESTAMP_OFFSET = 1000 * 60 * 60; // 60 min const CRASH_LOG_TIMESTAMP_FILE = '/tmp/crashlog.timestamp'; -function collectLogs(unitName, callback) { +async function collectLogs(unitName) { assert.strictEqual(typeof unitName, 'string'); - assert.strictEqual(typeof callback, 'function'); - var logs = safe.child_process.execSync('sudo ' + COLLECT_LOGS_CMD + ' ' + unitName, { encoding: 'utf8' }); - if (!logs) return callback(safe.error); - - callback(null, logs); + const logs = child_process.execSync(`sudo ${COLLECT_LOGS_CMD} ${unitName}`, { encoding: 'utf8' }); + return logs; } -function sendFailureLogs(unitName, callback) { +async function sendFailureLogs(unitName) { assert.strictEqual(typeof unitName, 'string'); - assert.strictEqual(typeof callback, 'function'); // check if we already sent a mail in the last CRASH_LOG_TIME_OFFSET window const timestamp = safe.fs.readFileSync(CRASH_LOG_TIMESTAMP_FILE, 'utf8'); if (timestamp && (parseInt(timestamp) + CRASH_LOG_TIMESTAMP_OFFSET) > Date.now()) { console.log('Crash log already sent within window'); - return callback(); + return; } - collectLogs(unitName, async function (error, logs) { - if (error) { - console.error('Failed to collect logs.', error); - logs = util.format('Failed to collect logs.', error); - } + let [error, logs] = await safe(collectLogs(unitName)); + if (error) { + console.error('Failed to collect logs.', error); + logs = util.format('Failed to collect logs.', error); + } - const crashId = `${new Date().toISOString()}`; - console.log(`Creating crash log for ${unitName} with id ${crashId}`); + const crashId = `${new Date().toISOString()}`; + console.log(`Creating crash log for ${unitName} with id ${crashId}`); - if (!safe.fs.writeFileSync(path.join(paths.CRASH_LOG_DIR, `${crashId}.log`), logs)) console.log(`Failed to stash logs to ${crashId}.log:`, safe.error); + if (!safe.fs.writeFileSync(path.join(paths.CRASH_LOG_DIR, `${crashId}.log`), logs)) console.log(`Failed to stash logs to ${crashId}.log:`, safe.error); - [error] = await safe(eventlog.add(eventlog.ACTION_PROCESS_CRASH, auditSource.HEALTH_MONITOR, { processName: unitName, crashId: crashId })); - if (error) console.log(`Error sending crashlog. Logs stashed at ${crashId}.log`); + [error] = await safe(eventlog.add(eventlog.ACTION_PROCESS_CRASH, auditSource.HEALTH_MONITOR, { processName: unitName, crashId: crashId })); + if (error) console.log(`Error sending crashlog. Logs stashed at ${crashId}.log`); - safe.fs.writeFileSync(CRASH_LOG_TIMESTAMP_FILE, String(Date.now())); - - callback(); - }); + safe.fs.writeFileSync(CRASH_LOG_TIMESTAMP_FILE, String(Date.now())); } diff --git a/src/database.js b/src/database.js index 1f1326ada..ce0b32fe7 100644 --- a/src/database.js +++ b/src/database.js @@ -32,10 +32,8 @@ const gDatabase = { name: 'box' }; -function initialize(callback) { - assert.strictEqual(typeof callback, 'function'); - - if (gConnectionPool !== null) return callback(null); +async function initialize() { + if (gConnectionPool !== null) return; if (constants.TEST) { // see setupTest script how the mysql-server is run @@ -66,14 +64,12 @@ function initialize(callback) { connection.query('USE ' + gDatabase.name); connection.query('SET SESSION sql_mode = \'strict_all_tables\''); }); - - callback(null); } -function uninitialize(callback) { - if (!gConnectionPool) return callback(null); +async function uninitialize() { + if (!gConnectionPool) return; - gConnectionPool.end(callback); + gConnectionPool.end(); gConnectionPool = null; }