From 0a429f60a5dc93a428e5e88932e7d2bbcdb81b36 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Wed, 1 Apr 2020 10:40:34 +0100 Subject: [PATCH] Add newTargetState event and use it for backup loading Change-type: patch Signed-off-by: Cameron Diver --- src/api-binder.ts | 18 +++++++++++------- src/device-state.ts | 4 ++++ src/event-bus.ts | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/api-binder.ts b/src/api-binder.ts index fd1e251f..b877a7f0 100644 --- a/src/api-binder.ts +++ b/src/api-binder.ts @@ -8,8 +8,6 @@ import { PinejsClientRequest, StatusError } from 'pinejs-client-request'; import * as url from 'url'; import * as deviceRegister from './lib/register-device'; -import * as globalEventBus from './event-bus'; - import Config, { ConfigType } from './config'; import Database from './db'; import { EventTracker } from './event-tracker'; @@ -30,6 +28,7 @@ import { DeviceStatus, TargetState } from './types/state'; import log from './lib/supervisor-console'; import DeviceState from './device-state'; +import * as globalEventBus from './event-bus'; import Logger from './logger'; // The exponential backoff starts at 15s @@ -207,11 +206,16 @@ export class APIBinder { log.debug('Starting current state report'); await this.startCurrentStateReport(); - await loadBackupFromMigration( - this.deviceState, - await this.getTargetState(), - bootstrapRetryDelay, - ); + // When we've provisioned, try to load the backup. We + // must wait for the provisioning because we need a + // target state on which to apply the backup + globalEventBus.getInstance().once('targetStateChanged', async state => { + await loadBackupFromMigration( + this.deviceState, + state, + bootstrapRetryDelay, + ); + }); this.readyForUpdates = true; log.debug('Starting target state poll'); diff --git a/src/device-state.ts b/src/device-state.ts index 1c7c4a89..ec048408 100644 --- a/src/device-state.ts +++ b/src/device-state.ts @@ -17,6 +17,7 @@ import { CompositionStepAction, } from './compose/composition-steps'; import { loadTargetFromFile } from './device-state/preload'; +import * as globalEventBus from './event-bus'; import * as hostConfig from './host-config'; import constants = require('./lib/constants'); import { InternalInconsistencyError, UpdatesLockedError } from './lib/errors'; @@ -447,6 +448,9 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit this.failedUpdates = 0; validateState(target); + + globalEventBus.getInstance().emit('targetStateChanged', target); + const apiEndpoint = await this.config.get('apiEndpoint'); await this.usingWriteLockTarget(async () => { diff --git a/src/event-bus.ts b/src/event-bus.ts index 77c34b6e..1052b55c 100644 --- a/src/event-bus.ts +++ b/src/event-bus.ts @@ -1,9 +1,11 @@ import { EventEmitter } from 'events'; import * as _ from 'lodash'; import StrictEventEmitter from 'strict-event-emitter-types'; +import { TargetState } from './types/state'; export interface GlobalEvents { deviceProvisioned: void; + targetStateChanged: TargetState; } type GlobalEventEmitter = StrictEventEmitter;