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"
}
}
I have used the diagnose and solve problems and searched for Function and execution errors and noticed the following exception.
Full Exception : Exception while executing function /Functions.EventHubConnectionTest ---> System.Net.Sockets.SocketException /Connection timed out System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result) Microsoft.Azure.Amqp.Transport.AmqpTransportInitiator.<>c.<ConnectAsync>b__17_1(IAsyncResult r) System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar,Func`2 endFunction,Action`1 endAction,Task`1 promise,Boolean requiresSynchronization) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.CreateAndOpenConnectionAsync(Version amqpVersion,Uri serviceEndpoint,Uri connectionEndpoint,EventHubsTransportType transportType,IWebProxy proxy,Int32 sendBufferSizeBytes,Int32 receiveBufferSizeBytes,RemoteCertificateValidationCallback certificateValidationCallback,String scopeIdentifier,TimeSpan timeout) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.Amqp.FaultTolerantAmqpObject`1.OnCreateAsync[T](??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync[TValue](??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync[TValue](??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.OpenProducerLinkAsync(String partitionId,TransportProducerFeatures features,PartitionPublishingOptions options,TimeSpan operationTimeout,TimeSpan linkTimeout,String linkIdentifier,CancellationToken cancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateLinkAndEnsureProducerStateAsync(String partitionId,String producerIdentifier,PartitionPublishingOptions partitionOptions,TimeSpan timeout,CancellationToken cancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateLinkAndEnsureProducerStateAsync(String partitionId,String producerIdentifier,PartitionPublishingOptions partitionOptions,TimeSpan timeout,CancellationToken cancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.Amqp.FaultTolerantAmqpObject`1.OnCreateAsync[T](??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync[TValue](??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync[TValue](??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateBatchAsync(CreateBatchOptions options,CancellationToken cancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Amqp.AmqpProducer.CreateBatchAsync(CreateBatchOptions options,CancellationToken cancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Azure.Messaging.EventHubs.Producer.EventHubProducerClient.CreateBatchAsync(??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Threading.Tasks.ValueTask`1.get_Result() async Microsoft.Azure.WebJobs.EventHubs.EventHubProducerClientImpl.CreateBatchAsync(??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.EventHubs.EventHubAsyncCollector.AddAsync(??) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Bindings.TypedAsyncCollectorAdapter`3.AddAsync[TSrc,TDest,TAttribute](TSrc item,CancellationToken cancellationToken) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Bindings\AsyncCollector\TypedAsyncCollectorAdapter.cs /40 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Binding.FunctionBinding.BindAsyncCollectorAsync[T](BindingContext context) /src/azure-functions-host/src/WebJobs.Script/Binding/FunctionBinding.cs /200 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Binding.ExtensionBinding.BindAsync(BindingContext context) /src/azure-functions-host/src/WebJobs.Script/Binding/ExtensionBinding.cs /80 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Description.WorkerFunctionInvoker.<>c__DisplayClass13_0.<BindOutputsAsync>b__0(??) /src/azure-functions-host/src/WebJobs.Script/Description/Workers/WorkerFunctionInvoker.cs /182 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Description.WorkerFunctionInvoker.BindOutputsAsync(Object input,Binder binder,ScriptInvocationResult result) /src/azure-functions-host/src/WebJobs.Script/Description/Workers/WorkerFunctionInvoker.cs /186 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Description.WorkerFunctionInvoker.InvokeCore(Object[] parameters,FunctionInvocationContext context) /src/azure-functions-host/src/WebJobs.Script/Description/Workers/WorkerFunctionInvoker.cs /109 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters) /src/azure-functions-host/src/WebJobs.Script/Description/FunctionInvokerBase.cs /82 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Script.Description.FunctionGenerator.Coerce[T](Task`1 src) /src/azure-functions-host/src/WebJobs.Script/Description/FunctionGenerator.cs /225 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync[TReflected,TReturnValue](Object instance,Object[] arguments) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs /52 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeWithTimeoutAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs /581 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance,ParameterHelper parameterHelper,ILogger logger,CancellationTokenSource functionCancellationTokenSource) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs /527 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance,FunctionStartedMessage message,FunctionInstanceLogEntry instanceLogEntry,ParameterHelper parameterHelper,ILogger logger,CancellationToken cancellationToken) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs /306 End of inner exception System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance,FunctionStartedMessage message,FunctionInstanceLogEntry instanceLogEntry,ParameterHelper parameterHelper,ILogger logger,CancellationToken cancellationToken) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs /352 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken) D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs /108