Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workers coloring implementation. #47

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ export const deploy = catchAsync(
currentFile
});

logProcessOutput(proc.stdout, 'green');
logProcessOutput(proc.stderr, 'red');
logProcessOutput(proc.stdout, proc.pid, currentFile.id);
logProcessOutput(proc.stderr, proc.pid, currentFile.id);

proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
Expand Down
23 changes: 23 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,26 @@ export interface childProcessResponse {
export interface InspectObject {
[key: string]: Array<{ name: string }>;
}
export interface LogMessage {
deploymentName: string;
workerPID: number;
message: string;
}

export const asniCode: number[] = [
166, 154, 142, 118, 203, 202, 190, 215, 214, 32, 6, 4, 220, 208, 184, 172
];

export interface PIDToColorCodeMapType {
[key: string]: number;
}

export interface AssignedColorCodesType {
[key: string]: boolean;
}

// Maps a PID to a color code
export const PIDToColorCodeMap: PIDToColorCodeMapType = {};

// Tracks whether a color code is assigned
export const assignedColorCodes: AssignedColorCodesType = {};
4 changes: 2 additions & 2 deletions src/utils/autoDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const findJsonFilesRecursively = async (
currentFile
});

logProcessOutput(proc.stdout, 'green');
logProcessOutput(proc.stderr, 'red');
logProcessOutput(proc.stdout, proc.pid, currentFile.id);
logProcessOutput(proc.stderr, proc.pid, currentFile.id);

proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
Expand Down
66 changes: 61 additions & 5 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
import * as fs from 'fs';
import * as path from 'path';
import { LogMessage } from '../constants';
import { assignColorToWorker } from './utils';

const logFilePath = path.join(__dirname, '../../logs/');
const logFileName = 'app.log';
const logFileFullPath = path.join(logFilePath, logFileName);

export const logger = {
log: (message: string): void => {
class Logger {
private logQueue: LogMessage[] = [];
private isProcessing = false;

private async processQueue(): Promise<void> {
if (this.isProcessing || this.logQueue.length === 0) return;
this.isProcessing = true;

while (this.logQueue.length > 0) {
const logEntry = this.logQueue.shift();
if (logEntry) {
const { deploymentName, workerPID, message } = logEntry;
this.store(deploymentName, message);
this.present(deploymentName, workerPID, message);
await new Promise(resolve => setTimeout(resolve, 0));
}
}

this.isProcessing = false;
}

public enqueueLog(
deploymentName: string,
workerPID: number,
message: string
): void {
this.logQueue.push({ deploymentName, workerPID, message });
this.processQueue().catch(console.error);
}

private store(deploymentName: string, message: string): void {
const timeStamp = new Date().toISOString();
const logMessage = `${timeStamp} - ${message}\n`;
console.log(message);
const logMessage = `${timeStamp} - ${deploymentName} | ${message}\n`;

if (!fs.existsSync(logFilePath)) {
fs.mkdirSync(logFilePath, { recursive: true });
}
fs.appendFileSync(logFileFullPath, logMessage, { encoding: 'utf-8' });
}
};

private present(
deploymentName: string,
workerPID: number,
message: string
): void {
message = message.trim();
const fixedWidth = 24;

let paddedName = deploymentName.padEnd(fixedWidth, ' ');
if (deploymentName.length > fixedWidth) {
paddedName = deploymentName.substring(0, fixedWidth - 2) + '_1';
}

// Regular expression for splitting by '\n', '. ', or ' /'
const messageLines = message.split(/(?:\n|\. | \/)/);
const coloredName = assignColorToWorker(`${paddedName} |`, workerPID);
const formattedMessageLines = messageLines.map(
line => `${coloredName} ${line}`
);
const logMessage = formattedMessageLines.join('\n');

console.log(logMessage);
}
}

export const logger = new Logger();
44 changes: 38 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { platform } from 'os';
import { join } from 'path';

import { LanguageId, MetaCallJSON } from '@metacall/protocol/deployment';
import { generatePackage, PackageError } from '@metacall/protocol/package';
import { PackageError, generatePackage } from '@metacall/protocol/package';
import { NextFunction, Request, RequestHandler, Response } from 'express';

import {
createInstallDependenciesScript,
currentFile,
IAllApps,
InspectObject
InspectObject,
PIDToColorCodeMap,
allApplications,
asniCode,
assignedColorCodes,
createInstallDependenciesScript,
currentFile
} from '../constants';
import { logger } from './logger';

Expand Down Expand Up @@ -147,9 +151,37 @@ export function isIAllApps(data: unknown): data is IAllApps {

export function logProcessOutput(
proc: NodeJS.ReadableStream | null,
color: 'green' | 'red'
workerPID: number | undefined,
deploymentName: string
): void {
proc?.on('data', (data: Buffer) => {
logger.log(data.toString()[color]);
logger.enqueueLog(deploymentName, workerPID || 0, data.toString());
});
}

export const maxWorkerWidth = (maxIndexWidth = 3): number => {
const workerLengths = Object.keys(allApplications).map(
worker => worker.length
);
return Math.max(...workerLengths) + maxIndexWidth;
};

export const assignColorToWorker = (
deploymentName: string,
workerPID: number
): string => {
if (!PIDToColorCodeMap[workerPID]) {
let colorCode: number;

// Keep looking for unique code
do {
colorCode = asniCode[Math.floor(Math.random() * asniCode.length)];
} while (assignedColorCodes[colorCode]);

// Assign the unique code and mark it as used
PIDToColorCodeMap[workerPID] = colorCode;
assignedColorCodes[colorCode] = true;
}
const assignColorCode = PIDToColorCodeMap[workerPID];
return `\x1b[38;5;${assignColorCode}m${deploymentName}\x1b[0m`;
};
Loading