Skip to content

Commit 065f61b

Browse files
authored
Merge pull request #82 from samchon/features/stdio
Add `stdio` property on `WorkerConnector`
2 parents 2438abf + ccad4e8 commit 065f61b

File tree

8 files changed

+81
-10
lines changed

8 files changed

+81
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tgrid",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "lib/index.js",
55
"typings": "lib/index.d.ts",
66
"exports": {

src/protocols/workers/WorkerConnector.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class WorkerConnector<
200200
const compiler: IWorkerCompiler = await this.compiler_.get();
201201
this.worker_ = await compiler.execute(
202202
jsFile,
203-
is_node() === true ? options.execArgv : undefined,
203+
is_node() === true ? options : undefined,
204204
);
205205

206206
// WAIT THE WORKER TO BE READY
@@ -348,5 +348,12 @@ export namespace WorkerConnector {
348348
* Arguments only for the NodeJS environments.
349349
*/
350350
execArgv: string[];
351+
352+
/**
353+
* Whether to redirect the standard input to the worker server.
354+
*
355+
* Available only in the NodeJS + Process environments.
356+
*/
357+
stdio: "overlapped" | "pipe" | "ignore" | "inherit";
351358
}
352359
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1+
import { WorkerConnector } from "../WorkerConnector";
2+
13
/**
24
* @internal
35
*/
46
export interface IWorkerCompiler {
57
compile(content: string): Promise<string>;
68
remove(path: string): Promise<void>;
7-
execute(jsFile: string, argv: string[] | undefined): Promise<Worker>;
9+
execute(
10+
jsFile: string,
11+
options?: Partial<WorkerConnector.IConnectOptions>,
12+
): Promise<Worker>;
813
}
914

1015
/**
1116
* @internal
1217
*/
1318
export namespace IWorkerCompiler {
1419
export type Creator = {
15-
new (jsFile: string, execArgv: string[] | undefined): IWorkerCompiler;
20+
new (
21+
jsFile: string,
22+
options?: Partial<WorkerConnector.IConnectOptions>,
23+
): IWorkerCompiler;
1624
};
1725
}

src/protocols/workers/internal/NodeWorkerCompiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { ThreadWorker } from "./threads/ThreadWorker";
1010
export const NodeWorkerCompiler = async (
1111
type: "process" | "thread",
1212
): Promise<IWorkerCompiler> => ({
13-
execute: async (jsFile, execArg) => {
13+
execute: async (jsFile, options) => {
1414
const factory =
1515
type === "process" ? await ProcessWorker() : await ThreadWorker();
16-
return (<any>new factory(jsFile, execArg)) as Worker;
16+
return (<any>new factory(jsFile, options)) as Worker;
1717
},
1818
compile: async (content) => {
1919
const os = await NodeModule.os.get();

src/protocols/workers/internal/processes/ProcessWorker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type cp from "child_process";
22

33
import { NodeModule } from "../../../../utils/internal/NodeModule";
4+
import { WorkerConnector } from "../../WorkerConnector";
45
import { IWorkerCompiler } from "../IWorkerCompiler";
56

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

15-
public constructor(jsFile: string, execArgv: string[] | undefined) {
16-
this.process_ = fork(jsFile, { execArgv });
16+
public constructor(
17+
jsFile: string,
18+
options?: Partial<WorkerConnector.IConnectOptions>,
19+
) {
20+
this.process_ = fork(jsFile, {
21+
execArgv: options?.execArgv,
22+
stdio: options?.stdio,
23+
});
1724
}
1825

1926
public terminate(): void {

src/protocols/workers/internal/threads/ThreadWorker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type thread from "worker_threads";
22

33
import { NodeModule } from "../../../../utils/internal/NodeModule";
4+
import { WorkerConnector } from "../../WorkerConnector";
45
import { IWorkerCompiler } from "../IWorkerCompiler";
56

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

14-
public constructor(jsFile: string, execArgv: string[] | undefined) {
15-
this.worker_ = new Worker(jsFile, { execArgv });
15+
public constructor(
16+
jsFile: string,
17+
arg?: Partial<WorkerConnector.IConnectOptions>,
18+
) {
19+
this.worker_ = new Worker(jsFile, {
20+
execArgv: arg?.execArgv,
21+
});
1622
}
1723

1824
public terminate(): void {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { WorkerServer } from "tgrid";
2+
3+
import { IScientific } from "../../../../controllers/ICalculator";
4+
5+
class ScientificCalculator implements IScientific {
6+
public pow(x: number, y: number): number {
7+
console.log("pow", x, y);
8+
return Math.pow(x, y);
9+
}
10+
public sqrt(x: number): number {
11+
console.log("sqrt", x);
12+
return Math.sqrt(x);
13+
}
14+
public log(x: number, y: number): number {
15+
console.log("log", x, y);
16+
return Math.log(x) / Math.log(y);
17+
}
18+
}
19+
20+
async function main(): Promise<void> {
21+
const server = new WorkerServer();
22+
await server.open(new ScientificCalculator());
23+
}
24+
main().catch((exp) => {
25+
console.log(exp);
26+
process.exit(-1);
27+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Driver, WorkerConnector } from "tgrid";
2+
3+
import { IScientific } from "../../../controllers/ICalculator";
4+
5+
export async function test_worker_stdio(): Promise<void> {
6+
const connector = new WorkerConnector(null, null, "process");
7+
await connector.connect(`${__dirname}/internal/loud.js`, {
8+
stdio: "ignore",
9+
});
10+
11+
const driver: Driver<IScientific> = connector.getDriver<IScientific>();
12+
await driver.pow(2, 4);
13+
await driver.sqrt(16);
14+
15+
await connector.close();
16+
}

0 commit comments

Comments
 (0)