mreinstein / node-gearman

⚙ Gearman client and worker for node
75 stars 13 forks source link

Multiple instances #28

Closed nickoledimitrova closed 3 years ago

nickoledimitrova commented 3 years ago

Hello!

Firstly, nice work with this project!

I am experiencing issues with it when running workers on multiple instances. I have a Gearman client written in PHP that sends jobs to a worker in Node.js.

When using the helper function adminWorkers, I can see that there are two workers available, but only one of them is handling the jobs. I ran multiple tests and tried to send a lot of jobs in order to see if the reason is that the worker is handling the jobs too fast and they aren't reaching the other worker, but without any success.

How I'm using it:

       worker.on('JOB_ASSIGN', async (job) => {
        const timer = metrics.commandJobExecutionTime.startTimer();
        metrics.countAllCommands();

        let result = job.payload.toString();
        let jobData = JSON.parse(result);

        try {
            await commandSender.sendCommand(jobData, message => {
                timer({ typeOfDevice: jobData.type });

                worker.sendWorkComplete(job.handle, message);
                worker.preSleep();
            })
        } catch(e){
                 console.log(e)
        }
    });

    worker.on('NOOP', () => {
        worker.grabJob();
    })

    worker.connect(() => {
        worker.setWorkerId(makeid(6));
        worker.addFunction('send_commands');
        logger.info({ message: "Send commands worker initialized" });
        worker.preSleep();
    });

I would appreciate your help!

mreinstein commented 3 years ago

You can only have 1 server connection per worker. If you want to have multiple workers, you should run the program multiple times.

Having more than 1 worker per node instance won't result in any parallelism because node is single threaded.

nickoledimitrova commented 3 years ago

I am using only one server, so this is why I was very confused why it was not handling jobs from the two workers (while running the program multiple times).

I managed to fix this by adding a preSleep function in the 'noop' function. Now all workers are handling jobs.

Thank you for your answer :)

mreinstein commented 3 years ago

I'm glad it worked out. :) Is this issue closable? By the way, what are you using gearman for? It's very old software now. I'm maintaining this module but surprised whenever anyone is using it over more modern and better approaches.

nickoledimitrova commented 3 years ago

I'm glad it worked out. :) Is this issue closable? By the way, what are you using gearman for? It's very old software now. I'm maintaining this module but surprised whenever anyone is using it over more modern and better approaches.

It is totally closable, thanks!

It is used in a legacy project :)