I have a nodeJS functions app, in which i have a http trigger function with following code. It runs successfully.
With all the below code and configuration, i am able to send data to events hub from a local functions app and published functions app, the issue which i am facing is when i run function from portal it takes forever to return response and then it gives 500 error, before adding this events hub code it was running fine on portal.
I am following guide from Microsof site but looks like it is not how it should send the message to events hub and i tried adding await to lines sending data, i have also tried commenting context.done(), and i also tried putting it in a function and calling it with await but still all gives me same behavior.
Same behavior i have seen with timer trigger function and it gives me following exception after running. It looks like it is sending the data but then after sometime it gives a timeout.
[Error] Executed 'Functions.myfunction' (Failed, Id=, Duration=131873ms)
Below is my code before adding events hub code (runs fine on local and portal)
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
try {
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
// Construct response
const responseJSON = {
"message": responseMessage,
"success": true
}
context.res = {
// status: 200, /* Defaults to 200 */
body: responseJSON,
contentType: 'application/json'
};
} catch(err) {
context.res = {
status: 500
};
}
}
I then upgraded my code to following to send data from functions app to events hub this source
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
try {
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
var timeStamp = new Date().toISOString();
var message = 'Message created at: ' + timeStamp;
context.bindings.outputEventHubMessage = [];
context.bindings.outputEventHubMessage.push("1 " + message);
context.bindings.outputEventHubMessage.push("2 " + message);
// context.done();
// Construct response
const responseJSON = {
"message": responseMessage,
"success": true
}
context.res = {
// status: 200, /* Defaults to 200 */
body: responseJSON,
contentType: 'application/json'
};
} catch(err) {
context.res = {
status: 500
};
}
}
I have also added binding of events hub
My functions.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "eventHub",
"direction": "out",
"name": "outputEventHubMessage",
"connection": "smaseventhubsend_EVENTHUBSETTING",
"eventHubName": "smaseventhub"
}
]
}
My local.settings.json file
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"smaseventhubsend_EVENTHUBSETTING": "Pathtoeventhub with key"
}
}