Merge pull request #1190 from balena-io/1189-delay-sending-logs

bug: Delay sending logs until the device is provisioned
This commit is contained in:
Rich Bayliss 2020-02-06 14:22:07 +00:00 committed by GitHub
commit 468f7c3d66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 37 deletions

View File

@ -8,6 +8,8 @@ import { PinejsClientRequest, StatusError } from 'pinejs-client-request';
import * as deviceRegister from 'resin-register-device';
import * as url from 'url';
import * as globalEventBus from './event-bus';
import Config, { ConfigType } from './config';
import Database from './db';
import { EventTracker } from './event-tracker';
@ -929,7 +931,9 @@ export class APIBinder {
]);
if (!conf.provisioned || conf.apiKey != null || conf.pinDevice != null) {
return this.provisionOrRetry(conf.bootstrapRetryDelay as number);
await this.provisionOrRetry(conf.bootstrapRetryDelay);
globalEventBus.getInstance().emit('deviceProvisioned');
return;
}
return conf;

17
src/event-bus.ts Normal file
View File

@ -0,0 +1,17 @@
import { EventEmitter } from 'events';
import * as _ from 'lodash';
import StrictEventEmitter from 'strict-event-emitter-types';
export interface GlobalEvents {
deviceProvisioned: void;
}
type GlobalEventEmitter = StrictEventEmitter<EventEmitter, GlobalEvents>;
export class GlobalEventBus extends (EventEmitter as new () => GlobalEventEmitter) {
public constructor() {
super();
}
}
export const getInstance = _.once(() => new GlobalEventBus());

View File

@ -1,7 +1,7 @@
import * as Bluebird from 'bluebird';
import * as _ from 'lodash';
import Config, { ConfigChangeMap, ConfigKey, ConfigType } from './config';
import Config, { ConfigType } from './config';
import DB from './db';
import { EventTracker } from './event-tracker';
import Docker from './lib/docker-utils';
@ -16,6 +16,7 @@ import {
} from './logging';
import LogMonitor from './logging/monitor';
import * as globalEventBus from './event-bus';
import log from './lib/supervisor-console';
interface LoggerSetupOptions {
@ -72,17 +73,7 @@ export class Logger {
// Only setup a config listener if we have to
if (!this.balenaBackend.isIntialised()) {
const handler = async (values: ConfigChangeMap<ConfigKey>) => {
if (
'uuid' in values ||
'apiEndpoint' in values ||
'deviceApiKey' in values
) {
// If any of the values we're interested in have
// changed, retrieve all of the values, check that
// they're all set, and provide them to the
// balenaBackend
globalEventBus.getInstance().once('deviceProvisioned', async () => {
const conf = await config.getMany([
'uuid',
'apiEndpoint',
@ -101,11 +92,8 @@ export class Logger {
conf.uuid!,
conf.deviceApiKey,
);
config.removeListener('change', handler);
}
}
};
config.on('change', handler);
});
}
}

View File

@ -87,9 +87,6 @@ export class Supervisor {
log.debug('Starting event tracker');
await this.eventTracker.init(conf);
log.debug('Starting api binder');
await this.apiBinder.initClient();
log.debug('Starting logging infrastructure');
this.logger.init({
enableLogs: conf.loggingEnabled,
@ -97,6 +94,9 @@ export class Supervisor {
...conf,
});
log.debug('Starting api binder');
await this.apiBinder.initClient();
this.logger.logSystemMessage('Supervisor starting', {}, 'Supervisor start');
if (conf.legacyAppsPresent && this.apiBinder.balenaApi != null) {
log.info('Legacy app detected, running migration');