Calling Azure function synchronously, without HttpTrigger - azure

I want to test a Queue-triggered Azure Function over HTTP (integration test).
Is there any general method to call a deployed Azure Function, synchronously?
I have successfully called it with the admin/functions/{function} endpoint as shown here. But I get 202 Accepted which is no good - My test needs to wait for the function to complete (and fail if the function failed).

That behavior is driven by the Function, not the client. So if your Function properly closes the http connection, but continues processing, there's nothing the client can do about that.
So, you can either test through the queue, or have a side function with an HTTP Trigger that calls the same processing method(s) that only returns when it's done.

This is not possible. The Azure Functions host does not support returning the output of a queue trigger to the admin HTTP endpoint. I suggest looking into the suggestion by Johns-305.

Related

Best way to start a background process from GCP HTTP function call?

So, according to the docs here https://cloud.google.com/functions/docs/writing/http
Terminating HTTP functions
If a function creates background tasks (such as threads, futures, Node.js Promise objects, callbacks, or system processes), you must terminate or otherwise resolve these tasks before returning an HTTP response. Any tasks not terminated prior to an HTTP response may not be completed, and may also cause undefined behavior.
So, if one needs to launch a long-running background task from within HTTP function, but still return from function fast, there is no a straightforward way.
Have tried the PubSub approach (calling await topic.publishJSON(pars)), but looks like publishing a topic is quite time-consuming operation - which takes 2-3 secs. (8-)
Then probably pubsub trigger function runs well ok, but this 2-3 seconds delay makes it useless.
P.S.: using the approach with starting Promise from inside function is actually working, but it sounds like error-prone since it's against the docs.
If you need a quick answer you have 2 type of solutions
Async
With Cloud Functions, you need to invoke (perform an HTTP call) another functions (or Cloud Run or App Engine), without waiting the answer, and answer back to the requester. The call that you performed will run in background and answer something to your cloud function that no longer listen!
With PubSub, it's similar. Instead of invoking a Cloud Functions (or Cloud Run or App Engine), you publish a message into a PubSub topic. Then create a subscription to call your long running pocess
Same idea with Cloud Task, but you create a Task in a queue
Sync
If you use Cloud Run instead of Cloud Functions, you are able to perform partial answer to the requester. Like that, you can immediately answer back to the requester with a partial response which says "OK" and continue the process in the request context, and send another partial response when you want, or at the end of the long running process to inform the user the end of their process.

Azure Function store requests and trigger the funtion again later

I have an Azure Function that will take a POST request , do some processing and then sends a POST to another Endpoint with the content of the input request.
I have the requirement, that I somehow have to store all the incoming requests and after a fixed time period the Azure Function needs to send the same POST request again.
What I could do is, set up a cloud storage where I store all the incoming requests and have a second Azure Function with a timer trigger that reads from the storage and sends the request again. My problem with this is, that I have to set up an additional storage and I am looking for a more cost efficient method.
Does anyone know an alternative way to "store" incoming requests and have the same Azure Funtion pick them up again later?
Sounds like what you're looking for is durable functions which can handle exactly this kind of scenario and it'll do all the complicated parts of storing state/context during delays. They are backed by Azure storage but as has been said this is one of the cheapest services available on Azure.
For what you've described you might want to do a combination of function chaining:
Combined with a timer in between the processing you do in chained functions:
//do some initial processing (possibly in an Activity function)
//...
DateTime waitTime = context.CurrentUtcDateTime.Add(TimeSpan.FromDays(1));
await context.CreateTimer(waitTime, CancellationToken.None);
//call the next activity function (parameters can also be passed in)
await context.CallActivityAsync("DoNextSetOfThings");
High level what you'd have is something along the lines of:
An HTTP endpoint which you POST to initially
An Orchestrator function which handles the chaining and delays
One or more Activity functions that do the work and can accept parameters and return results to the Orchestrator

Azure function - [Error] (Function) Executed Messages cannot be larger than 65536 bytes. but without queue

My Function is running directly from a Logic App, the Function sits at the end and receives the message from the Logic App, sometimes this message can be very large. That's why I've avoided using a queue.
Unfortunately I'm having the same situation without the queue and I cannot find a documentation to understand if it's a Function limitation or if it's just because I'm using HTTP request as the trigger.
If I use a blob, will the Function give me the same error with the same data?
The function uses http trigger (as linked to the logic app) and the data passed (as string) can be very small or very large.
The data flows correctly from Logic app to the function, I can print the data in the function when arrives from the logic app but after that I get the error

Azure: Call one functionapp from another functionapp

have two function app (httptrigger) in one of azure function apps project.
PUT
DELETE
In certain condition, would like to call DELETE function app from PUT function app.
Is it possible to get directly RUN of DELETE function app as both are resides in same function app project ?
I wouldn't recommend trying to call the actual function directly, but you can certainly refactor the DELETE functionality into a normal method and then call that from both the DELETE and PUT functions.
There is a few ways to call a function from the function:
HTTP request - it's simple, execute a normal HTTP request to your second function. It's not recommended, because it extends function execution time and generates a few additional problems, such as the possibility of receiving a timeout, the unavailability of the service and others.
Storage queues - make communication through queues (recommended), e.g. the first function (in your situation: "PUT function) can insert a message to the queue and the second function ("DELETE function") can listen on this queue and process a message.
Azure Durable Functions - this extensions allows to create rich, easy-to-understand workflows that are cheap and reliable. Another advantage is that they can retain their own internal state, which can be used for communication between functions.
Read more about cross function communication here.

How do I use an Azure Storage Queue in a request / response mode?

As we port more of our node.js code into Azure Functions we see references in the Azure docs that using Storage Queues is the preferred way to delegate processing responsibility instead of using http requests to call other functions.
What is the request/response design pattern we should use for this delegation? Specifically, how can the response sent back through a queue be delivered only to the Function where the request originated?
Here's an example of what we want to do:
Request comes in to an HTTP Trigger Function A
Function A places message into Queue X (in JSON format) with the first key being the unique requestId: "ABC345"
Function A starts listening to Queue Y for the response
Function B dequeues this message and does its work
Function B places message with work results added into Queue Y with requestId: "ABC345"
Function A sees this message with requestId: "ABC345" and returns the HTTP response
How can we get Function A to pick up only the request that it is waiting for?
The getMessage method doesn't seem to be able to selectively listen to a queue, only to grab the top message:
getMessage(queue [, options], callback)
Another angle on this would be if we want multiple Worker Functions to listen to Queue X. Function C would process all requests that have requestType: "query" and Function D would process all requestType: "blob". Without this filtering we would use one queue per Worker function. Is that the right way to do it, too?
Note: We're using node.js but I'm assuming that the Queue API's are equivalent across all SDK's.
Azure Queues really don't do request-response.
Http processing should not be waiting on queues. Http messages should return quickly (synchronously, < 1 minute), whereas queues are used for longer asynchronous background processing. If Http is waiting on queues, it should be using a 202 long-running pattern.
Consider:
keep using Http. You can port to Functions and keep your underlying
patterns.
Use queues in a fully asynchronous matter. So A queues a
message to kick off B and returns; A2 listens on the response from
B.
Check out the Durable Functions preview. This allows
synchronous calls exactly like what you want. It's in preview, but
see https://github.com/Azure/azure-functions-durable-extension
It looks like Azure Logic Apps is the future of orchestrating multiple functions in a request/response pattern. Using Logic Apps you can set up an HTTP trigger (among many others) then set up several functions to run sequentially with conditional logic:
https://learn.microsoft.com/en-us/azure/logic-apps/logic-apps-azure-functions

Resources