Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use TypeORM or Prisma inside worker ? #392

Closed
ghostlexly opened this issue Jun 30, 2023 · 7 comments
Closed

How to use TypeORM or Prisma inside worker ? #392

ghostlexly opened this issue Jun 30, 2023 · 7 comments
Labels

Comments

@ghostlexly
Copy link

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
Copy link
Owner

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

@ghostlexly
Copy link
Author

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 josdejong reopened this Jul 5, 2023
@josdejong
Copy link
Owner

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

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

@josdejong josdejong added question and removed invalid labels Jul 5, 2023
@ghostlexly
Copy link
Author

ghostlexly commented Jul 8, 2023

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
Copy link
Owner

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
Copy link
Author

ghostlexly commented Jul 8, 2023

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants