X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=lib%2Fworkers.js;h=103061aa78b55e55625eb877f81b300239116d14;hb=1b8a85ea27d85751abc26c233c48ae8b4690a22f;hp=c192f1812c4a08cf8aa9cdc305a397c0740abdb5;hpb=34a572ebf9c071ffb2f5b1efefd6784d84170138;p=poolifier.git diff --git a/lib/workers.js b/lib/workers.js index c192f181..103061aa 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -27,9 +27,7 @@ class ThreadWorker extends AsyncResource { if (value && value.data && value._id) { // here you will receive messages // console.log('This is the main thread ' + isMainThread) - const res = this.runInAsyncScope(fn, null, value.data) - this.parent.postMessage({ data: res, _id: value._id }) - this.lastTask = Date.now() + this.runInAsyncScope(this._run.bind(this), this, fn, value) } else if (value.parent) { // save the port to communicate with the main thread // this will be received once @@ -47,6 +45,17 @@ class ThreadWorker extends AsyncResource { this.parent.postMessage({ kill: 1 }) } } + + _run (fn, value) { + try { + const res = fn(value.data) + this.parent.postMessage({ data: res, _id: value._id }) + this.lastTask = Date.now() + } catch (e) { + this.parent.postMessage({ error: e, _id: value._id }) + this.lastTask = Date.now() + } + } } module.exports.ThreadWorker = ThreadWorker