private void HandleWaitingForTermination()

in UnityProject/Assets/Scripts/Server/Server.cs [324:366]


    private void HandleWaitingForTermination()
    {
        this.waitingForTerminateCounter += Time.deltaTime;
        // Check the status every 5 seconds
        if (waitingForTerminateCounter > 5.0f)
        {
            this.waitingForTerminateCounter = 0.0f;

            Debug.Log("Waiting for other servers in the Task to finish...");

            var lambdaConfig = new AmazonLambdaConfig() { RegionEndpoint = this.regionEndpoint };
            var lambdaClient = new Amazon.Lambda.AmazonLambdaClient(lambdaConfig);

            // Call Lambda function to check if we should terminate
            var taskStatusRequestData = new TaskStatusData();
            taskStatusRequestData.taskArn = this.taskDataArn;
            var request = new Amazon.Lambda.Model.InvokeRequest()
            {
                FunctionName = "FargateGameServersCheckIfAllContainersInTaskAreDone",
                Payload = JsonConvert.SerializeObject(taskStatusRequestData),
                InvocationType = InvocationType.RequestResponse
            };

            // As we are not doing anything else on the server anymore, we can just wait for the invoke response
            var invokeResponse = lambdaClient.InvokeAsync(request);
            invokeResponse.Wait();
            invokeResponse.Result.Payload.Position = 0;
            var sr = new StreamReader(invokeResponse.Result.Payload);
            var responseString = sr.ReadToEnd();

            Debug.Log("Got response: " + responseString);

            // Try catching to boolean, if it was a failure, this will also result in false
            var allServersInTaskDone = false;
            bool.TryParse(responseString, out allServersInTaskDone);

            if (allServersInTaskDone)
            {
                Debug.Log("All servers in the Task done running full amount of sessions --> Terminate");
                Application.Quit();
            }
        }
    }