2

I want to insert data into the mongoDB using worker thread

Right now I have a worker file, whole purpose of worker file is to save the data into the mongoDB and mainjs is sending the name of the agent that I want to save into the mongoDB

worker.js

const { workerData, parentPort } = require('worker_threads')
const Agent = require('../models/agent')


console.log("Worker thread is running like a hell: ",  workerData)


    const processRecord =  async () => {
      
       const agent = new Agent({agent:workerData})
                
        try {
            await agent.save()
        }catch(e) {
            console.log(e)
        }
    }

    processRecord();

main.js

const express = require('express')
const router = new express.Router()
const { Worker } = require('worker_threads')

router.post('/agent', async (req, res) => {        
    function runService(workerData) {
        return new Promise((resolve, reject) => {
          const worker = new Worker('../src/router/worker.js', { workerData });
          worker.on('message', resolve);
          worker.on('error', reject);
          worker.on('exit', (code) => {
            if (code !== 0)
              reject(new Error(`Worker stopped with exit code ${code}`));
          })
        })
      }
      
      async function run() {
        const result = await runService('john wick')
        console.log("this is from main thread " + result);
      }
      
      run().catch(err => console.error(err))
})

When I try to save the data using worker thread then I am getting this error:

MongooseError: Operation `agents.insertOne()` buffering timed out after 10000ms
    at Timeout.setTimeout (F:\node\insuredmine\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:184:20)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

and when I run the same line of code without worker thread then its running fine.

1 Answer 1

6

You would need to configure mongoose and connect to MongoDB within a worker thread because Worker has isolated env ie its own event loop etc. If you are using a Worker to save data to MongoDB for just learning purposes then it's ok otherwise no use of a Worker here. Workers only useful when you have long-running synchronous tasks to perform for example encrypting a file, resizing an image or complex data processing, sorting large data, etc.

You can refer to this article for example code https://henrikgronvall.com/articles/nodejs-worker-threads/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.