From 6fb0147d3c45801375f1e5a0477278fa86a2b26d Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Thu, 18 Oct 2018 17:36:42 +0200 Subject: [PATCH] Fix preloading in flasher images by reading apps.json if target hasn't been set i.e. if we're not provisioned or if the target state is empty (of apps), then we read apps.json to preload. We then mark that the target state has been set to avoid trying to preload again if we ever get an empty target state from the API. Change-type: patch Signed-off-by: Pablo Carranza Velez --- src/config.ts | 1 + src/device-state.coffee | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index 55ac05df..9c962120 100644 --- a/src/config.ts +++ b/src/config.ts @@ -75,6 +75,7 @@ class Config extends EventEmitter { // a JSON value, which is either null, or { app: number, commit: string } pinDevice: { source: 'db', mutable: true, default: 'null' }, currentCommit: { source: 'db', mutable: true }, + targetStateSet: { source: 'db', mutable: true, default: 'false' }, }; public constructor({ db, configPath }: ConfigOpts) { diff --git a/src/device-state.coffee b/src/device-state.coffee index 34419c4b..fc017cef 100644 --- a/src/device-state.coffee +++ b/src/device-state.coffee @@ -166,7 +166,8 @@ module.exports = class DeviceState extends EventEmitter @config.getMany([ 'initialConfigSaved', 'listenPort', 'apiSecret', 'osVersion', 'osVariant', - 'version', 'provisioned', 'apiEndpoint', 'connectivityCheckEnabled', 'legacyAppsPresent' + 'version', 'provisioned', 'apiEndpoint', 'connectivityCheckEnabled', 'legacyAppsPresent', + 'targetStateSet' ]) .then (conf) => Promise.try => @@ -195,8 +196,20 @@ module.exports = class DeviceState extends EventEmitter update_downloaded: false ) .then => - if !conf.provisioned + @applications.getTargetApps() + .then (targetApps) => + if !conf.provisioned or (_.isEmpty(targetApps) and !validation.checkTruthy(conf.targetStateSet)) @loadTargetFromFile() + .finally => + @config.set({ targetStateSet: 'true' }) + else + console.log('Skipping preloading') + if conf.provisioned and !_.isEmpty(targetApps) + # If we're in this case, it's because we've updated from an older supervisor + # and we need to mark that the target state has been set so that + # the supervisor doesn't try to preload again if in the future target + # apps are empty again (which may happen with multi-app). + @config.set({ targetStateSet: 'true' }) .then => @triggerApplyTarget({ initial: true }) @@ -315,12 +328,14 @@ module.exports = class DeviceState extends EventEmitter return { apps, config: filteredDeviceConf } loadTargetFromFile: (appsPath) -> + console.log('Attempting to load preloaded apps...') appsPath ?= constants.appsJsonPath fs.readFileAsync(appsPath, 'utf8') .then(JSON.parse) .then (stateFromFile) => if _.isArray(stateFromFile) # This is a legacy apps.json + console.log('Legacy apps.json detected') return @_convertLegacyAppsJson(stateFromFile) else return stateFromFile @@ -357,9 +372,11 @@ module.exports = class DeviceState extends EventEmitter local: stateFromFile }) .then => + console.log('Preloading complete') if stateFromFile.pinDevice # multi-app warning! # The following will need to be changed once running multiple applications is possible + console.log('Device will be pinned') if commitToPin? and appToPin? @config.set pinDevice: JSON.stringify {