diff --git a/crashnotifierservice.js b/crashnotifierservice.js index eb3d33c47..74846bc89 100755 --- a/crashnotifierservice.js +++ b/crashnotifierservice.js @@ -12,7 +12,7 @@ function main() { var processName = process.argv[2]; console.log('Started crash notifier for', processName); - // mailer needs the db + // notifications api needs the db database.initialize(function (error) { if (error) return console.error('Cannot connect to database. Unable to send crash log.', error); diff --git a/src/logcollector.js b/src/logcollector.js index fa3179152..825ffdbec 100644 --- a/src/logcollector.js +++ b/src/logcollector.js @@ -5,7 +5,7 @@ exports = module.exports = { }; var assert = require('assert'), - mailer = require('./mailer.js'), + notifications = require('./notifications.js'), safe = require('safetydance'), path = require('path'), util = require('util'); @@ -62,9 +62,9 @@ function sendFailureLogs(processName, options) { var stashedLogs = safe.fs.readFileSync(CRASH_LOG_STASH_FILE, 'utf8'); var compiledLogs = stashedLogs ? (stashedLogs + newLogs) : newLogs; - var mailSubject = processName + (stashedLogs ? ' and others' : ''); + var subject = `${processName} ${stashedLogs ? ' and others' : ''} exited unexpectedly`; - mailer.unexpectedExit(mailSubject, compiledLogs, function (error) { + notifications.unexpectedExit(subject, compiledLogs, function (error) { if (error) { console.log('Error sending crashlog. Stashing logs.'); return stashLogs(newLogs); diff --git a/src/mail_templates/unexpected_exit.ejs b/src/mail_templates/unexpected_exit.ejs index 1760a969a..1c8215270 100644 --- a/src/mail_templates/unexpected_exit.ejs +++ b/src/mail_templates/unexpected_exit.ejs @@ -2,7 +2,7 @@ Dear <%= cloudronName %> Admin, -Unfortunately <%= program %> exited unexpectedly! +<%= subject %> Please see some excerpt of the logs below: diff --git a/src/mailer.js b/src/mailer.js index a923eead5..0fe833e95 100644 --- a/src/mailer.js +++ b/src/mailer.js @@ -201,7 +201,6 @@ function mailUserEvent(mailTo, user, event) { }); } - function sendInvite(user, invitor) { assert.strictEqual(typeof user, 'object'); assert(typeof invitor === 'object'); @@ -523,24 +522,22 @@ function oomEvent(mailTo, program, context) { // this function bypasses the queue intentionally. it is also expected to work without the mailer module initialized // NOTE: crashnotifier should ideally be able to send mail when there is no db, however we need the 'from' address domain from the db -function unexpectedExit(program, context, callback) { - assert.strictEqual(typeof program, 'string'); +function unexpectedExit(mailTo, subject, context) { + assert.strictEqual(typeof mailTo, 'string'); + assert.strictEqual(typeof subject, 'string'); assert.strictEqual(typeof context, 'string'); - assert.strictEqual(typeof callback, 'function'); - - if (config.provider() !== 'caas') return callback(); // no way to get admins without db access getMailConfig(function (error, mailConfig) { if (error) return debug('Error getting mail details:', error); var mailOptions = { from: mailConfig.notificationFrom, - to: 'support@cloudron.io', - subject: util.format('[%s] %s exited unexpectedly', mailConfig.cloudronName, program), - text: render('unexpected_exit.ejs', { cloudronName: mailConfig.cloudronName, program: program, context: context, format: 'text' }) + to: mailTo, + subject: `[${mailConfig.cloudronName}] ${subject}`, + text: render('unexpected_exit.ejs', { cloudronName: mailConfig.cloudronName, subject: subject, context: context, format: 'text' }) }; - sendMails([ mailOptions ], callback); + sendMails([ mailOptions ]); }); } diff --git a/src/notifications.js b/src/notifications.js index a0acef94d..4ae9cc0ae 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -13,7 +13,8 @@ exports = module.exports = { userRemoved: userRemoved, adminChanged: adminChanged, oomEvent: oomEvent, - appDied: appDied + appDied: appDied, + unexpectedExit: unexpectedExit }; var assert = require('assert'), @@ -208,3 +209,19 @@ function appDied(app, callback) { add(admin.id, `App ${app.fqdn} died`, `The application ${app.manifest.title} installed at ${app.fqdn} is not responding.`, '/#/apps', callback); }, callback); } + +function unexpectedExit(subject, compiledLogs, callback) { + assert.strictEqual(typeof subject, 'string'); + assert.strictEqual(typeof compiledLogs, 'string'); + assert(typeof callback === 'undefined' || typeof callback === 'function'); + + callback = callback || NOOP_CALLBACK; + + // also send us a notification mail + if (config.provider() === 'caas') mailer.unexpectedExit('support@cloudron.io', subject, compiledLogs); + + actionForAllAdmins(function (admin, callback) { + mailer.unexpectedExit(admin.email, subject, compiledLogs); + add(admin.id, subject, 'Detailed logs have been sent to your email address.', '/#/system', callback); + }, callback); +}