private async Task Loop()

in Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/DlqMonitor.cs [46:115]


        private async Task Loop(CancellationToken token)
        {
            var aws = this._runtime.AWSService;
            while (!token.IsCancellationRequested)
            {
                Message message = null;
                LogRecord logRecord = null;
                try
                {
                    // Read a message from the queue using the ExternalCommands console application.
                    message = await aws.ReadMessageAsync(this._profile, this._region, this._queueUrl);
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    if (message == null)
                    {
                        // Since there are no messages, sleep a bit to wait for messages to come.
                        Thread.Sleep(1000);
                        continue;
                    }

                    // If a message was received execute the Lambda function within the test tool.
                    var request = new ExecutionRequest
                    {
                        AWSProfile = this._profile,
                        AWSRegion = this._region,
                        Function = this._function,
                        Payload = JsonSerializer.Serialize(new
                        {
                            Records = new List<Message>
                            {
                                message
                            }
                        })
                    };

                    var response = await this._runtime.ExecuteLambdaFunctionAsync(request);

                    // Capture the results to send back to the client application.
                    logRecord = new LogRecord
                    {
                        ProcessTime = DateTime.Now,
                        ReceiptHandle = message.ReceiptHandle,
                        Logs = response.Logs,
                        Error = response.Error
                    };
                }
                catch (Exception e)
                {
                    logRecord = new LogRecord
                    {
                        ProcessTime = DateTime.Now,
                        Error = e.Message
                    };

                    Thread.Sleep(1000);
                }

                if (logRecord != null && message != null)
                {
                    logRecord.Event = message.Body;
                }

                lock (LOG_LOCK)
                {
                    this._records.Add(logRecord);
                }
            }
        }