josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.06k stars 147 forks source link

(question) How to override the .exec function ? #391

Closed ghostlexly closed 1 year ago

ghostlexly commented 1 year ago

I want to override the .exec function on workerpool but it's calls my console.log twice, can't figure out why :

export const queues = workerpool.pool({
  maxWorkers: require("os").cpus().length,
});
const originalExec = queues.exec;
queues.exec = function (method, params, options) {
  console.log("task started");

  return originalExec.call(queues, method, params, options);
};

Tryed to change the "this" argument on the call, also the .apply() function, the results are the same.

josdejong commented 1 year ago

The exec method can indeed call itself:

https://github.com/josdejong/workerpool/blob/8219f7deaf535310cb8c3f273cda8e047959502c/src/Pool.js#L143-L146

I guess you can just write a wrapper function (or object) and invoke that instead of replacing the original method?

ghostlexly commented 1 year ago

Okay, i will wrap it. Thanks for your reply.