diff --git a/src/apphealthmonitor.js b/src/apphealthmonitor.js index 628bc9296..0122087f8 100644 --- a/src/apphealthmonitor.js +++ b/src/apphealthmonitor.js @@ -7,7 +7,7 @@ var appdb = require('./appdb.js'), DatabaseError = require('./databaseerror.js'), debug = require('debug')('box:apphealthmonitor'), docker = require('./docker.js').connection, - notifications = require('./notifications.js'), + eventlog = require('./eventlog.js'), superagent = require('superagent'), util = require('util'); @@ -132,8 +132,10 @@ function processDockerEvents(intervalSecs, callback) { stream.setEncoding('utf8'); stream.on('data', function (data) { var ev = JSON.parse(data); - appdb.getByContainerId(ev.id, function (error, app) { // this can error for addons - var program = error || !app.id ? ev.id : `app-${app.id}`; + var containerId = ev.id; + + appdb.getByContainerId(containerId, function (error, app) { // this can error for addons + var program = error || !app.id ? containerId : `app-${app.id}`; var now = Date.now(); const notifyUser = (!app || !app.debugMode) && (now - gLastOomMailTime > OOM_MAIL_LIMIT); @@ -142,7 +144,13 @@ function processDockerEvents(intervalSecs, callback) { // do not send mails for dev apps if (notifyUser) { - notifications.oomEvent(program, { app: app || null, details: ev }); // app can be null if it's an addon crash + var auditSource = { + containerId: containerId, + app: app || null // app can be null if it's an addon crash + }; + + eventlog.add(eventlog.ACTION_APP_OOM, auditSource, ev); + gLastOomMailTime = now; } }); diff --git a/src/eventlog.js b/src/eventlog.js index 059165c27..5ab6749de 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -18,6 +18,7 @@ exports = module.exports = { ACTION_APP_UNINSTALL: 'app.uninstall', ACTION_APP_UPDATE: 'app.update', ACTION_APP_LOGIN: 'app.login', + ACTION_APP_OOM: 'app.oom', ACTION_BACKUP_FINISH: 'backup.finish', ACTION_BACKUP_START: 'backup.start', @@ -104,6 +105,8 @@ function add(action, source, data, callback) { notifications.userRemoved(source.userId, data.user); } if (action === exports.ACTION_USER_UPDATE && data.adminStatusChanged) { notifications.adminChanged(source.userId, data.user); + } if (action === exports.ACTION_APP_OOM) { + notifications.oomEvent(source.app ? source.app.id : source.containerId, data); } else { // no notification } diff --git a/src/notifications.js b/src/notifications.js index 6a1edfe6f..b498c3b91 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -169,12 +169,9 @@ function adminChanged(performedBy, user) { }); } -function oomEvent(program, context, callback) { +function oomEvent(program, context) { assert.strictEqual(typeof program, 'string'); assert.strictEqual(typeof context, 'object'); - assert(typeof callback === 'undefined' || typeof callback === 'function'); - - callback = callback || NOOP_CALLBACK; // also send us a notification mail if (config.provider() === 'caas') mailer.oomEvent('support@cloudron.io', program, JSON.stringify(context, null, 4)); @@ -187,7 +184,9 @@ function oomEvent(program, context, callback) { else message = `The container with id ${context.details.id} ran out of memory`; add(admin.id, 'Process died out-of-memory', message, context.app ? '/#/apps' : '', callback); - }, callback); + }, function (error) { + if (error) console.error(error); + }); } function appDied(app, callback) {