Merge pull request #1202 from balena-io/1196-better-detached-logging

Better livepush ux
This commit is contained in:
CameronDiver 2019-05-02 14:16:44 +01:00 committed by GitHub
commit 29d684f9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 8 deletions

View File

@ -164,7 +164,6 @@ export async function deployToDevice(opts: DeviceDeployOptions): Promise<void> {
deployOpts: opts,
});
globalLogger.logLivepush('Watching for file changes...');
const promises = [livepush.init()];
// Only show logs if we're not detaching
if (!opts.detached) {
@ -174,7 +173,12 @@ export async function deployToDevice(opts: DeviceDeployOptions): Promise<void> {
promises.push(
displayDeviceLogs(logStream, globalLogger, opts.system, opts.service),
);
} else {
globalLogger.logLivepush(
'Running in detached mode, no service logs will be shown',
);
}
globalLogger.logLivepush('Watching for file changes...');
await Promise.all(promises);
} else {
if (opts.detached) {

View File

@ -16,6 +16,7 @@ import {
rebuildSingleTask,
} from './deploy';
import { BuildError } from './errors';
import { getServiceColourFn } from './logs';
// How often do we want to check the device state
// engine has settled (delay in ms)
@ -81,7 +82,9 @@ export class LivepushManager {
// so that all of the containers are running and ready to
// be livepush'd into
await this.awaitDeviceStateSettle();
// Split the composition into a load of differents paths which we can
// Split the composition into a load of differents paths
// which we can
this.logger.logLivepush('Device state settled');
// create livepush instances for
for (const serviceName of _.keys(this.composition.services)) {
@ -129,9 +132,11 @@ export class LivepushManager {
);
}
const log = (msg: string) => {
this.logger.logLivepush(`[service ${serviceName}] ${msg}`);
};
const msgString = (msg: string) =>
`[${getServiceColourFn(serviceName)(serviceName)}] ${msg}`;
const log = (msg: string) => this.logger.logLivepush(msgString(msg));
const error = (msg: string) => this.logger.logError(msgString(msg));
const debugLog = (msg: string) => this.logger.logDebug(msgString(msg));
const livepush = await Livepush.init(
dockerfile,
@ -147,6 +152,16 @@ export class LivepushManager {
livepush.on('commandOutput', output =>
log(` ${output.output.data.toString()}`),
);
livepush.on('commandReturn', ({ returnCode, command }) => {
if (returnCode !== 0) {
error(` Command ${command} failed with exit code: ${returnCode}`);
} else {
debugLog(`Command ${command} exited successfully`);
}
});
livepush.on('containerRestart', () => {
log('Restarting service...');
});
// TODO: Memoize this for containers which share a context
const monitor = chokidar.watch('.', {
@ -292,7 +307,16 @@ export class LivepushManager {
this.logger.logLivepush(
`Detected changes for container ${fsEvent.serviceName}, updating...`,
);
await livepush.performLivepush(updates, deletes);
try {
await livepush.performLivepush(updates, deletes);
} catch (e) {
this.logger.logError(
`An error occured whilst trying to perform a livepush: `,
);
this.logger.logError(` ${e.message}`);
this.logger.logDebug(e.stack);
}
}
private async handleServiceRebuild(serviceName: string): Promise<void> {

View File

@ -107,7 +107,7 @@ export function displayLogObject<T extends Log>(
logger.logLogs(toPrint);
}
const getServiceColourFn = _.memoize(_getServiceColourFn);
export const getServiceColourFn = _.memoize(_getServiceColourFn);
const colorHash = new ColorHash();
function _getServiceColourFn(serviceName: string): (msg: string) => string {

View File

@ -144,7 +144,7 @@
"is-root": "^1.0.0",
"js-yaml": "^3.10.0",
"klaw": "^3.0.0",
"livepush": "^1.1.3",
"livepush": "^1.2.0",
"lodash": "^4.17.4",
"minimatch": "^3.0.4",
"mixpanel": "^0.10.1",