worker-parrot-party
Version:
Parrot party for everyone
38 lines • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parrot = void 0;
const worker_threads_1 = require("worker_threads");
const uuid_1 = require("uuid");
const tslog_1 = require("tslog");
class Parrot {
constructor(_parrotConfig) {
this._parrotConfig = _parrotConfig;
this._logger = new tslog_1.Logger({ name: 'Parrot' });
this._id = (0, uuid_1.v4)();
const { execFilePath, ...resourceLimits } = this._parrotConfig;
const opts = { resourceLimits };
this._workerThread = new worker_threads_1.Worker(execFilePath, opts);
}
// eslint-disable-next-line class-methods-use-this
_messageHandler(resolve, workerThread) {
return function handler(executionResult) {
resolve(executionResult);
workerThread.removeListener('message', handler);
};
}
async runTask(args) {
return new Promise((resolve) => {
this._workerThread.once('message', this._messageHandler(resolve, this._workerThread));
this._logger.info(`[${this.pid}] Parrot will execute the task`);
this._workerThread.postMessage({ args, pid: this._id });
});
}
kill() {
this._workerThread.terminate();
}
get pid() {
return this._id;
}
}
exports.Parrot = Parrot;
//# sourceMappingURL=Parrot.js.map