Skip to content

Commit

Permalink
used PID as identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Apr 9, 2024
1 parent 8becca2 commit 718d0af
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
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, currentFile.id);
logProcessOutput(proc.stderr, currentFile.id);
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
36 changes: 16 additions & 20 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,24 @@ export interface InspectObject {
}
export interface LogMessage {
deploymentName: string;
workerPID: number;
message: string;
}

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

export const deploymentNameToColorCodeMap: Record<string, string> = {};
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, currentFile.id);
logProcessOutput(proc.stderr, currentFile.id);
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
20 changes: 14 additions & 6 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ class Logger {
while (this.logQueue.length > 0) {
const logEntry = this.logQueue.shift();
if (logEntry) {
const { deploymentName, message } = logEntry;
const { deploymentName, workerPID, message } = logEntry;
this.store(deploymentName, message);
this.present(deploymentName, message);
this.present(deploymentName, workerPID, message);
await new Promise(resolve => setTimeout(resolve, 0));
}
}

this.isProcessing = false;
}

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

Expand All @@ -43,7 +47,11 @@ class Logger {
fs.appendFileSync(logFileFullPath, logMessage, { encoding: 'utf-8' });
}

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

Expand All @@ -54,7 +62,7 @@ class Logger {

// Regular expression for splitting by '\n', '. ', or ' /'
const messageLines = message.split(/(?:\n|\. | \/)/);
const coloredName = assignColorToWorker(`${paddedName} |`);
const coloredName = assignColorToWorker(`${paddedName} |`, workerPID);
const formattedMessageLines = messageLines.map(
line => `${coloredName} ${line}`
);
Expand Down
29 changes: 21 additions & 8 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { NextFunction, Request, RequestHandler, Response } from 'express';
import {
IAllApps,
InspectObject,
PIDToColorCodeMap,
allApplications,
asniCode,
assignedColorCodes,
createInstallDependenciesScript,
currentFile,
deploymentNameToColorCodeMap
currentFile
} from '../constants';
import { logger } from './logger';

Expand Down Expand Up @@ -150,10 +151,11 @@ export function isIAllApps(data: unknown): data is IAllApps {

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

Expand All @@ -164,11 +166,22 @@ export const maxWorkerWidth = (maxIndexWidth = 3): number => {
return Math.max(...workerLengths) + maxIndexWidth;
};

export const assignColorToWorker = (deploymentName: string): string => {
if (!deploymentNameToColorCodeMap[deploymentName]) {
const colorCode = asniCode[Math.floor(Math.random() * asniCode.length)];
deploymentNameToColorCodeMap[deploymentName] = colorCode;
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 = deploymentNameToColorCodeMap[deploymentName];
const assignColorCode = PIDToColorCodeMap[workerPID];
return `\x1b[38;5;${assignColorCode}m${deploymentName}\x1b[0m`;
};

0 comments on commit 718d0af

Please sign in to comment.