Skip to content

Commit

Permalink
Merge pull request #69 from smiggleworth/sqs-cleanup
Browse files Browse the repository at this point in the history
cache the sqs work providers
  • Loading branch information
smiggleworth authored Feb 13, 2019
2 parents aaa202d + 36706ea commit 6443fd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
39 changes: 22 additions & 17 deletions src/Quidjibo.Aws.Sqs/Factories/SqsWorkProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using Amazon.SQS;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Quidjibo.Aws.Sqs.Configurations;
using Quidjibo.Aws.Sqs.Providers;
Expand All @@ -13,43 +14,47 @@ namespace Quidjibo.Aws.Sqs.Factories
{
public class SqsWorkProviderFactory : IWorkProviderFactory
{
private readonly MemoryCache _providerCache = new MemoryCache(new MemoryCacheOptions());
private readonly ILoggerFactory _loggerFactory;
private readonly SqsQuidjiboConfiguration _sqsQuidjiboConfiguration;
private readonly AmazonSQSClient _client;

public SqsWorkProviderFactory(
ILoggerFactory loggerFactory,
SqsQuidjiboConfiguration sqsQuidjiboConfiguration)
{
_loggerFactory = loggerFactory;
_sqsQuidjiboConfiguration = sqsQuidjiboConfiguration;
_client = new AmazonSQSClient(_sqsQuidjiboConfiguration.Credentials, _sqsQuidjiboConfiguration.AmazonSqsConfig);
}

public async Task<IWorkProvider> CreateAsync(string queues, CancellationToken cancellationToken = default(CancellationToken))
public Task<IWorkProvider> CreateAsync(string queues, CancellationToken cancellationToken = default(CancellationToken))
{
var client = new AmazonSQSClient(_sqsQuidjiboConfiguration.Credentials, _sqsQuidjiboConfiguration.AmazonSqsConfig);
var response = await client.GetQueueUrlAsync(queues, cancellationToken);
if (response.HttpStatusCode != HttpStatusCode.OK)
return _providerCache.GetOrCreateAsync(queues, async e =>
{
throw new InvalidOperationException("Could not load the queues url.");
}

var provider = new SqsWorkProvider(
_loggerFactory.CreateLogger<SqsWorkProvider>(),
client,
response.QueueUrl,
_sqsQuidjiboConfiguration.Type,
_sqsQuidjiboConfiguration.LockInterval,
_sqsQuidjiboConfiguration.BatchSize,
_sqsQuidjiboConfiguration.LongPollDuration);
var response = await _client.GetQueueUrlAsync(queues, cancellationToken);
if (response.HttpStatusCode != HttpStatusCode.OK)
{
throw new InvalidOperationException("Could not load the queues url.");
}

return provider;
var provider = new SqsWorkProvider(
_loggerFactory.CreateLogger<SqsWorkProvider>(),
_client,
response.QueueUrl,
_sqsQuidjiboConfiguration.Type,
_sqsQuidjiboConfiguration.LockInterval,
_sqsQuidjiboConfiguration.BatchSize,
_sqsQuidjiboConfiguration.LongPollDuration);
return (IWorkProvider)provider;
});
}

public Task<IWorkProvider> CreateAsync(string[] queues, CancellationToken cancellationToken = default(CancellationToken))
{
if (queues.Length != 1)
{
throw new NotSupportedException("Each queues requires a seperate listener. Please pass a single queue.");
throw new NotSupportedException("Each queues requires a separate listener. Please pass a single queue.");
}

return CreateAsync(queues[0], cancellationToken);
Expand Down
5 changes: 3 additions & 2 deletions src/Quidjibo.Aws.Sqs/Quidjibo.Aws.Sqs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.SQS" Version="3.3.3.11" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="AWSSDK.SQS" Version="3.3.3.55" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 6443fd3

Please sign in to comment.