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

MongooDB instance is not connecting in dedicated worker #404

Closed striveminzkishan closed 5 months ago

striveminzkishan commented 11 months ago

we are using dedicated workers and in our worker code we are executing MongoDB query but we are getting timeout error from the MongoDB server , but the same query works outside of the worker.

our query let queryOutput = await dbModel.findOne({ _id: 1})

getting error

MongooseError: OperationdbModel.findOne()buffering timed out after 10000ms

josdejong commented 10 months ago

I'm not sure how I can help. I did some googling on using MongoDB in a worker thread, maybe you're hitting the following issue:

https://stackoverflow.com/questions/65746964/how-to-insert-a-new-data-in-mongodb-using-worker-thread-in-nodejs

striveminzkishan commented 10 months ago

after using this getting error Cannot overwrite my model name model once compiled.\n at Mongoose.model

as we have already that model in my code

we need to import that into worker file

josdejong commented 10 months ago

Then you need to import your model in both your main application and in a dedicated worker.

striveminzkishan commented 10 months ago

Hello @josdejong i have done this

image

please check in the image

but getting issue

we are using dedicated workers and in our worker code we are executing MongoDB query but we are getting timeout error from the MongoDB server , but the same query works outside of the worker.

our query let queryOutput = await dbModel.findOne({ _id: 1})

getting error

MongooseError: OperationdbModel.findOne() buffering timed out after 10000ms

josdejong commented 10 months ago

Ok so loading the code in the worker works, but running a mongo query not.

I think you'll have to do more debugging and googling. As a first step I would try to create a minimal demo for yourself where you can rule out as much causes as possible. For example rule out workerpool by creating a thread yourself and running the mongo query there (I don't expect the issue has to do with workerpool, but you never know).

striveminzkishan commented 10 months ago

HI @josdejong

I am still struggling with this problem could you please help with the demo code

Thanks

josdejong commented 10 months ago

To debug your issue, you'll have to rule out possible causes of the issue until you find the root of the issue. Possible causes: there is something wrong with MongoDB, there is something wrong with Mongoose, there is something wrong with workerpool, there is something wrong with running MongoDB in a worker thread, there is something wrong with your local environment, and maybe some more.

Sometimes it is easiest to start with a minimal piece of code that works (i.e. a minimal connection to MongoDB that executes a findOne, and then step by step add all the components that you're using (move it into a worker thread, add mongoose, add workerpool), working towards your full application.

Or the other way around: sometimes it is easiest to take your broken application, and start stripping code and components until it works.

striveminzkishan commented 10 months ago

Hello @josdejong

yes i have checked with a minimal piece of code that works without the MongoDB model

getting the same issue when we use the MongoDB model it is working fine

MongooseError: OperationdbModel.findOne() buffering timed out after 10000ms

striveminzkishan commented 10 months ago

Hello @josdejong

when i run worker without MongoDB model the it is working , means if i did not import any MongoDB model and just perform only some calculation then it is working fine

we are getting issue only when we try to find any record with mongoDB

Thanks

josdejong commented 10 months ago

Good to hear you're one step further. I'm not entirely sure what your findings mean. Please let me know if it turns out to be some issue with workerpool that we can fix or improve upon. If it is not related to workerpool let's close this issue OK?