Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.06k stars 1.19k forks source link

[Service Bus] Cancel a message in a queue by sequenceNumber #25081

Open YHAMSTM opened 1 year ago

YHAMSTM commented 1 year ago

Is your feature request related to a problem? Please describe. Hello, My current work using Service Bus is to provide a producer/consumer scheme by scheduling works for a set of workers. When an user schedule a work, a message is directly published in a queue.

Since workers can take some time to be available, an user can be frustated and may want to cancel its current work.

Describe the solution you'd like As currently available with scheduledMessages, I would like to have the same function as cancelScheduledMessages for messages already available in queue.

Currently it relies on "receiveMessages" and "abandonMessage", but it increases the number of "delivered". It should not on this use-case. Describe alternatives you've considered To do so, I would like to be able to cancel(), or complete() a message by its sequenceNumber. Example:

    const sbClient = new ServiceBusClient(AZURE_SERVICEBUS_CONNECTION_STRING, sbOptions);
    const queueReceiver = sbClient.createReceiver(queueName, { receiveMode: 'peekLock' });
    const queueSender = sbClient.createSender(queueName);
    try {
        let canceled = false;
        for (const message of await queueReceiver.peekMessages(1000)) {
            const { shouldBeCanceled } = message.body;
            if (shouldBeCanceled && message.sequenceNumber) {
                await queueReceiver.cancelMessages(message.sequenceNumber);
                /* or: await queueSender.cancelMessages(message.sequenceNumber); */
                canceled = true;
            }
        }
        return { canceled, message: canceled ? 'Message deleted' : 'Message does not exist' };

    }
    catch (err: unknown) {
        if (isError(err)) {
            return { canceled: false, message: 'Error while deleting message: ' + err.message };
        } 
        return { canceled: false };
    } finally {
        await queueReceiver.close();
        await queueSender.close();
        await sbClient.close();
    }

Additional context Add any other context or screenshots about the feature request here.

jeremymeng commented 1 year ago

@YHAMSTM thank you for submitting this issue! To clarify, you would like remove/delete a message from the queue by its sequence number? This operation isn't supported by the Azure Service Bus service today but I will forward this feedback to the service team for consideration.

YHAMSTM commented 1 year ago

Hi @jeremymeng Yes, exactly. I would like to have the ability to receive, complete or cancel a message by its sequence number. Since my current way to "cancel" is by receiving a batch of messages in queue (with peekLock), complete the message I want to cancel and abandon messages I want to let in queue. With this, raising "maxDeliveryCount" is mandatory...

jeremymeng commented 1 year ago

@YHAMSTM the service team has planned features that may enable this scenario. But we don't have any ETA on when this will be available.

YHAMSTM commented 1 year ago

Great news!