From 1cd9d07d8c96b0fdbafb5c6aedae64760a092f54 Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Mon, 14 Sep 2015 10:52:11 -0700 Subject: [PATCH] Merge apphealthtask into box server We used to run this as a separate process but no amount of node/v8 tweaking makes them run as standalone with 50M RSS. Three solutions were considered for the memory issue: 1. Use systemd timer. apphealthtask needs to run quiet frequently (10 sec) for the ui to get the app health update immediately after install. 2. Merge into box server (this commit) 3. Increase memory to 80M. This seems to make apphealthtask run as-is. --- box.js | 28 ++++++-------- setup/container/systemd/apphealthtask.service | 15 -------- setup/container/systemd/cloudron.target | 4 +- apphealthtask.js => src/apphealthmonitor.js | 38 ++++++------------- 4 files changed, 25 insertions(+), 60 deletions(-) delete mode 100644 setup/container/systemd/apphealthtask.service rename apphealthtask.js => src/apphealthmonitor.js (85%) diff --git a/box.js b/box.js index 191d55bad..f8a880426 100755 --- a/box.js +++ b/box.js @@ -4,9 +4,11 @@ require('supererror')({ splatchError: true }); -var server = require('./src/server.js'), +var appHealthMonitor = require('./src/apphealthmonitor.js'), + async = require('async'), + config = require('./src/config.js'), ldap = require('./src/ldap.js'), - config = require('./src/config.js'); + server = require('./src/server.js'); console.log(); console.log('=========================================='); @@ -23,22 +25,15 @@ console.log(); console.log('=========================================='); console.log(); -server.start(function (err) { - if (err) { - console.error('Error starting server', err); +async.series([ + server.start, + ldap.start, + appHealthMonitor.start +], function (error) { + if (error) { + console.error('Error starting server', error); process.exit(1); } - - console.log('Server listening on port ' + config.get('port')); - - ldap.start(function (error) { - if (error) { - console.error('Error LDAP starting server', err); - process.exit(1); - } - - console.log('LDAP server listen on port ' + config.get('ldapPort')); - }); }); var NOOP_CALLBACK = function () { }; @@ -52,4 +47,3 @@ process.on('SIGTERM', function () { server.stop(NOOP_CALLBACK); setTimeout(process.exit.bind(process), 3000); }); - diff --git a/setup/container/systemd/apphealthtask.service b/setup/container/systemd/apphealthtask.service deleted file mode 100644 index 72c4627eb..000000000 --- a/setup/container/systemd/apphealthtask.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=Cloudron App Health Monitor -OnFailure=crashnotifier@%n.service -StopWhenUnneeded=true - -[Service] -Type=idle -WorkingDirectory=/home/yellowtent/box -Restart=always -ExecStart=/usr/bin/node --optimize_for_size --max_semi_space_size=5 --max_old_space_size=30 /home/yellowtent/box/apphealthtask.js -Environment="HOME=/home/yellowtent" "USER=yellowtent" "DEBUG=box*,connect-lastmile" "BOX_ENV=cloudron" "NODE_ENV=production" -KillMode=process -User=yellowtent -Group=yellowtent -MemoryLimit=50M diff --git a/setup/container/systemd/cloudron.target b/setup/container/systemd/cloudron.target index 64fc7225f..fff92e173 100644 --- a/setup/container/systemd/cloudron.target +++ b/setup/container/systemd/cloudron.target @@ -2,8 +2,8 @@ Description=Cloudron Smart Cloud Documentation=https://cloudron.io/documentation.html StopWhenUnneeded=true -Requires=apphealthtask.service box.service janitor.timer oauthproxy.service -After=apphealthtask.service box.service janitor.timer oauthproxy.service +Requires=box.service janitor.timer oauthproxy.service +After=box.service janitor.timer oauthproxy.service # AllowIsolate=yes [Install] diff --git a/apphealthtask.js b/src/apphealthmonitor.js similarity index 85% rename from apphealthtask.js rename to src/apphealthmonitor.js index 53463e842..cd4b3e18d 100755 --- a/apphealthtask.js +++ b/src/apphealthmonitor.js @@ -4,41 +4,31 @@ require('supererror')({ splatchError: true }); -var appdb = require('./src/appdb.js'), +var appdb = require('./appdb.js'), assert = require('assert'), async = require('async'), - database = require('./src/database.js'), - DatabaseError = require('./src/databaseerror.js'), - debug = require('debug')('box:apphealthtask'), - docker = require('./src/docker.js'), - mailer = require('./src/mailer.js'), + DatabaseError = require('./databaseerror.js'), + debug = require('debug')('box:apphealthmonitor'), + docker = require('./docker.js'), + mailer = require('./mailer.js'), superagent = require('superagent'), util = require('util'); exports = module.exports = { - run: run + start: start }; var HEALTHCHECK_INTERVAL = 10 * 1000; // every 10 seconds. this needs to be small since the UI makes only healthy apps clickable var UNHEALTHY_THRESHOLD = 3 * 60 * 1000; // 3 minutes var gHealthInfo = { }; // { time, emailSent } -function debugApp(app, args) { +function debugApp(app) { assert(!app || typeof app === 'object'); var prefix = app ? app.location : '(no app)'; debug(prefix + ' ' + util.format.apply(util, Array.prototype.slice.call(arguments, 1))); } -function initialize(callback) { - assert.strictEqual(typeof callback, 'function'); - - async.series([ - database.initialize, - mailer.initialize - ], callback); -} - function setHealth(app, health, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof health, 'string'); @@ -134,14 +124,10 @@ function run() { }); } -if (require.main === module) { - initialize(function (error) { - if (error) { - console.error('apphealth task exiting with error', error); - process.exit(1); - } +function start(callback) { + assert.strictEqual(typeof callback, 'function'); - run(); - }); + debug('Starting apphealthmonitor'); + run(); + callback(); } -