0

I have 2 functions created in a function app

  1. Function 1 adds messages to a service bus queue (Queuename - TestQueue)
  2. Function 2 gets triggered whenever there is a message in the service bus queue (Queuename - TestQueue) and adds that received message to a mongo DB table.

Function 1 successfully added 5K messages into the Queue, and the queue also shows 5K messages received and sent. But Function 2 got executed only 3964 times and I see only those many entries into the mongo DB table as well.

Code of the Function 1

const { ServiceBusClient } = require("@azure/service-bus");
const {MongoClient} = require('mongodb');
const uri = "mongodb://XXXXXXXXX:27017";

// Define connection string and related Service Bus entity names here
const connectionString = "Endpoint=sb://XXXX";
const queueName = "XXXX";

module.exports = async function(context, mySbMsg) {
    context.log('JavaScript ServiceBus queue trigger function processed message', mySbMsg);
    const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
    const queueClient = sbClient.createQueueClient(queueName);
    const sender = queueClient.createSender();

try {
    var client = await MongoClient.connect(uri,{ useUnifiedTopology: true });
    var db = client.db("test_users");
    var response = await db.collection("users").find({"batch_id":mySbMsg}).toArray();
    for (let i = 0; i < response.length; i++) {
    const message= {
        body: response[i].email
    };
        context.log(`Sending message: ${response[i].email} Timestamp: ${new Date()}`);
        await sender.send(message);
    }
    await client.close(); 
    await queueClient.close();
}
catch(e)
{
    context.log(e);
}
finally 
{
    await sbClient.close();
}

};

Code of Function 2

const {MongoClient} = require('mongodb');
const uri = "mongodb://XXXXXX:27017";

module.exports = async function(context, mySbMsg) {

try
{
    context.log(mySbMsg);
    context.log(JSON.stringify(mySbMsg));

    var client = await MongoClient.connect(uri,{ useUnifiedTopology: true });
    var db = client.db("test_users");
    var message = {
        "email": mySbMsg,
        "created_at": new Date(),
        "updated_at": new Date()
    };
    var response_of_adding = await db.collection("users_emails").insertOne(message);
    await client.close();
    context.log(response_of_adding);
    context.log('JavaScript ServiceBus queue trigger function processed message', mySbMsg);
    context.log('Node.js ServiceBus queue trigger function processed message', mySbMsg);
    context.log('EnqueuedTimeUtc =', context.bindingData.enqueuedTimeUtc);
    context.log('DeliveryCount =', context.bindingData.deliveryCount);
    context.log('MessageId =', context.bindingData.messageId);
    context.done();
}
catch(e)
{
    context.log(e);
}
};

I am unable to understand the cause of this. Any help would be appreciated.

Thanks.

0 Answers0