Skip to content

Commit

Permalink
feat: parse the logs to the AppConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
tapiarafael committed Nov 27, 2023
1 parent 11c62d9 commit c04c9a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions deno-runtime/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum LogMessageSeverity {
export class Logger {
private static instance: Logger;
private entries: Array<object> = [];
public method = '';

private constructor() {}

Expand Down Expand Up @@ -59,6 +60,7 @@ export class Logger {

this.entries.push({
severity,
method: this.method,
timestamp: new Date(),
args: i,
});
Expand All @@ -67,6 +69,7 @@ export class Logger {
public flush() {
const logs = this.entries;
this.entries = [];
this.method = '';
return logs;
}
}
1 change: 1 addition & 0 deletions deno-runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async function handleRequest({ type, payload }: Messenger.JsonRpcRequest): Promi

const { id, method, params } = payload;

logger.method = method;
switch (method) {
case 'construct': {
const [appId, source] = params as [string, string];
Expand Down
32 changes: 23 additions & 9 deletions src/server/runtime/AppsEngineDenoRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as jsonrpc from 'jsonrpc-lite';

import type { AppAccessorManager, AppApiManager } from '../managers';
import type { AppManager } from '../AppManager';
import type { AppLogStorage } from '../storage';
import { AppConsole } from '../logging';

export type AppRuntimeParams = {
appId: string;
Expand Down Expand Up @@ -68,8 +70,10 @@ export class DenoRuntimeSubprocessController extends EventEmitter {

private readonly api: AppApiManager;

private readonly logStorage: AppLogStorage;

// We need to keep the appSource around in case the Deno process needs to be restarted
constructor(private readonly appId: string, private readonly appSource: string, deps: ControllerDeps) {
constructor(private readonly appId: string, private readonly appSource: string, manager: AppManager) {
super();

this.state = 'uninitialized';
Expand All @@ -86,8 +90,9 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
this.state = 'invalid';
}

this.accessors = deps.accessors;
this.api = deps.api;
// this.accessors = manager.getAccessorManager();
// this.api = manager.getApiManager();
// this.logStorage = manager.getLogStorage();
}

emit(eventName: string | symbol, ...args: any[]): boolean {
Expand Down Expand Up @@ -260,11 +265,22 @@ export class DenoRuntimeSubprocessController extends EventEmitter {

if (message.type === 'success') {
param = message.payload.result;
const {value, logs} = param as any;
const { value, logs } = param as any;
param = value;

logs.forEach(({args, severity, timestamp}: { timestamp: any; severity: any; args: any; }) => console.log(`${timestamp} - [${severity}] - ${args}`))
const logger = new AppConsole(logs[0].method); // the method will be the same for all entries
logs.forEach((log: any) => {
const logMethod = logger[log.severity as keyof AppConsole];

if (typeof logMethod !== 'function') {
throw new Error('Invalid log severity');
}

logMethod.apply(logger, log.args);
console.log(`${log.timestamp} - ${log.method} [${log.severity}]: ${log.args}`);
});

// this.logStorage.storeEntries(this.appId, logs);
} else {
param = message.payload.error;
}
Expand Down Expand Up @@ -312,9 +328,7 @@ type ExecRequestContext = {
export class AppsEngineDenoRuntime {
private readonly subprocesses: Record<string, DenoRuntimeSubprocessController> = {};

private readonly accessorManager: AppAccessorManager;

private readonly apiManager: AppApiManager;
private readonly appManager: AppManager;

// constructor(manager: AppManager) {
// this.accessorManager = manager.getAccessorManager();
Expand All @@ -326,7 +340,7 @@ export class AppsEngineDenoRuntime {
throw new Error('App already has an associated runtime');
}

this.subprocesses[appId] = new DenoRuntimeSubprocessController(appId, appSource, { accessors: this.accessorManager, api: this.apiManager });
this.subprocesses[appId] = new DenoRuntimeSubprocessController(appId, appSource, this.appManager);

await this.subprocesses[appId].setupApp();
}
Expand Down

0 comments on commit c04c9a5

Please sign in to comment.