Skip to content

Commit 91fd3f4

Browse files
committed
Add specific work debugging.
Signed-off-by: Toha <[email protected]>
1 parent 72227ab commit 91fd3f4

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ntlab/work",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Nodejs queue and promise based work queue",
55
"main": "index.js",
66
"scripts": {

work.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Work extends EventEmitter {
8787
if (typeof options == 'function') {
8888
options = {callback: options};
8989
}
90+
const d = x => typeof options.dbg == 'function' ? options.dbg(x) : dbg(x);
9091
const w = new this(workers);
9192
return new Promise((resolve, reject) => {
9293
let id = ++seq;
@@ -109,7 +110,7 @@ class Work extends EventEmitter {
109110
if (w.works.length == 0) {
110111
always()
111112
.then(() => {
112-
debug('%d> [%d] resolved with %s', id, idx, dbg(w.rres));
113+
debug('%d> [%d] resolved with %s', id, idx, d(w.rres));
113114
resolve(w.rres);
114115
})
115116
.catch(err => reject(err))
@@ -132,7 +133,10 @@ class Work extends EventEmitter {
132133
debug('%d> [%d] rejected but return as resolved', id, idx);
133134
resolve();
134135
} else {
135-
debug('%d> [%d] rejected with %s', id, idx, dbg(err));
136+
debug('%d> [%d] rejected with %s', id, idx, d(err));
137+
if (typeof options.onerror == 'function') {
138+
options.onerror(w);
139+
}
136140
reject(err);
137141
}
138142
})
@@ -141,8 +145,9 @@ class Work extends EventEmitter {
141145
}
142146
// worker main handler
143147
const f = worker => {
148+
w.current = worker;
144149
const idx = worker.idx;
145-
const winfo = worker.handler.toString();
150+
const winfo = worker.info;
146151
const skip = !worker.isEnabled(w);
147152
try {
148153
if (skip) {
@@ -152,7 +157,7 @@ class Work extends EventEmitter {
152157
debug('%d> [%d] call %s', id, idx, winfo);
153158
worker.handler(w)
154159
.then(res => {
155-
debug('%d> [%d] return %s', id, idx, dbg(res));
160+
debug('%d> [%d] return %s', id, idx, d(res));
156161
w.rres = res;
157162
next(idx, res);
158163
})
@@ -214,6 +219,10 @@ class Worker
214219
return typeof this.enabled == 'function' ? this.enabled(caller) : true;
215220
}
216221

222+
get info() {
223+
return this.handler.toString();
224+
}
225+
217226
static create(work) {
218227
if (work instanceof Worker) {
219228
return work;

0 commit comments

Comments
 (0)