Skip to content

Commit

Permalink
Add logs for worker requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Apr 10, 2024
1 parent fcb0718 commit 3131c3c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
1 change: 0 additions & 1 deletion file_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function fileLogger(filename: string): LoggingProvider {
removeEventListener("unload", unload);
}
addEventListener("unload", unload);
Deno.addSignalListener("SIGINT", unload);

function log(...args: any[]) {
entries.push(args);
Expand Down
6 changes: 6 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ const workers = new WorkerManager();
for (let i = 0; i < workerCount; i++) {
const id = workers.create();
await workers.call(id, "init", id, apiId, apiHash);
log.info(`Started worker ${id + 1}.`);
}

Deno.addSignalListener("SIGINT", async () => {
await workers.unload();
Deno.exit();
});

Deno.serve({
port,
onListen: ({ port }) => {
Expand Down
52 changes: 45 additions & 7 deletions worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,37 @@

///<reference lib="webworker"/>
///<reference lib="dom" />
import * as log from "std/log/mod.ts";
import * as path from "std/path/mod.ts";
import { existsSync } from "std/fs/mod.ts";

import { InputError } from "mtkruto/0_errors.ts";
import { setLogVerbosity } from "mtkruto/1_utilities.ts";
import { functions, setLoggingProvider, types, Update } from "mtkruto/mod.ts";

import { log } from "./log.ts";
import { serialize } from "./tl_json.ts";
import { deserialize } from "./tl_json.ts";
import { fileLogger } from "./file_logger.ts";
import { isFunctionDisallowed } from "./disallowed_functions.ts";
import { ClientManager, ClientStats } from "./client_manager.ts";
import { ALLOWED_METHODS, AllowedMethod } from "./allowed_methods.ts";

const LOG_PATH = path.join(Deno.cwd(), ".logs", "clients");
if (!existsSync(LOG_PATH)) {
Deno.mkdirSync(LOG_PATH, { recursive: true });
const WORKER_LOG_PATH = path.join(Deno.cwd(), ".logs", "workers");
const CLIENT_LOG_PATH = path.join(Deno.cwd(), ".logs", "clients");
if (!existsSync(CLIENT_LOG_PATH)) {
Deno.mkdirSync(CLIENT_LOG_PATH, { recursive: true });
}
if (!existsSync(WORKER_LOG_PATH)) {
Deno.mkdirSync(WORKER_LOG_PATH, { recursive: true });
}
let id = -1;
let clientManager = new ClientManager(0, "");

addEventListener("message", async (e) => {
const [_id, { _, args }] = e.data;
if (id != -1) {
log.info("in", _, _id, args);
}
let result;
try {
result = await (handlers as any)[_](...args as any[]);
Expand All @@ -63,6 +70,7 @@ addEventListener("message", async (e) => {
result = [null, { status: 500 }];
}
}
log.info("out", _, _id, result ?? null);
postMessage([_id, result ?? null]);
});

Expand All @@ -77,6 +85,7 @@ const handlers = {
setWebhook,
deleteWebhook,
startWebhookLoop,
unload,
};
export type Handler = typeof handlers;

Expand All @@ -86,10 +95,35 @@ function init(id_: number, apiId: number, apiHash: string) {
}
id = id_;
clientManager = new ClientManager(apiId, apiHash);
const logFile = path.join(LOG_PATH, id + "");
const workerLogFile = path.join(WORKER_LOG_PATH, id + "");
const clientLogFile = path.join(CLIENT_LOG_PATH, id + "");
setLogVerbosity(Infinity);
setLoggingProvider(fileLogger(logFile));
log.info(`Started worker ${id + 1}.`);
setLoggingProvider(fileLogger(clientLogFile));
const ENTRY_SEPARATOR = "-".repeat(25);
log.setup({
loggers: {
default: {
level: "INFO",
handlers: ["file"],
},
},
handlers: {
file: new log.FileHandler("NOTSET", {
filename: workerLogFile,
formatter(record) {
const A = record.msg == "in" ? ">>>>>>>>>>" : "<<<<<<<<<<";
const time = record.datetime.toISOString();
const name = record.args[0];
const id = (record.args[1] as string).toUpperCase();
const payload = JSON.stringify(record.args[2], null, 2)
.split("\n")
.map((v) => ` ${v}`)
.join("\n");
return `[${time}]\n [${id}]\n ${A} ${name}\n${payload}\n\n${ENTRY_SEPARATOR}\n`;
},
}),
},
});
}

function clientCount() {
Expand Down Expand Up @@ -178,3 +212,7 @@ async function deleteWebhook(
async function startWebhookLoop(id: string) {
await clientManager.startWebhookLoop(id);
}

function unload() {
dispatchEvent(new Event("unload"));
}
8 changes: 8 additions & 0 deletions worker_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,12 @@ export class WorkerManager {
}
}
}

async unload() {
const promises = new Array<Promise<void>>();
for (const worker of this.#workers.keys()) {
promises.push(this.call(worker, "unload"));
}
await Promise.all(promises);
}
}

0 comments on commit 3131c3c

Please sign in to comment.