Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 53 additions & 36 deletions src/CloudPrint.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
else
{
builder.Services.AddSingleton<IRawPrinter, RawPrinter>();
builder.Services.AddSingleton<IDocumentPrinter, DocumentPrinter>();

Check warning on line 131 in src/CloudPrint.Service/Program.cs

View workflow job for this annotation

GitHub Actions / windows

This call site is reachable on all platforms. 'DocumentPrinter' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 131 in src/CloudPrint.Service/Program.cs

View workflow job for this annotation

GitHub Actions / windows

This call site is reachable on all platforms. 'DocumentPrinter' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 131 in src/CloudPrint.Service/Program.cs

View workflow job for this annotation

GitHub Actions / windows

This call site is reachable on all platforms. 'DocumentPrinter' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
builder.Services.AddSingleton<IPdfPrinter, PdfPrinter>();

Check warning on line 132 in src/CloudPrint.Service/Program.cs

View workflow job for this annotation

GitHub Actions / windows

This call site is reachable on all platforms. 'PdfPrinter' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 132 in src/CloudPrint.Service/Program.cs

View workflow job for this annotation

GitHub Actions / windows

This call site is reachable on all platforms. 'PdfPrinter' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 132 in src/CloudPrint.Service/Program.cs

View workflow job for this annotation

GitHub Actions / windows

This call site is reachable on all platforms. 'PdfPrinter' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
#endif

Expand Down Expand Up @@ -616,64 +616,81 @@
using var sqsClient = new AmazonSQSClient(
accessKey, secretKey, RegionEndpoint.GetBySystemName(region));

// Create DLQ first
var queueTags = tags ?? new Dictionary<string, string>();

// Tags are applied separately (via TagQueue below) rather than passed into
// CreateQueue. SQS rejects CreateQueue when a queue of the same name already
// exists with different tags ("a queue already exists with the same name and
// different tags"), so provisioning must not depend on tags matching at create
// time. TagQueue is additive and never fails on an existing queue.

// Create the DLQ first, reusing it if it already exists.
var dlqName = $"{queueName}-dlq";
var dlqResponse = await sqsClient.CreateQueueAsync(new CreateQueueRequest
{
QueueName = dlqName,
Tags = tags ?? new Dictionary<string, string>()
});
var dlqUrl = await EnsureQueue(dlqName, attributes: null);

// Get DLQ ARN
var dlqAttributes = await sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest
{
QueueUrl = dlqResponse.QueueUrl,
QueueUrl = dlqUrl,
AttributeNames = ["QueueArn"]
});
var dlqArn = dlqAttributes.Attributes["QueueArn"];

// Create or update main queue with redrive policy
string queueUrl;
try
// Create the main queue with its redrive policy, reusing it if it already exists.
var queueUrl = await EnsureQueue(queueName, new Dictionary<string, string>
{
["RedrivePolicy"] = JsonSerializer.Serialize(new { deadLetterTargetArn = dlqArn, maxReceiveCount = 5 })
});

// Apply tags additively to both queues — safe whether they were just created or
// already existed (TagQueue overwrites matching keys and adds new ones).
await TagIfAny(dlqUrl);
await TagIfAny(queueUrl);

Console.WriteLine(queueUrl);
return 0;

// Creates the named queue with the given attributes, or reuses an existing queue
// and brings its attributes in line. Tags are handled separately by TagIfAny so a
// tag mismatch on an existing queue can't fail provisioning.
async Task<string> EnsureQueue(string name, Dictionary<string, string>? attributes)
{
var response = await sqsClient.CreateQueueAsync(new CreateQueueRequest
try
{
QueueName = queueName,
Attributes = new Dictionary<string, string>
var response = await sqsClient.CreateQueueAsync(new CreateQueueRequest
{
["RedrivePolicy"] = JsonSerializer.Serialize(new { deadLetterTargetArn = dlqArn, maxReceiveCount = 5 })
},
Tags = tags ?? new Dictionary<string, string>()
});
queueUrl = response.QueueUrl;
}
catch (AmazonSQSException ex) when (ex.ErrorCode == "QueueAlreadyExists")
{
var urlResponse = await sqsClient.GetQueueUrlAsync(queueName);
queueUrl = urlResponse.QueueUrl;

await sqsClient.SetQueueAttributesAsync(new SetQueueAttributesRequest
QueueName = name,
Attributes = attributes ?? new Dictionary<string, string>()
});
return response.QueueUrl;
}
catch (QueueNameExistsException)
{
QueueUrl = queueUrl,
Attributes = new Dictionary<string, string>
// Same name, different attributes — reuse the queue and update its attributes.
var url = (await sqsClient.GetQueueUrlAsync(name)).QueueUrl;
if (attributes is { Count: > 0 })
{
["RedrivePolicy"] = JsonSerializer.Serialize(new { deadLetterTargetArn = dlqArn, maxReceiveCount = 5 })
await sqsClient.SetQueueAttributesAsync(new SetQueueAttributesRequest
{
QueueUrl = url,
Attributes = attributes
});
}
});
return url;
}
}

// Apply tags to the existing queue (additive — doesn't remove other tags)
if (tags is { Count: > 0 })
async Task TagIfAny(string url)
{
if (queueTags.Count > 0)
{
await sqsClient.TagQueueAsync(new TagQueueRequest
{
QueueUrl = queueUrl,
Tags = tags
QueueUrl = url,
Tags = queueTags
});
}
}

Console.WriteLine(queueUrl);
return 0;
}
catch (Exception ex)
{
Expand Down
Loading