smartspawn/dist/smartipc.classes.thread.js

72 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-01-29 19:50:36 +00:00
"use strict";
2017-03-03 19:52:23 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2017-01-29 19:50:36 +00:00
const plugins = require("./smartipc.plugins");
const q = require("smartq");
2017-03-04 10:40:32 +00:00
exports.workerBasePath = null;
2017-01-29 19:50:36 +00:00
exports.setWorkerBasePath = (basePathArg) => {
2017-03-04 10:40:32 +00:00
exports.workerBasePath = basePathArg;
2017-01-29 19:50:36 +00:00
plugins.threads.config.set({
basepath: {
2017-03-04 10:40:32 +00:00
node: exports.workerBasePath
2017-01-29 19:50:36 +00:00
}
});
};
class Thread {
constructor(filePathArg) {
2017-01-29 22:41:26 +00:00
this.running = false;
this.assignedPool = null;
this.workerPath = filePathArg;
2017-01-29 19:50:36 +00:00
}
/**
* sends a message to the spawned process
2017-03-03 19:52:23 +00:00
* spawns it and keeps running
2017-01-29 19:50:36 +00:00
*/
send(message) {
let done = q.defer();
2017-01-29 22:41:26 +00:00
this.checkSpawn();
this.thread.send(message);
this.thread.on('message', (message) => {
2017-01-29 19:50:36 +00:00
done.resolve(message);
2017-01-29 22:41:26 +00:00
});
this.thread.on('done', (job, message) => {
done.resolve(message);
});
this.thread.on('error', err => {
2017-01-29 19:50:36 +00:00
done.reject(err);
});
return done.promise;
}
2017-03-03 19:52:23 +00:00
/**
* sends a command once and then kills the child process
*/
sendOnce(message) {
let done = q.defer();
this.send(message).then(message => {
done.resolve(message);
this.kill();
});
return done.promise;
}
/**
* kills the thread
*/
2017-01-29 19:50:36 +00:00
kill() {
this.thread.kill();
2017-01-29 22:41:26 +00:00
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);
}
2017-01-29 19:50:36 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