diff --git a/janitor.js b/janitor.js deleted file mode 100755 index e4dfdf533..000000000 --- a/janitor.js +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env node - -'use strict'; - -require('supererror')({ splatchError: true }); - -// remove timestamp from debug() based output -require('debug').formatArgs = function formatArgs() { - arguments[0] = this.namespace + ' ' + arguments[0]; - return arguments; -}; - -var assert = require('assert'), - debug = require('debug')('box:janitor'), - async = require('async'), - tokendb = require('./src/tokendb.js'), - authcodedb = require('./src/authcodedb.js'), - database = require('./src/database.js'); - -function initialize(callback) { - assert.strictEqual(typeof callback, 'function'); - - async.series([ - database.initialize - ], callback); -} - -function cleanupExpiredTokens(callback) { - assert.strictEqual(typeof callback, 'function'); - - tokendb.delExpired(function (error, result) { - if (error) return callback(error); - - debug('Cleaned up %s expired tokens.', result); - - callback(null); - }); -} - -function cleanupExpiredAuthCodes(callback) { - assert.strictEqual(typeof callback, 'function'); - - authcodedb.delExpired(function (error, result) { - if (error) return callback(error); - - debug('Cleaned up %s expired authcodes.', result); - - callback(null); - }); -} - -function run() { - cleanupExpiredTokens(function (error) { - if (error) console.error(error); - - cleanupExpiredAuthCodes(function (error) { - if (error) console.error(error); - - process.exit(0); - }); - }); -} - -if (require.main === module) { - initialize(function (error) { - if (error) { - console.error('janitor task exiting with error', error); - process.exit(1); - } - - run(); - }); -} - diff --git a/setup/container.sh b/setup/container.sh index 81bd9b05c..76e62e923 100755 --- a/setup/container.sh +++ b/setup/container.sh @@ -14,6 +14,7 @@ rm -rf "${CONFIG_DIR}" sudo -u yellowtent mkdir "${CONFIG_DIR}" ########## systemd +rm /etc/systemd/system/janitor.* cp -r "${container_files}/systemd/." /etc/systemd/system/ systemctl daemon-reload systemctl enable cloudron.target diff --git a/setup/container/systemd/cloudron.target b/setup/container/systemd/cloudron.target index 557973025..c21afa52e 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=box.service janitor.timer docker_janitor.timer -After=box.service janitor.timer docker_janitor.timer +Requires=box.service docker_janitor.timer +After=box.service docker_janitor.timer # AllowIsolate=yes [Install] diff --git a/setup/container/systemd/janitor.service b/setup/container/systemd/janitor.service deleted file mode 100644 index 3f7811740..000000000 --- a/setup/container/systemd/janitor.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=Cloudron Janitor -OnFailure=crashnotifier@%n.service - -[Service] -Type=simple -WorkingDirectory=/home/yellowtent/box -Restart=no -ExecStart=/usr/bin/node /home/yellowtent/box/janitor.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 -WatchdogSec=30 diff --git a/setup/container/systemd/janitor.timer b/setup/container/systemd/janitor.timer deleted file mode 100644 index cda634646..000000000 --- a/setup/container/systemd/janitor.timer +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description=Cloudron Janitor -StopWhenUnneeded=true - -[Timer] -# this activates it immediately -OnBootSec=0 -OnUnitActiveSec=30min -Unit=janitor.service - diff --git a/src/cron.js b/src/cron.js index b8dab41f3..5fb9f493a 100644 --- a/src/cron.js +++ b/src/cron.js @@ -10,6 +10,7 @@ var apps = require('./apps.js'), cloudron = require('./cloudron.js'), CronJob = require('cron').CronJob, debug = require('debug')('box:cron'), + janitor = require('./janitor.js'), settings = require('./settings.js'), updateChecker = require('./updatechecker.js'); @@ -17,7 +18,8 @@ var gAutoupdaterJob = null, gBoxUpdateCheckerJob = null, gAppUpdateCheckerJob = null, gHeartbeatJob = null, - gBackupJob = null; + gBackupJob = null, + gCleanupTokensJob = null; var gInitialized = false; @@ -82,6 +84,14 @@ function recreateJobs(unusedTimeZone, callback) { timeZone: allSettings[settings.TIME_ZONE_KEY] }); + if (gCleanupTokensJob) gCleanupTokensJob.stop(); + gCleanupTokensJob = new CronJob({ + cronTime: '00 */30 * * * *', // every 30 minutes + onTick: janitor.cleanupTokens, + start: true, + timeZone: allSettings[settings.TIME_ZONE_KEY] + }); + autoupdatePatternChanged(allSettings[settings.AUTOUPDATE_PATTERN_KEY]); if (callback) callback(); @@ -136,6 +146,9 @@ function uninitialize(callback) { gBackupJob.stop(); gBackupJob = null; + gCleanupTokensJob.stop(); + gCleanupTokensJob = null; + gInitialized = false; callback(); diff --git a/src/janitor.js b/src/janitor.js new file mode 100644 index 000000000..a9801ed07 --- /dev/null +++ b/src/janitor.js @@ -0,0 +1,54 @@ +'use strict'; + +var assert = require('assert'), + async = require('async'), + authcodedb = require('./authcodedb.js'), + debug = require('debug')('box:src/janitor'), + tokendb = require('./tokendb.js'); + +exports = module.exports = { + cleanupTokens: cleanupTokens +}; + +function ignoreError(func) { + return function (callback) { + func(function (error) { + if (error) console.error('Ignored error:', error); + + callback(); + }); + }; +} + +function cleanupExpiredTokens(callback) { + assert.strictEqual(typeof callback, 'function'); + + tokendb.delExpired(function (error, result) { + if (error) return callback(error); + + debug('Cleaned up %s expired tokens.', result); + + callback(null); + }); +} + +function cleanupExpiredAuthCodes(callback) { + assert.strictEqual(typeof callback, 'function'); + + authcodedb.delExpired(function (error, result) { + if (error) return callback(error); + + debug('Cleaned up %s expired authcodes.', result); + + callback(null); + }); +} + +function cleanupTokens(callback) { + assert.strictEqual(typeof callback, 'function'); + + async.series([ + ignoreError(cleanupExpiredTokens), + ignoreError(cleanupExpiredAuthCodes) + ], callback); +}