question

Jerry avatar image
Jerry asked

Scheduled Task args through cloudscript Azure function

Hi,

so im new to the cloud scripting thing, and my knowledge of JavaScript isnt the best, and i more C# friendly, i have a working azure function that i can execute from both the azure console and the playfab scheduled task. but only the arguments in azure go through, and the playfab doesn't. could anyone tell me what im doing wrong.

thanks

[FunctionName("X")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string responseMessage = "";
            int rate;
            PlayFabSettings.staticSettings.TitleId = "X";
            PlayFabSettings.staticSettings.DeveloperSecretKey = "X";

            GetPlayersInSegmentRequest request = new GetPlayersInSegmentRequest()
            {
                SegmentId = "X"
            };          
            var getAllPlayersResult = await PlayFabServerAPI.GetPlayersInSegmentAsync(request);
            
            try{rate = int.Parse(req.Query["Rate"]);          
	    catch{rate = 375;}

            
            responseMessage += "\nrate is set to " + rate;
            log.LogInformation("C# HTTP trigger function processed a request.");

// removed cause i couldnt get it to work

/*
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            PostData data = (PostData)JsonConvert.DeserializeObject(requestBody);

            responseMessage += "\nDatarate is set to " + data.Rate;
*/
            return new OkObjectResult(responseMessage);
        }
    }

CloudScriptscheduled tasks
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

·
Citrus Yan avatar image
Citrus Yan answered

PlayFab uses the “ScheduledTaskFunctionExecutionContext” context model when executing Azure Function via Scheduled Task, please check out this section for more details: https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context#use-the-context-model-when-executing-via-scheduled-task. Therefore, to access function arguments executed via Scheduled Task, you’d need to use the correct context model, here is a function example you may find helpful:

[FunctionName("CSAFForScheduledTask")]
        public static async Task<object> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]  HttpRequest httpRequest,
            ILogger log)
        {


            string body = await httpRequest.ReadAsStringAsync();
            log.LogInformation($"HTTP POST Body: {body}");


            ScheduledTaskFunctionExecutionContext<object> ctx = JsonConvert.DeserializeObject<ScheduledTaskFunctionExecutionContext<object>>(body);
            log.LogInformation($"ScheduledTaskFunctionExecutionContext: {JsonConvert.SerializeObject(ctx)}");


            return await Task.FromResult(ctx.FunctionArgument); // return function arguments
    
        }


10 |1200

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

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.