Skip to content

Commit

Permalink
Merge pull request #82 from samchon/features/stdio
Browse files Browse the repository at this point in the history
Add `stdio` property on `WorkerConnector`
samchon authored Jun 17, 2024
2 parents 2438abf + ccad4e8 commit 065f61b
Showing 8 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tgrid",
"version": "1.0.0",
"version": "1.0.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"exports": {
9 changes: 8 additions & 1 deletion src/protocols/workers/WorkerConnector.ts
Original file line number Diff line number Diff line change
@@ -200,7 +200,7 @@ export class WorkerConnector<
const compiler: IWorkerCompiler = await this.compiler_.get();
this.worker_ = await compiler.execute(
jsFile,
is_node() === true ? options.execArgv : undefined,
is_node() === true ? options : undefined,
);

// WAIT THE WORKER TO BE READY
@@ -348,5 +348,12 @@ export namespace WorkerConnector {
* Arguments only for the NodeJS environments.
*/
execArgv: string[];

/**
* Whether to redirect the standard input to the worker server.
*
* Available only in the NodeJS + Process environments.
*/
stdio: "overlapped" | "pipe" | "ignore" | "inherit";
}
}
12 changes: 10 additions & 2 deletions src/protocols/workers/internal/IWorkerCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { WorkerConnector } from "../WorkerConnector";

/**
* @internal
*/
export interface IWorkerCompiler {
compile(content: string): Promise<string>;
remove(path: string): Promise<void>;
execute(jsFile: string, argv: string[] | undefined): Promise<Worker>;
execute(
jsFile: string,
options?: Partial<WorkerConnector.IConnectOptions>,
): Promise<Worker>;
}

/**
* @internal
*/
export namespace IWorkerCompiler {
export type Creator = {
new (jsFile: string, execArgv: string[] | undefined): IWorkerCompiler;
new (
jsFile: string,
options?: Partial<WorkerConnector.IConnectOptions>,
): IWorkerCompiler;
};
}
4 changes: 2 additions & 2 deletions src/protocols/workers/internal/NodeWorkerCompiler.ts
Original file line number Diff line number Diff line change
@@ -10,10 +10,10 @@ import { ThreadWorker } from "./threads/ThreadWorker";
export const NodeWorkerCompiler = async (
type: "process" | "thread",
): Promise<IWorkerCompiler> => ({
execute: async (jsFile, execArg) => {
execute: async (jsFile, options) => {
const factory =
type === "process" ? await ProcessWorker() : await ThreadWorker();
return (<any>new factory(jsFile, execArg)) as Worker;
return (<any>new factory(jsFile, options)) as Worker;
},
compile: async (content) => {
const os = await NodeModule.os.get();
11 changes: 9 additions & 2 deletions src/protocols/workers/internal/processes/ProcessWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type cp from "child_process";

import { NodeModule } from "../../../../utils/internal/NodeModule";
import { WorkerConnector } from "../../WorkerConnector";
import { IWorkerCompiler } from "../IWorkerCompiler";

/**
@@ -12,8 +13,14 @@ export async function ProcessWorker(): Promise<IWorkerCompiler.Creator> {
class ProcessWorker {
private process_: cp.ChildProcess;

public constructor(jsFile: string, execArgv: string[] | undefined) {
this.process_ = fork(jsFile, { execArgv });
public constructor(
jsFile: string,
options?: Partial<WorkerConnector.IConnectOptions>,
) {
this.process_ = fork(jsFile, {
execArgv: options?.execArgv,
stdio: options?.stdio,
});
}

public terminate(): void {
10 changes: 8 additions & 2 deletions src/protocols/workers/internal/threads/ThreadWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type thread from "worker_threads";

import { NodeModule } from "../../../../utils/internal/NodeModule";
import { WorkerConnector } from "../../WorkerConnector";
import { IWorkerCompiler } from "../IWorkerCompiler";

/**
@@ -11,8 +12,13 @@ export async function ThreadWorker(): Promise<IWorkerCompiler.Creator> {
class ThreadWorker {
private readonly worker_: thread.Worker;

public constructor(jsFile: string, execArgv: string[] | undefined) {
this.worker_ = new Worker(jsFile, { execArgv });
public constructor(
jsFile: string,
arg?: Partial<WorkerConnector.IConnectOptions>,
) {
this.worker_ = new Worker(jsFile, {
execArgv: arg?.execArgv,
});
}

public terminate(): void {
27 changes: 27 additions & 0 deletions test/node/protocols/workers/internal/loud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { WorkerServer } from "tgrid";

import { IScientific } from "../../../../controllers/ICalculator";

class ScientificCalculator implements IScientific {
public pow(x: number, y: number): number {
console.log("pow", x, y);
return Math.pow(x, y);
}
public sqrt(x: number): number {
console.log("sqrt", x);
return Math.sqrt(x);
}
public log(x: number, y: number): number {
console.log("log", x, y);
return Math.log(x) / Math.log(y);
}
}

async function main(): Promise<void> {
const server = new WorkerServer();
await server.open(new ScientificCalculator());
}
main().catch((exp) => {
console.log(exp);
process.exit(-1);
});
16 changes: 16 additions & 0 deletions test/node/protocols/workers/test_worker_stdio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Driver, WorkerConnector } from "tgrid";

import { IScientific } from "../../../controllers/ICalculator";

export async function test_worker_stdio(): Promise<void> {
const connector = new WorkerConnector(null, null, "process");
await connector.connect(`${__dirname}/internal/loud.js`, {
stdio: "ignore",
});

const driver: Driver<IScientific> = connector.getDriver<IScientific>();
await driver.pow(2, 4);
await driver.sqrt(16);

await connector.close();
}

0 comments on commit 065f61b

Please sign in to comment.