in Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/SQSEventSource/SQSEventSourceProcess.cs [31:102]
public static SQSEventSourceProcess Startup(RunCommandSettings settings, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(settings.SQSEventSourceConfig))
{
throw new InvalidOperationException($"The {nameof(RunCommandSettings.SQSEventSourceConfig)} can not be null when starting the SQS event source process");
}
var sqsEventSourceConfigs = LoadSQSEventSourceConfig(settings.SQSEventSourceConfig);
var tasks = new List<Task>();
// Create a separate SQSEventSourceBackgroundService for each SQS event source config listed in the SQSEventSourceConfig
foreach (var sqsEventSourceConfig in sqsEventSourceConfigs)
{
var builder = Host.CreateApplicationBuilder();
var sqsConfig = new AmazonSQSConfig();
if (!string.IsNullOrEmpty(sqsEventSourceConfig.Profile))
{
sqsConfig.Profile = new Profile(sqsEventSourceConfig.Profile);
}
if (!string.IsNullOrEmpty(sqsEventSourceConfig.Region))
{
sqsConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(sqsEventSourceConfig.Region);
}
var sqsClient = new AmazonSQSClient(sqsConfig);
builder.Services.AddSingleton<IAmazonSQS>(sqsClient);
builder.Services.AddSingleton<ILambdaClient, LambdaClient>();
var queueUrl = sqsEventSourceConfig.QueueUrl;
if (string.IsNullOrEmpty(queueUrl))
{
throw new InvalidOperationException("QueueUrl is a required property for SQS event source config");
}
var lambdaRuntimeApi = sqsEventSourceConfig.LambdaRuntimeApi;
if (string.IsNullOrEmpty(lambdaRuntimeApi))
{
if (!settings.LambdaEmulatorPort.HasValue)
{
throw new InvalidOperationException("No Lambda runtime api endpoint was given as part of the SQS event source config and the current " +
"instance of the test tool is not running the Lambda runtime api. Either provide a Lambda runtime api endpoint or set a port for " +
"the lambda runtime api when starting the test tool.");
}
lambdaRuntimeApi = $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}/";
}
var backgroundServiceConfig = new SQSEventSourceBackgroundServiceConfig
{
BatchSize = sqsEventSourceConfig.BatchSize ?? DefaultBatchSize,
DisableMessageDelete = sqsEventSourceConfig.DisableMessageDelete ?? false,
FunctionName = sqsEventSourceConfig.FunctionName ?? LambdaRuntimeApi.DefaultFunctionName,
LambdaRuntimeApi = lambdaRuntimeApi,
QueueUrl = queueUrl,
VisibilityTimeout = sqsEventSourceConfig.VisibilityTimeout ?? DefaultVisiblityTimeout
};
builder.Services.AddSingleton(backgroundServiceConfig);
builder.Services.AddHostedService<SQSEventSourceBackgroundService>();
var app = builder.Build();
var task = app.RunAsync(cancellationToken);
tasks.Add(task);
}
return new SQSEventSourceProcess
{
RunningTask = Task.WhenAll(tasks)
};
}