Merge pull request #1243 from balena-io/catch-fetch-error

Add newTargetState event and use it for backup loading
This commit is contained in:
CameronDiver 2020-04-07 16:42:54 +01:00 committed by GitHub
commit 2271071c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 url from 'url';
import * as deviceRegister from './lib/register-device'; import * as deviceRegister from './lib/register-device';
import * as globalEventBus from './event-bus';
import Config, { ConfigType } from './config'; import Config, { ConfigType } from './config';
import Database from './db'; import Database from './db';
import { EventTracker } from './event-tracker'; import { EventTracker } from './event-tracker';
@ -30,6 +28,7 @@ import { DeviceStatus, TargetState } from './types/state';
import log from './lib/supervisor-console'; import log from './lib/supervisor-console';
import DeviceState from './device-state'; import DeviceState from './device-state';
import * as globalEventBus from './event-bus';
import Logger from './logger'; import Logger from './logger';
// The exponential backoff starts at 15s // The exponential backoff starts at 15s
@ -207,11 +206,16 @@ export class APIBinder {
log.debug('Starting current state report'); log.debug('Starting current state report');
await this.startCurrentStateReport(); await this.startCurrentStateReport();
// 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( await loadBackupFromMigration(
this.deviceState, this.deviceState,
await this.getTargetState(), state,
bootstrapRetryDelay, bootstrapRetryDelay,
); );
});
this.readyForUpdates = true; this.readyForUpdates = true;
log.debug('Starting target state poll'); log.debug('Starting target state poll');

View File

@ -17,6 +17,7 @@ import {
CompositionStepAction, CompositionStepAction,
} from './compose/composition-steps'; } from './compose/composition-steps';
import { loadTargetFromFile } from './device-state/preload'; import { loadTargetFromFile } from './device-state/preload';
import * as globalEventBus from './event-bus';
import * as hostConfig from './host-config'; import * as hostConfig from './host-config';
import constants = require('./lib/constants'); import constants = require('./lib/constants');
import { InternalInconsistencyError, UpdatesLockedError } from './lib/errors'; import { InternalInconsistencyError, UpdatesLockedError } from './lib/errors';
@ -447,6 +448,9 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
this.failedUpdates = 0; this.failedUpdates = 0;
validateState(target); validateState(target);
globalEventBus.getInstance().emit('targetStateChanged', target);
const apiEndpoint = await this.config.get('apiEndpoint'); const apiEndpoint = await this.config.get('apiEndpoint');
await this.usingWriteLockTarget(async () => { await this.usingWriteLockTarget(async () => {

View File

@ -1,9 +1,11 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import * as _ from 'lodash'; import * as _ from 'lodash';
import StrictEventEmitter from 'strict-event-emitter-types'; import StrictEventEmitter from 'strict-event-emitter-types';
import { TargetState } from './types/state';
export interface GlobalEvents { export interface GlobalEvents {
deviceProvisioned: void; deviceProvisioned: void;
targetStateChanged: TargetState;
} }
type GlobalEventEmitter = StrictEventEmitter<EventEmitter, GlobalEvents>; type GlobalEventEmitter = StrictEventEmitter<EventEmitter, GlobalEvents>;