Add newTargetState event and use it for backup loading

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2020-04-01 10:40:34 +01:00
parent 75203d0425
commit 0a429f60a5
3 changed files with 17 additions and 7 deletions

View File

@ -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');

View File

@ -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 () => {

View File

@ -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<EventEmitter, GlobalEvents>;