Skip to content

Commit

Permalink
refactor(snapshot): clean up modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Jan 13, 2025
1 parent 795c1cc commit 672f45b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace BuildingRegistry.Producer.Snapshot.Oslo.Infrastructure.Modules
using Be.Vlaanderen.Basisregisters.GrAr.Oslo.SnapshotProducer;
using Be.Vlaanderen.Basisregisters.MessageHandling.Kafka;
using Be.Vlaanderen.Basisregisters.MessageHandling.Kafka.Producer;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.SqlServer.MigrationExtensions;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.SqlStreamStore.Autofac;
using Be.Vlaanderen.Basisregisters.Projector;
using Be.Vlaanderen.Basisregisters.Projector.ConnectedProjections;
using Be.Vlaanderen.Basisregisters.Projector.Modules;
using BuildingRegistry.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -64,19 +66,33 @@ private void RegisterProjectionSetup(ContainerBuilder builder)
.RegisterEventstreamModule(_configuration)
.RegisterModule(new ProjectorModule(_configuration));

var logger = _loggerFactory.CreateLogger<ProducerModule>();
var connectionString = _configuration.GetConnectionString("ProducerSnapshotProjections");

var hasConnectionString = !string.IsNullOrWhiteSpace(connectionString);
if (hasConnectionString)
{
RunOnSqlServer(_services, _loggerFactory, connectionString);
}
else
{
RunInMemoryDb(_services, _loggerFactory, logger);
}

logger.LogInformation(
"Added {Context} to services:" +
Environment.NewLine +
"\tSchema: {Schema}" +
Environment.NewLine +
"\tTableName: {TableName}",
nameof(ProducerContext), Schema.ProducerSnapshotOslo, MigrationTables.ProducerSnapshotOslo);

RegisterProjections(builder);
//RegisterReproducers();
}

private void RegisterProjections(ContainerBuilder builder)
{
builder
.RegisterModule(
new ProducerModule(
_configuration,
_services,
_loggerFactory));

var connectedProjectionSettings = ConnectedProjectionSettings.Configure(x =>
{
x.ConfigureCatchUpPageSize(ConnectedProjectionSettings.Default.CatchUpPageSize);
Expand Down Expand Up @@ -231,5 +247,34 @@ private ProducerOptions CreateBuildingUnitProducerOptions()

return producerOptions;
}

private static void RunOnSqlServer(
IServiceCollection services,
ILoggerFactory loggerFactory,
string producerSnapshotConnectionString)
{
services
.AddDbContext<ProducerContext>((_, options) => options
.UseLoggerFactory(loggerFactory)
.UseSqlServer(producerSnapshotConnectionString, sqlServerOptions =>
{
sqlServerOptions.EnableRetryOnFailure();
sqlServerOptions.MigrationsHistoryTable(MigrationTables.ProducerSnapshotOslo, Schema.ProducerSnapshotOslo);
})
.UseExtendedSqlServerMigrations());
}

private static void RunInMemoryDb(
IServiceCollection services,
ILoggerFactory loggerFactory,
ILogger logger)
{
services
.AddDbContext<ProducerContext>(options => options
.UseLoggerFactory(loggerFactory)
.UseInMemoryDatabase(Guid.NewGuid().ToString(), sqlServerOptions => { }));

logger.LogWarning("Running InMemory for {Context}!", nameof(ProducerContext));
}
}
}
70 changes: 0 additions & 70 deletions src/BuildingRegistry.Producer.Snapshot.Oslo/ProducerModule.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
_logger.LogInformation("Starting snapshot projections");
await _projectionsManager.Start(stoppingToken);
}
catch (Exception exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,6 @@ private void RegisterProjections(ContainerBuilder builder)
.RegisterProjectionMigrator<ProducerContextMigrationFactory>(
_configuration,
_loggerFactory)
// TODO: see if we still need to implements this
// .RegisterProjections<ProducerProjections, ProducerContext>(() =>
// {
// var topic = $"{_configuration[ProducerProjections.TopicKey]}" ?? throw new ArgumentException($"Configuration has no value for {ProducerProjections.TopicKey}");
// var producerOptions = new ProducerOptions(
// new BootstrapServers(bootstrapServers),
// new Topic(topic),
// true,
// EventsJsonSerializerSettingsProvider.CreateSerializerSettings())
// .ConfigureEnableIdempotence();
// if (!string.IsNullOrEmpty(saslUserName)
// && !string.IsNullOrEmpty(saslPassword))
// {
// producerOptions.ConfigureSaslAuthentication(new SaslAuthentication(
// saslUserName,
// saslPassword));
// }
//
// return new ProducerProjections(new Producer(producerOptions));
// }, connectedProjectionSettings)
.RegisterProjections<ProducerMigrateProjections, ProducerContext>(() =>
{
var topic = $"{_configuration[ProducerMigrateProjections.TopicKey]}" ?? throw new ArgumentException($"Configuration has no value for {ProducerMigrateProjections.TopicKey}");
Expand Down

0 comments on commit 672f45b

Please sign in to comment.