UNPKG

tgrid

Version:

Grid Computing Framework for TypeScript

156 lines (155 loc) 5.94 kB
import { Invoke } from "../../components/Invoke"; import { ConnectorBase } from "../internal/ConnectorBase"; import { IWorkerSystem } from "./internal/IWorkerSystem"; /** * Worker Connector. * * The `WorkerConnector` is a communicator class, which creates an `Worker` instance * and interacts with it through RPC (Remote Procedure Call). In other words, * `WorkerConnector` considers the `Worker` instance as a remote server accepting * only one client; {@link WorkerServer}. * * You can create an `Worker` instance with {@link connect} or {@link compile} method. * The {@link connect} method just opens an existing JS (or TS) file, and * {@link compile} method writes a temporary JS (TS) file, and connects to it. * Anyway, the `Worker` instanced program must open the {@link WorkerServer}. * * By the way, don't forget {@link close closing} the worker to clean up the resources. * If the closing be performed by {@link WorkerServer}, you can wait * the worker server closing through the {@link join} method. * * Also, when declaring this `WorkerConnector` type, you've to define three * generic arguments; `Header`, `Provider` and `Remote`. Those generic arguments must * be same with the ones defined in the target {@link WorkerServer} class * (`Provider` and `Remote` must be reversed). * * For reference, the first `Header` type represents an initial data from the * remote client after the connection. I recommend utilize it as an activation tool * for security enhancement. The second generic argument `Provider` represents a * provider from client to server, and the other `Remote` means a provider from the * remote server to client. * * @template Header Type of the header containing initial data. * @template Provider Type of features provided for the remote server. * @template Remote Type of features supported by remote server. * @author Jeongho Nam - https://github.com/samchon */ export declare class WorkerConnector<Header, Provider extends object | null, Remote extends object | null> extends ConnectorBase<Header, Provider, Remote> implements IWorkerSystem { /** * @hidden */ private readonly compiler_; /** * @hidden */ private worker_?; /** * Initializer Constructor. * * For reference, you're planning to run a bundled JavaScript file, * and you're using the NodeJS environment, you can't use the `"thread"` * mode. You've to use the `"process"` mode instead. * * @param header An object containing initialization data like activation. * @param provider An object providing features for remote system. * @param type You can specify the worker mode when NodeJS. Default is "process". */ constructor(header: Header, provider: Provider, type?: "thread" | "process"); /** * Compile server and connect to there. * * The {@link compile} method tries compile JS source code, creates `Worker` instance * with that code connects to the `Worker`. To complete the compilation and connection, * the `Worker` program must open that server using the {@link WorkerServer.open}() * method. * * Note that, after your business has been completed, you've to close the `Worker` using * {@link close}() or {@link WorkerServer.close}(). If you don't close that, vulnerable * memory usage and communication channel would not be destroyed and it may cause the * memory leak. * * @param content JS Source code to compile. * @param options Detailed options like timeout. */ compile(content: string, options?: Partial<WorkerConnector.IConnectOptions>): Promise<void>; /** * Connect to server. * * The {@link connect}() method tries to create an `Worker` instance and connect to the * `Worker`. To complete the connection, the `Worker` program must open that server using * the {@link WorkerServer.open}() method. * * Note that, after your business has been completed, you've to close the `Worker` using * {@link close}() or {@link WorkerServer.close}(). If you don't close that, vulnerable * memory usage and communication channel would not be destroyed and it may cause the * memory leak. * * @param jsFile JS File to be {@link WorkerServer}. * @param options Detailed options like timeout. */ connect(jsFile: string, options?: Partial<WorkerConnector.IConnectOptions>): Promise<void>; /** * @hidden */ private _Test_connection; /** * @hidden */ private _Connect; /** * @hidden */ private _Handshake; /** * @inheritDoc */ close(): Promise<void>; /** * @hidden */ protected sendData(invoke: Invoke): Promise<void>; /** * @hidden */ private _Handle_message; /** * @hidden */ private _Handle_close; } /** * */ export declare namespace WorkerConnector { /** * Current state of the {@link WorkerConnector}. */ export import State = ConnectorBase.State; /** * Connection options for the {@link WorkerConnector.connect}. */ interface IConnectOptions { /** * Milliseconds to wait the worker server to accept or reject it. If omitted, the waiting would be forever. */ timeout: number; /** * 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"; /** * Current working directory of the worker server. */ cwd: string; /** * Environment variables to be passed to the worker server. */ env: Record<string, string>; } }