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

Help: Queue with ack = true and prefetchCount >1. Multiple handlers #461

Open
FrostBy opened this issue Jun 27, 2017 · 0 comments
Open

Help: Queue with ack = true and prefetchCount >1. Multiple handlers #461

FrostBy opened this issue Jun 27, 2017 · 0 comments

Comments

@FrostBy
Copy link

FrostBy commented Jun 27, 2017

Hi!
Can anybody help me with the next issue:

  1. I subscribe to a queue with ack = true and prefetchCount >1.
  2. All messages transfers to a balancer, which creates instances of workers, which works with messages.
  3. All messages from a subscription are sent to an array and when any worker is ready for a message, I send it to him from this array.
  4. Once the message is processed, worker acknowledge this message and tries to take another message from the array.
  5. If the array is empty, worker tries to call queue.shift();

So... if i try to call queue.shift() after messageObject.acknowledge, i get

Error: PRECONDITION_FAILED - unknown delivery tag 1

Question: How can i acknowledge messages and then request queue for another bundle of messages

Here some examples of my logic:

   /**
     *
     * @param connection {Connection}
     * @param queue {Queue}
     */
    runQueue(connection, queue) {
        this._connection = connection;
        this._queue = queue;
        queue.subscribe({
            ack:           true,
            prefetchCount: this._availableBots.length
        }, (message, headers, deliveryInfo, ack) => {
            message = JSON.parse(message.data.toString());
            this.balance(message, ack);
        });
    }
    /**
     *
     * @param message {string}
     * @param messageObject {Message}
     */
    balance(message, messageObject) {
        let selectedBot = null;

        for (const bot of this._bots) {
            if (!bot.busy) {
                selectedBot = bot;
                break;
            }
        }
        if (selectedBot) {
            this.run(message, messageObject, selectedBot)
        }
        else {
            this._tasks.push([message, messageObject]);
            if (this._availableBots.length) {
                this.createBot();
            }
        }
    }
 bot.on('free', (messageObject) => {
            console.log('freeBot');
            bot.busy = false;
            if (messageObject) {
                messageObject.acknowledge();
            }
            if (this._tasks.length) {
                let task = this._tasks.shift();
                this.run(task[0], task[1], bot)
            }
            else {
                this._queue.shift(true);
            }
        });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant