-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDatabaseOutboxRelayHostedServiceBaseT.cs
More file actions
39 lines (34 loc) · 2.02 KB
/
Copy pathDatabaseOutboxRelayHostedServiceBaseT.cs
File metadata and controls
39 lines (34 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace CoreEx.Database.Outbox;
/// <summary>
/// Provides the <see cref="IDatabaseOutboxRelay.RelayAsync(CoreEx.Database.Outbox.DatabaseOutboxRelayArgs, CancellationToken)"/> execution leveraging a <see cref="TimerHostedServiceBase"/>.
/// </summary>
/// <typeparam name="TOutboxRelay">The <see cref="IDatabaseOutboxRelay"/> <see cref="Type"/>.</typeparam>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>.</param>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public abstract class DatabaseOutboxRelayHostedServiceBase<TOutboxRelay>(IServiceProvider serviceProvider, ILogger logger) : DatabaseOutboxRelayHostedServiceBase(serviceProvider, logger) where TOutboxRelay : IDatabaseOutboxRelay
{
/// <summary>
/// Gets or sets the factory method to create the <typeparamref name="TOutboxRelay"/>.
/// </summary>
public Func<IServiceProvider, TOutboxRelay>? RelayFactory { get => field; set => field = SetValueWhenStatusIsInitializedOnly(value); }
/// <inheritdoc/>
protected override async Task<bool> OnExecuteAsync(ExecutionContext executionContext, CancellationToken cancellationToken)
{
// Instantiate the relay via the factory where specified.
var relay = RelayFactory is null
? ExecutionContext.GetRequiredService<TOutboxRelay>()
: RelayFactory(executionContext.ServiceProvider.ThrowIfNull()) ?? throw new InvalidOperationException($"The {typeof(TOutboxRelay).Name} was not be created using the specified {nameof(RelayFactory)}.");
// Create the arguments.
var args = new DatabaseOutboxRelayArgs
{
PartitionPicker = PartitionPicker,
BatchSize = BatchSize,
LeaseDuration = LeaseDuration,
BackOffDuration = BackOffDuration
};
// Execute the relay.
var relayed = await relay.RelayAsync(args, cancellationToken).ConfigureAwait(false);
// Immediately re-execute where work was done (doesn't matter how much); otherwise, sleep.
return relayed;
}
}