From 6c4df5abf04c01a6cb02de9d9352f88b6aa4b5d3 Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Wed, 19 Aug 2020 21:39:58 -0700 Subject: [PATCH] unify update check into a single job --- src/cron.js | 17 +++++------------ src/routes/cloudron.js | 6 +----- src/test/updatechecker-test.js | 10 +++++----- src/updatechecker.js | 25 ++++++++++++++++++++----- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/cron.js b/src/cron.js index b738f3607..377452b07 100644 --- a/src/cron.js +++ b/src/cron.js @@ -40,9 +40,8 @@ var appHealthMonitor = require('./apphealthmonitor.js'), var gJobs = { appAutoUpdater: null, boxAutoUpdater: null, - appUpdateChecker: null, backup: null, - boxUpdateChecker: null, + updateChecker: null, systemChecks: null, diskSpaceChecker: null, certificateRenew: null, @@ -68,7 +67,7 @@ var NOOP_CALLBACK = function (error) { if (error) debug(error); }; function startJobs(callback) { assert.strictEqual(typeof callback, 'function'); - const randomMinute = Math.floor(60*Math.random()); + const randomTick = Math.floor(60*Math.random()); gJobs.systemChecks = new CronJob({ cronTime: '00 30 2 * * *', // once a day. if you change this interval, change the notification messages with correct duration onTick: () => cloudron.runSystemChecks(NOOP_CALLBACK), @@ -81,16 +80,10 @@ function startJobs(callback) { start: true }); - gJobs.boxUpdateCheckerJob = new CronJob({ - cronTime: '00 ' + randomMinute + ' 1,3,5,21,23 * * *', // 5 times - onTick: () => updateChecker.checkBoxUpdates({ automatic: true }, NOOP_CALLBACK), - start: true - }); - // this is run separately from the update itself so that the user can disable automatic updates but can still get a notification - gJobs.appUpdateChecker = new CronJob({ - cronTime: '00 ' + randomMinute + ' 2,4,6,20,22 * * *', // 5 times - onTick: () => updateChecker.checkAppUpdates({ automatic: true }, NOOP_CALLBACK), + gJobs.updateCheckerJob = new CronJob({ + cronTime: `${randomTick} ${randomTick} 1,5,9,13,17,21,23 * * *`, + onTick: () => updateChecker.checkForUpdates({ automatic: true }, NOOP_CALLBACK), start: true }); diff --git a/src/routes/cloudron.js b/src/routes/cloudron.js index dada88e37..b9ebe2672 100644 --- a/src/routes/cloudron.js +++ b/src/routes/cloudron.js @@ -24,7 +24,6 @@ exports = module.exports = { }; let assert = require('assert'), - async = require('async'), auditSource = require('../auditsource.js'), BoxError = require('../boxerror.js'), cloudron = require('../cloudron.js'), @@ -204,10 +203,7 @@ function checkForUpdates(req, res, next) { // it can take a while sometimes to get all the app updates one by one req.clearTimeout(); - async.series([ - (done) => updateChecker.checkAppUpdates({ automatic: false }, done), - (done) => updateChecker.checkBoxUpdates({ automatic: false }, done), - ], function () { + updateChecker.checkForUpdates({ automatic: false }, function () { next(new HttpSuccess(200, { update: updateChecker.getUpdateInfo() })); }); } diff --git a/src/test/updatechecker-test.js b/src/test/updatechecker-test.js index 2854a6cbd..19b460fb9 100644 --- a/src/test/updatechecker-test.js +++ b/src/test/updatechecker-test.js @@ -97,7 +97,7 @@ describe('updatechecker - box - manual (email)', function () { .query({ boxVersion: constants.VERSION, accessToken: 'atoken', automatic: false }) .reply(204, { } ); - updatechecker.checkBoxUpdates({ automatic: false }, function (error) { + updatechecker.checkForUpdates({ automatic: false }, function (error) { expect(!error).to.be.ok(); expect(updatechecker.getUpdateInfo().box).to.be(null); expect(scope.isDone()).to.be.ok(); @@ -114,7 +114,7 @@ describe('updatechecker - box - manual (email)', function () { .query({ boxVersion: constants.VERSION, accessToken: 'atoken', automatic: false }) .reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig' } ); - updatechecker.checkBoxUpdates({ automatic: false }, function (error) { + updatechecker.checkForUpdates({ automatic: false }, function (error) { expect(!error).to.be.ok(); expect(updatechecker.getUpdateInfo().box.version).to.be(UPDATE_VERSION); expect(updatechecker.getUpdateInfo().box.sourceTarballUrl).to.be('box.tar.gz'); @@ -132,7 +132,7 @@ describe('updatechecker - box - manual (email)', function () { .query({ boxVersion: constants.VERSION, accessToken: 'atoken', automatic: false }) .reply(404, { version: '2.0.0-pre.0', changelog: [''], sourceTarballUrl: 'box-pre.tar.gz' } ); - updatechecker.checkBoxUpdates({ automatic: false }, function (error) { + updatechecker.checkForUpdates({ automatic: false }, function (error) { expect(error).to.be.ok(); expect(updatechecker.getUpdateInfo().box).to.be(null); expect(scope.isDone()).to.be.ok(); @@ -167,7 +167,7 @@ describe('updatechecker - box - automatic (no email)', function () { .query({ boxVersion: constants.VERSION, accessToken: 'atoken', automatic: false }) .reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig' } ); - updatechecker.checkBoxUpdates({ automatic: false }, function (error) { + updatechecker.checkForUpdates({ automatic: false }, function (error) { expect(!error).to.be.ok(); expect(updatechecker.getUpdateInfo().box.version).to.be(UPDATE_VERSION); expect(scope.isDone()).to.be.ok(); @@ -202,7 +202,7 @@ describe('updatechecker - box - automatic free (email)', function () { .query({ boxVersion: constants.VERSION, accessToken: 'atoken', automatic: false }) .reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig' } ); - updatechecker.checkBoxUpdates({ automatic: false }, function (error) { + updatechecker.checkForUpdates({ automatic: false }, function (error) { expect(!error).to.be.ok(); expect(updatechecker.getUpdateInfo().box.version).to.be(UPDATE_VERSION); expect(scope.isDone()).to.be.ok(); diff --git a/src/updatechecker.js b/src/updatechecker.js index a291f4cb7..97cac6f31 100644 --- a/src/updatechecker.js +++ b/src/updatechecker.js @@ -1,12 +1,11 @@ 'use strict'; exports = module.exports = { - checkAppUpdates: checkAppUpdates, - checkBoxUpdates: checkBoxUpdates, + checkForUpdates, - getUpdateInfo: getUpdateInfo, - resetUpdateInfo: resetUpdateInfo, - resetAppUpdateInfo: resetAppUpdateInfo, + getUpdateInfo, + resetUpdateInfo, + resetAppUpdateInfo, _setUpdateInfo: setUpdateInfo }; @@ -171,3 +170,19 @@ function checkBoxUpdates(options, callback) { }); }); } + +function checkForUpdates(options, callback) { + assert.strictEqual(typeof options, 'object'); + assert.strictEqual(typeof callback, 'function'); + + checkBoxUpdates(options, function (error) { + if (error) debug('checkForUpdates: error checking for box updates:', error); + + checkAppUpdates(options, function (error) { + if (error) debug('checkForUpdates: error checking for app updates:', error); + + callback(); + }); + }); + +}