54 lines
3.5 KiB
JavaScript
54 lines
3.5 KiB
JavaScript
"use strict";
|
|
const plugins = require("./smartipc.plugins");
|
|
const q = require("smartq");
|
|
exports.setWorkerBasePath = (basePathArg) => {
|
|
plugins.threads.config.set({
|
|
basepath: {
|
|
node: basePathArg
|
|
}
|
|
});
|
|
};
|
|
class Thread {
|
|
constructor(filePathArg) {
|
|
this.running = false;
|
|
this.assignedPool = null;
|
|
this.workerPath = filePathArg;
|
|
}
|
|
/**
|
|
* sends a message to the spawned process
|
|
*/
|
|
send(message) {
|
|
let done = q.defer();
|
|
this.checkSpawn();
|
|
this.thread.send(message);
|
|
this.thread.on('message', (message) => {
|
|
done.resolve(message);
|
|
});
|
|
this.thread.on('done', (job, message) => {
|
|
done.resolve(message);
|
|
});
|
|
this.thread.on('error', err => {
|
|
done.reject(err);
|
|
});
|
|
return done.promise;
|
|
}
|
|
kill() {
|
|
this.thread.kill();
|
|
this.running = false;
|
|
}
|
|
assignToPool(poolArg) {
|
|
this.assignedPool = poolArg;
|
|
}
|
|
checkSpawn() {
|
|
if (!this.running && !this.assignedPool) {
|
|
this.running = true;
|
|
this.thread = plugins.threads.spawn(this.workerPath);
|
|
}
|
|
else if (!this.running && this.assignedPool) {
|
|
this.running = true;
|
|
this.thread = this.assignedPool.run(this.workerPath);
|
|
}
|
|
}
|
|
}
|
|
exports.Thread = Thread;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMuY2xhc3Nlcy50aHJlYWQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGlwYy5jbGFzc2VzLnRocmVhZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsOENBQTZDO0FBQzdDLDRCQUEyQjtBQUloQixRQUFBLGlCQUFpQixHQUFHLENBQUMsV0FBbUI7SUFDL0MsT0FBTyxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsR0FBR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