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

How to use TypeORM or Prisma inside worker ? #392

Closed ghostlexly closed 1 year ago

ghostlexly commented 1 year ago

Hey,

I'm using TypeORM. How i can use "await" inside .exec() function ? I want to wait for the results from my database inside a worker.

import { Connection } from "@/entities/Connection";

await queues.exec(() => {
      const connections = await Connection.find({ where: { facebook: Not(null) } });
    }, []);
josdejong commented 1 year ago

If this is a question about TypeORM please ask at the project itself. You can ask questions here about workerpool.

ghostlexly commented 1 year ago

It's about workerpool, i want to find a way to use Async methods inside workerpool, like typeorm's async methods, it's just an example

josdejong commented 1 year ago

You can "just" use async functions and return Promise like results.

Here is an example: https://jsbin.com/semafew/edit?html,console

ghostlexly commented 1 year ago

Here is the problem i have with the same code:

import workerpool from "workerpool";
import { User } from "./entities/User";
export const queues = workerpool.pool({
  maxWorkers: require("os").cpus().length,
});

  async function getUsers() {
    function getResults() {
      return new Promise(async (resolve, reject) => {
        resolve(await User.find());
      });
    }

    return await getResults();
  }

  const test = await queues.exec(getUsers, []);

User is the entity from TypeORM.

crosslikes-backend-1  | ReferenceError: User_1 is not defined
crosslikes-backend-1  |     at eval (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:6:25)
crosslikes-backend-1  |     at new Promise (<anonymous>)
crosslikes-backend-1  |     at getResults (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:5:20)
crosslikes-backend-1  |     at getUsers (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:9:22)
crosslikes-backend-1  |     at Function.eval (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:10:8)
crosslikes-backend-1  |     at Function.run (/usr/src/app/node_modules/workerpool/src/worker.js:106:12)
crosslikes-backend-1  |     at MessagePort.<anonymous> (/usr/src/app/node_modules/workerpool/src/worker.js:157:27)
crosslikes-backend-1  |     at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
crosslikes-backend-1  |     at exports.emitMessage (node:internal/per_context/messageport:23:28)
josdejong commented 1 year ago

The function getUsers references external code User. The function is serialized, send to the worker, and then deserialized, and therefore must be completely standalone. In this case, on the worker side there is no User. You'll have to either embed this class in the function, or create a dedicated worker.

ghostlexly commented 1 year ago

Is there a way to call the dedicated worker with a class name ? i try to avoid file paths like this

__dirname + '/myWorker.js'

Thanks for the solutions

josdejong commented 1 year ago

No, just like a regular import you need to point to the path where the worker script can be found.