question

gestion avatar image
gestion asked

How to get FunctionExecutionContext inside queue function AzureFunction?

Hi there guys,

We've been struggling (like, a lot) to try and get Azure Functions working. We finally managed to call an http triggered function that writes messages on a queue and checked that queue triggered functions actually get executed (thanks again to everyone who gave us advice).

So, FunctionExecutionContext gets passed to the http triggered functions along with the request (as seen in the docs)but, how do we get it inside the queue triggered functions? So far we've tried getting it inside the http triggered function and serialize it as a json string and writing it to the queue itself. Something like this:

[FunctionName("HttpExample")]
 public static async Task<dynamic> Run(
 [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
 [Queue("myqueue-items"),StorageAccount("AzureWebJobsStorage")] ICollector<string> msg,
 ILogger log)
 {
 FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
 string playFabContext = JsonConvert.SerializeObject(context);
 msg.Add(playFabContext);
 return new { messageValue = "Queue function should have been launched." };
 } 

The thing is that this http triggered function takes too long itself (over 1second), either because of the await for the request async read or because of all that json serializing.

Our question is: Is there a way of getting the FunctionExecutionContext inside the queue triggered function without passing it from the http triggered function? We're asking because we believe we will need the authentication context or something similar to work with PlayFab data from the queue triggered function.

Thanks in advance.

[Quick edit]: We tried writing the context directly to the queue without deserializing and serializing and we still get almost 1s execution time, so we believe it's the await for the request read async operation that takes most of the time.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gudge avatar image
Gudge answered

Hi @gestion, I'm sorry to hear you are having trouble with using Azure Functions. I have a few questions;

1. Are you invoking the Azure Functions via the ExecuteFunction API? Or are you invoking them via PlayStream Actions?

2. Have you downloaded https://github.com/PlayFab/PlayFab-Samples/blob/master/Samples/CSharp/AzureFunctions/CS2AFHelperClasses.cs and added it to your Azure Functions project. This file has the various execution contexts you will need. It's basically the replacement for the CloudScriptPlugIn for now.

3. Are your functions registered with PlayFab as HTTP functions or Queued functions?


For a queue triggered function, the queue message will contain the function execution context. However, queue triggered functions only really make sense in PlayStream Action or scheduled task cases. They only make sense in the ExecuteFunction API case if the game client does not need a response.

Let me know the answers to the above and hopefully we'll be able to get you up and running.

Gudge

6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

gestion avatar image gestion commented ·

Hi @Gudge, thanks for the response.

1) ExecuteFunction API (it's a user triggered call).

2) Yes. We have it as PlayFab.Samples.

3) Http function.

We have already been able to make the call and execute http triggered functions from PlayFab, but not queue triggered functions. I believe we can use just http ones but, how can we call queue triggered functions?

Thanks.

0 Likes 0 ·
Gudge avatar image Gudge gestion commented ·

Hi @gestion,

Let's talk about HTTP functions first. HTTP functions have a 10 second timeout when invoked via ExecuteFunction. Your original post mentioned that your HTTP function ran for more than 1 second, but that should be fine so long as it doesn't take more than 10 seconds. I would note that sometimes the very first invocation of an HTTP function since the function app was last deployed (or if there have been no function calls for quite some time) can take a while as the Azure Functions infrastructure has to start up the app. But subsequent calls should be much quicker.

For queued functions, you need to register them via the RegisterQueuedFunction API or by selecting the Queued option on the Register Function page at playfab.com. You will be asked for the name you want to give the function, the name of the queue that function is associated with and the storage account connection string for the storage account the queue lives in. I recommend that you have a storage account just for the queues for any queued functions you register as the connection string gives PlayFab full access to anything in that storage account.

And I'm running out of characters. Continued below...

1 Like 1 ·
Gudge avatar image Gudge gestion commented ·

Continued from previous reply...

Please note that there is no way for Queued functions to return anything to whoever called ExecuteFunction. The response data will always be something like { "StatusCode" = "Accepted", "QueueName" = "MyQueue" }. Queued functions are really designed to be used with PlayStream actions where there is no external caller and so not having a return value doesn't matter. Also the timeout for V1 PlayStream is 1 second and so HTTP functions are quite difficult to use in that case.

Please let me know if you are exceeding the 10 second limit for HTTP functions or if, for some reason (perhaps I have a bug somewhere) your HTTP functions are getting timed out by PlayFab after 1 second.

Regards

Gudge

1 Like 1 ·
gestion avatar image gestion Gudge commented ·

Hi @Gudge,

Thanks for the reply. We were worrying about the wrong execution time. It's over 1 second, but that's the Azure Function itself, so there's no real problem there.

We are now beginning to learn/understand how to invoke PlayFab functionality from within the Azure Function.

Thanks.

0 Likes 0 ·
Gudge avatar image Gudge gestion commented ·

Follow up about getting the FunctionExecutionContext inside the queue triggered function. If you have a string parameter which is the queue message that that string will contain the JSON sent by PlayFab. For example;

[FunctionName("IdentityQueued")]
public static async Task Run(
[QueueTrigger("identity", Connection = "QueueStorage")] string msg,
ILogger log)
{
FunctionExecutionContext ctx = JsonConvert.DeserializeObject<FunctionExecutionContext>(msg);

...
}

Gudge

1 Like 1 ·
gestion avatar image gestion Gudge commented ·

@Gudge thanks again for the reply.

We understand that the message does contain the context. Right now we are moving towards the http triggered functions but we will run some tests as soon as possible.

Thanks.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.