Skip to content

Commit b06b690

Browse files
committed
add alternate constuctor arguments to allow supply of provisioned sqs and dynamo clients
1 parent e7219e5 commit b06b690

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

src/providers/WorkflowCore.Providers.AWS/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ services.AddWorkflow(cfg =>
3434
If any AWS resources do not exists, they will be automatcially created. By default, all DynamoDB tables and indexes will be provisioned with a throughput of 1, you can modify these values from the AWS console.
3535
You may also specify a prefix for the dynamo table names.
3636

37+
If you have a preconfigured dynamoClient, you can pass this in instead of the credentials and config
38+
```C#
39+
var client = new AmazonDynamoDBClient();
40+
var sqsClient = new AmazonSQSClient();
41+
services.AddWorkflow(cfg =>
42+
{
43+
cfg.UseAwsDynamoPersistenceWithProvisionedClient(client, "table-prefix");
44+
cfg.UseAwsDynamoLockingWithProvisionedClient(client, "workflow-core-locks");
45+
cfg.UseAwsSimpleQueueServiceWithProvisionedClient(sqsClient, "queues-prefix");
46+
});
47+
```
48+
3749

3850
## Usage (Kinesis)
3951

src/providers/WorkflowCore.Providers.AWS/ServiceCollectionExtensions.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,39 @@ public static class ServiceCollectionExtensions
1515
{
1616
public static WorkflowOptions UseAwsSimpleQueueService(this WorkflowOptions options, AWSCredentials credentials, AmazonSQSConfig config, string queuesPrefix = "workflowcore")
1717
{
18-
options.UseQueueProvider(sp => new SQSQueueProvider(credentials, config, sp.GetService<ILoggerFactory>(), queuesPrefix));
18+
options.UseQueueProvider(sp => new SQSQueueProvider(credentials, config, null, sp.GetService<ILoggerFactory>(), queuesPrefix));
19+
return options;
20+
}
21+
22+
public static WorkflowOptions UseAwsSimpleQueueServiceWithProvisionedClient(this WorkflowOptions options, AmazonSQSClient sqsClient, string queuesPrefix = "workflowcore")
23+
{
24+
options.UseQueueProvider(sp => new SQSQueueProvider(null, null, sqsClient, sp.GetService<ILoggerFactory>(), queuesPrefix));
1925
return options;
2026
}
2127

2228
public static WorkflowOptions UseAwsDynamoLocking(this WorkflowOptions options, AWSCredentials credentials, AmazonDynamoDBConfig config, string tableName)
2329
{
24-
options.UseDistributedLockManager(sp => new DynamoLockProvider(credentials, config, tableName, sp.GetService<ILoggerFactory>(), sp.GetService<IDateTimeProvider>()));
30+
options.UseDistributedLockManager(sp => new DynamoLockProvider(credentials, config, null, tableName, sp.GetService<ILoggerFactory>(), sp.GetService<IDateTimeProvider>()));
31+
return options;
32+
}
33+
34+
public static WorkflowOptions UseAwsDynamoLockingWithProvisionedClient (this WorkflowOptions options, AmazonDynamoDBClient dynamoClient, string tableName)
35+
{
36+
options.UseDistributedLockManager(sp => new DynamoLockProvider(null, null, dynamoClient, tableName, sp.GetService<ILoggerFactory>(), sp.GetService<IDateTimeProvider>()));
2537
return options;
2638
}
2739

2840
public static WorkflowOptions UseAwsDynamoPersistence(this WorkflowOptions options, AWSCredentials credentials, AmazonDynamoDBConfig config, string tablePrefix)
2941
{
30-
options.Services.AddTransient<IDynamoDbProvisioner>(sp => new DynamoDbProvisioner(credentials, config, tablePrefix, sp.GetService<ILoggerFactory>()));
31-
options.UsePersistence(sp => new DynamoPersistenceProvider(credentials, config, sp.GetService<IDynamoDbProvisioner>(), tablePrefix, sp.GetService<ILoggerFactory>()));
42+
options.Services.AddTransient<IDynamoDbProvisioner>(sp => new DynamoDbProvisioner(credentials, config, null, tablePrefix, sp.GetService<ILoggerFactory>()));
43+
options.UsePersistence(sp => new DynamoPersistenceProvider(credentials, config, null, sp.GetService<IDynamoDbProvisioner>(), tablePrefix, sp.GetService<ILoggerFactory>()));
44+
return options;
45+
}
46+
47+
public static WorkflowOptions UseAwsDynamoPersistenceWithProvisionedClient(this WorkflowOptions options, AmazonDynamoDBClient dynamoClient, string tablePrefix)
48+
{
49+
options.Services.AddTransient<IDynamoDbProvisioner>(sp => new DynamoDbProvisioner(null, null, dynamoClient, tablePrefix, sp.GetService<ILoggerFactory>()));
50+
options.UsePersistence(sp => new DynamoPersistenceProvider(null, null, dynamoClient, sp.GetService<IDynamoDbProvisioner>(), tablePrefix, sp.GetService<ILoggerFactory>()));
3251
return options;
3352
}
3453

src/providers/WorkflowCore.Providers.AWS/Services/DynamoDbProvisioner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public class DynamoDbProvisioner : IDynamoDbProvisioner
1515
private readonly IAmazonDynamoDB _client;
1616
private readonly string _tablePrefix;
1717

18-
public DynamoDbProvisioner(AWSCredentials credentials, AmazonDynamoDBConfig config, string tablePrefix, ILoggerFactory logFactory)
18+
public DynamoDbProvisioner(AWSCredentials credentials, AmazonDynamoDBConfig config, AmazonDynamoDBClient dynamoDBClient, string tablePrefix, ILoggerFactory logFactory)
1919
{
2020
_logger = logFactory.CreateLogger<DynamoDbProvisioner>();
21-
_client = new AmazonDynamoDBClient(credentials, config);
21+
_client = dynamoDBClient ?? new AmazonDynamoDBClient(credentials, config);
2222
_tablePrefix = tablePrefix;
2323
}
2424

src/providers/WorkflowCore.Providers.AWS/Services/DynamoLockProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public class DynamoLockProvider : IDistributedLockProvider
2525
private readonly AutoResetEvent _mutex = new AutoResetEvent(true);
2626
private readonly IDateTimeProvider _dateTimeProvider;
2727

28-
public DynamoLockProvider(AWSCredentials credentials, AmazonDynamoDBConfig config, string tableName, ILoggerFactory logFactory, IDateTimeProvider dateTimeProvider)
28+
public DynamoLockProvider(AWSCredentials credentials, AmazonDynamoDBConfig config, AmazonDynamoDBClient dynamoDBClient, string tableName, ILoggerFactory logFactory, IDateTimeProvider dateTimeProvider)
2929
{
3030
_logger = logFactory.CreateLogger<DynamoLockProvider>();
31-
_client = new AmazonDynamoDBClient(credentials, config);
31+
_client = dynamoDBClient ?? new AmazonDynamoDBClient(credentials, config);
3232
_localLocks = new List<string>();
3333
_tableName = tableName;
3434
_nodeId = Guid.NewGuid().ToString();

src/providers/WorkflowCore.Providers.AWS/Services/DynamoPersistenceProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public class DynamoPersistenceProvider : IPersistenceProvider
2626

2727
public bool SupportsScheduledCommands => false;
2828

29-
public DynamoPersistenceProvider(AWSCredentials credentials, AmazonDynamoDBConfig config, IDynamoDbProvisioner provisioner, string tablePrefix, ILoggerFactory logFactory)
29+
public DynamoPersistenceProvider(AWSCredentials credentials, AmazonDynamoDBConfig config, AmazonDynamoDBClient dynamoDBClient, IDynamoDbProvisioner provisioner, string tablePrefix, ILoggerFactory logFactory)
3030
{
3131
_logger = logFactory.CreateLogger<DynamoPersistenceProvider>();
32-
_client = new AmazonDynamoDBClient(credentials, config);
32+
_client = dynamoDBClient ?? new AmazonDynamoDBClient(credentials, config);
3333
_tablePrefix = tablePrefix;
3434
_provisioner = provisioner;
3535
}

src/providers/WorkflowCore.Providers.AWS/Services/SQSQueueProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public class SQSQueueProvider : IQueueProvider
2121

2222
public bool IsDequeueBlocking => true;
2323

24-
public SQSQueueProvider(AWSCredentials credentials, AmazonSQSConfig config, ILoggerFactory logFactory, string queuesPrefix)
24+
public SQSQueueProvider(AWSCredentials credentials, AmazonSQSConfig config, AmazonSQSClient sqsClient, ILoggerFactory logFactory, string queuesPrefix)
2525
{
2626
_logger = logFactory.CreateLogger<SQSQueueProvider>();
27-
_client = new AmazonSQSClient(credentials, config);
27+
_client = sqsClient ?? new AmazonSQSClient(credentials, config);
2828
_queuesPrefix = queuesPrefix;
2929
}
3030

test/WorkflowCore.Tests.DynamoDB/DynamoPersistenceProviderFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ protected override IPersistenceProvider Subject
2626
if (_subject == null)
2727
{
2828
var cfg = new AmazonDynamoDBConfig { ServiceURL = DynamoDbDockerSetup.ConnectionString };
29-
var provisioner = new DynamoDbProvisioner(DynamoDbDockerSetup.Credentials, cfg, "unittests", new LoggerFactory());
30-
var client = new DynamoPersistenceProvider(DynamoDbDockerSetup.Credentials, cfg, provisioner, "unittests", new LoggerFactory());
29+
var provisioner = new DynamoDbProvisioner(DynamoDbDockerSetup.Credentials, cfg, null, "unittests", new LoggerFactory());
30+
var client = new DynamoPersistenceProvider(DynamoDbDockerSetup.Credentials, cfg, null, provisioner, "unittests", new LoggerFactory());
3131
client.EnsureStoreExists();
3232
_subject = client;
3333
}

0 commit comments

Comments
 (0)