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

Add stdio property on WorkerConnector #82

Merged
merged 1 commit into from
Jun 17, 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
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": {
Expand Down
9 changes: 8 additions & 1 deletion src/protocols/workers/WorkerConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Up @@ -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();
Expand Down
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";

/**
Expand All @@ -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 {
Expand Down
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";

/**
Expand All @@ -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 {
Expand Down
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();
}