partly or unimplemented features - now implemented
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Provcache.Invalidation;
|
||||
|
||||
namespace StellaOps.Provcache;
|
||||
|
||||
/// <summary>
|
||||
/// Hosted service that manages the lifecycle of registered <see cref="IProvcacheInvalidator"/> instances,
|
||||
/// starting them on application startup and stopping them on shutdown.
|
||||
/// </summary>
|
||||
public sealed class InvalidatorHostedService : IHostedService
|
||||
{
|
||||
private readonly IReadOnlyList<IProvcacheInvalidator> _invalidators;
|
||||
private readonly ILogger<InvalidatorHostedService> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="InvalidatorHostedService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="invalidators">The registered invalidator instances.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public InvalidatorHostedService(
|
||||
IEnumerable<IProvcacheInvalidator> invalidators,
|
||||
ILogger<InvalidatorHostedService> logger)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(invalidators);
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_invalidators = invalidators.ToArray();
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_invalidators.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("No Provcache invalidators registered; skipping startup");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Starting {Count} Provcache invalidator(s)", _invalidators.Count);
|
||||
|
||||
foreach (var invalidator in _invalidators)
|
||||
{
|
||||
await invalidator.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
_logger.LogInformation("All Provcache invalidators started");
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_invalidators.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Stopping {Count} Provcache invalidator(s)", _invalidators.Count);
|
||||
|
||||
foreach (var invalidator in _invalidators.Reverse())
|
||||
{
|
||||
await invalidator.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
_logger.LogInformation("All Provcache invalidators stopped");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using StellaOps.Messaging;
|
||||
using StellaOps.Messaging.Abstractions;
|
||||
using StellaOps.Provcache.Events;
|
||||
using StellaOps.Provcache.Invalidation;
|
||||
|
||||
namespace StellaOps.Provcache;
|
||||
|
||||
public static partial class ProvcacheServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds event-driven invalidator services for Provcache.
|
||||
/// </summary>
|
||||
/// <param name="services">The service collection.</param>
|
||||
/// <returns>The service collection for chaining.</returns>
|
||||
public static IServiceCollection AddProvcacheInvalidators(this IServiceCollection services)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
|
||||
services.TryAddSingleton<IEventStream<SignerRevokedEvent>>(sp =>
|
||||
{
|
||||
var factory = sp.GetRequiredService<IEventStreamFactory>();
|
||||
return factory.Create<SignerRevokedEvent>(new EventStreamOptions
|
||||
{
|
||||
StreamName = SignerRevokedEvent.StreamName
|
||||
});
|
||||
});
|
||||
|
||||
services.TryAddSingleton<IEventStream<FeedEpochAdvancedEvent>>(sp =>
|
||||
{
|
||||
var factory = sp.GetRequiredService<IEventStreamFactory>();
|
||||
return factory.Create<FeedEpochAdvancedEvent>(new EventStreamOptions
|
||||
{
|
||||
StreamName = FeedEpochAdvancedEvent.StreamName
|
||||
});
|
||||
});
|
||||
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IProvcacheInvalidator, SignerSetInvalidator>());
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IProvcacheInvalidator, FeedEpochInvalidator>());
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, InvalidatorHostedService>());
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ public static partial class ProvcacheServiceCollectionExtensions
|
||||
services.AddSingleton<IWriteBehindQueue, WriteBehindQueue>();
|
||||
services.AddHostedService<WriteBehindQueueHostedService>();
|
||||
services.AddHttpClient(HttpChunkFetcher.HttpClientName);
|
||||
services.AddProvcacheInvalidators();
|
||||
|
||||
return services;
|
||||
}
|
||||
@@ -76,6 +77,7 @@ public static partial class ProvcacheServiceCollectionExtensions
|
||||
services.AddSingleton<IWriteBehindQueue, WriteBehindQueue>();
|
||||
services.AddHostedService<WriteBehindQueueHostedService>();
|
||||
services.AddHttpClient(HttpChunkFetcher.HttpClientName);
|
||||
services.AddProvcacheInvalidators();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user