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 deviceRegister from 'resin-register-device';
import * as url from 'url'; import * as url from 'url';
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';
@ -929,7 +931,9 @@ export class APIBinder {
]); ]);
if (!conf.provisioned || conf.apiKey != null || conf.pinDevice != null) { 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; 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 Bluebird from 'bluebird';
import * as _ from 'lodash'; import * as _ from 'lodash';
import Config, { ConfigChangeMap, ConfigKey, ConfigType } from './config'; import Config, { ConfigType } from './config';
import DB from './db'; import DB from './db';
import { EventTracker } from './event-tracker'; import { EventTracker } from './event-tracker';
import Docker from './lib/docker-utils'; import Docker from './lib/docker-utils';
@ -16,6 +16,7 @@ import {
} from './logging'; } from './logging';
import LogMonitor from './logging/monitor'; import LogMonitor from './logging/monitor';
import * as globalEventBus from './event-bus';
import log from './lib/supervisor-console'; import log from './lib/supervisor-console';
interface LoggerSetupOptions { interface LoggerSetupOptions {
@ -72,40 +73,27 @@ export class Logger {
// Only setup a config listener if we have to // Only setup a config listener if we have to
if (!this.balenaBackend.isIntialised()) { if (!this.balenaBackend.isIntialised()) {
const handler = async (values: ConfigChangeMap<ConfigKey>) => { globalEventBus.getInstance().once('deviceProvisioned', async () => {
if ( const conf = await config.getMany([
'uuid' in values || 'uuid',
'apiEndpoint' in values || 'apiEndpoint',
'deviceApiKey' in values 'deviceApiKey',
) { ]);
// 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
const conf = await config.getMany([ // We use Boolean here, as deviceApiKey when unset
'uuid', // is '' for legacy reasons. Once we're totally
'apiEndpoint', // typescript, we can make it have a default value
'deviceApiKey', // of undefined.
]); if (_.every(conf, Boolean)) {
// Everything is set, provide the values to the
// We use Boolean here, as deviceApiKey when unset // balenaBackend, and remove our listener
// is '' for legacy reasons. Once we're totally this.balenaBackend!.assignFields(
// typescript, we can make it have a default value conf.apiEndpoint,
// of undefined. conf.uuid!,
if (_.every(conf, Boolean)) { conf.deviceApiKey,
// Everything is set, provide the values to the );
// balenaBackend, and remove our listener
this.balenaBackend!.assignFields(
conf.apiEndpoint,
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'); log.debug('Starting event tracker');
await this.eventTracker.init(conf); await this.eventTracker.init(conf);
log.debug('Starting api binder');
await this.apiBinder.initClient();
log.debug('Starting logging infrastructure'); log.debug('Starting logging infrastructure');
this.logger.init({ this.logger.init({
enableLogs: conf.loggingEnabled, enableLogs: conf.loggingEnabled,
@ -97,6 +94,9 @@ export class Supervisor {
...conf, ...conf,
}); });
log.debug('Starting api binder');
await this.apiBinder.initClient();
this.logger.logSystemMessage('Supervisor starting', {}, 'Supervisor start'); this.logger.logSystemMessage('Supervisor starting', {}, 'Supervisor start');
if (conf.legacyAppsPresent && this.apiBinder.balenaApi != null) { if (conf.legacyAppsPresent && this.apiBinder.balenaApi != null) {
log.info('Legacy app detected, running migration'); log.info('Legacy app detected, running migration');