From 8bb67dfba247a05eec6dac246f4b1c57ce8cc23d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 15 Jul 2024 15:00:21 -0400 Subject: [PATCH] refactor: Tweak SparkCoreServices to synchronize Configuration.init globally --- .../spark_core/Services/ISparkConfigLoader.cs | 12 +++++++ src/client/spark_core/Services/SparkConfig.cs | 6 ---- .../spark_core/Services/SparkConfigLoader.cs | 27 ++++++++++++++++ .../Services/SparkConfigLoaderOpts.cs | 8 +++++ .../Services/SparkConfigLoaderService.cs | 25 +++++++++++++++ .../spark_core/Services/SparkCoreServices.cs | 8 ++--- .../spark_core/Services/SparkService.cs | 32 ------------------- 7 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 src/client/spark_core/Services/ISparkConfigLoader.cs delete mode 100644 src/client/spark_core/Services/SparkConfig.cs create mode 100644 src/client/spark_core/Services/SparkConfigLoader.cs create mode 100644 src/client/spark_core/Services/SparkConfigLoaderOpts.cs create mode 100644 src/client/spark_core/Services/SparkConfigLoaderService.cs delete mode 100644 src/client/spark_core/Services/SparkService.cs diff --git a/src/client/spark_core/Services/ISparkConfigLoader.cs b/src/client/spark_core/Services/ISparkConfigLoader.cs new file mode 100644 index 000000000..651176f19 --- /dev/null +++ b/src/client/spark_core/Services/ISparkConfigLoader.cs @@ -0,0 +1,12 @@ +#nullable enable +namespace Spark.Core.Services; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// Initializes Spark protobuf config. +/// +public interface ISparkConfigLoader { + Task InitializeAsync(CancellationToken cancellationToken); +} diff --git a/src/client/spark_core/Services/SparkConfig.cs b/src/client/spark_core/Services/SparkConfig.cs deleted file mode 100644 index 8f0976363..000000000 --- a/src/client/spark_core/Services/SparkConfig.cs +++ /dev/null @@ -1,6 +0,0 @@ -#nullable enable -namespace Spark.Core.Services; - -public class SparkConfig { - public string? ConfigServer { get; set; } -} diff --git a/src/client/spark_core/Services/SparkConfigLoader.cs b/src/client/spark_core/Services/SparkConfigLoader.cs new file mode 100644 index 000000000..1a0fb9aa4 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfigLoader.cs @@ -0,0 +1,27 @@ +#nullable enable +namespace Spark.Core.Services; + +using bts; + +using mbts.config; + +using Microsoft.Extensions.Options; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// Initializes Spark protobuf config. +/// +class SparkConfigLoader : ISparkConfigLoader { + readonly SparkConfigLoaderOpts config; + + public SparkConfigLoader(IOptions config) + : this(config.Value) { } + + public SparkConfigLoader(SparkConfigLoaderOpts config) => + this.config = config; + + public async Task InitializeAsync(CancellationToken cancellationToken) => + Log.assert(await Configuration.InitializeAsync(config.ConfigServer, cancellationToken)); +} diff --git a/src/client/spark_core/Services/SparkConfigLoaderOpts.cs b/src/client/spark_core/Services/SparkConfigLoaderOpts.cs new file mode 100644 index 000000000..63db5e489 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfigLoaderOpts.cs @@ -0,0 +1,8 @@ +#nullable enable +namespace Spark.Core.Services; + +public class SparkConfigLoaderOpts { + public string? ConfigServer { get; set; } + + public static SparkConfigLoaderOpts Default() => new(); +} diff --git a/src/client/spark_core/Services/SparkConfigLoaderService.cs b/src/client/spark_core/Services/SparkConfigLoaderService.cs new file mode 100644 index 000000000..6dc710242 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfigLoaderService.cs @@ -0,0 +1,25 @@ +#nullable enable +namespace Spark.Core.Services; + +using Microsoft.Extensions.Hosting; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// An facade that initializes Spark protobuf config, logging, and +/// quickbus for both c# and cpp. +/// +public class SparkConfigLoaderService : IHostedService { + readonly ISparkConfigLoader loader; + + public SparkConfigLoaderService(ISparkConfigLoader loader) { + this.loader = loader; + } + + public Task StartAsync(CancellationToken cancellationToken) => + loader.InitializeAsync(cancellationToken); + + public Task StopAsync(CancellationToken cancellationToken) => + Task.CompletedTask; +} diff --git a/src/client/spark_core/Services/SparkCoreServices.cs b/src/client/spark_core/Services/SparkCoreServices.cs index 93e5fe2cb..9cbac82a7 100644 --- a/src/client/spark_core/Services/SparkCoreServices.cs +++ b/src/client/spark_core/Services/SparkCoreServices.cs @@ -8,11 +8,11 @@ using System.Linq; public static class SparkCoreServices { public static IServiceCollection AddSpark(this IServiceCollection services, IConfiguration configurationSection) { - if (services.Any(s => s.ServiceType == typeof(SparkService))) { + if (services.Any(s => s.ServiceType == typeof(SparkConfigLoader))) { throw new InvalidOperationException("SparkService has already been added"); } - return services.Configure(configurationSection) - .AddSingleton() - .AddHostedService(sp => sp.GetRequiredService()); + return services.Configure(configurationSection) + .AddSingleton() + .AddHostedService(sp => sp.GetRequiredService()); } } diff --git a/src/client/spark_core/Services/SparkService.cs b/src/client/spark_core/Services/SparkService.cs deleted file mode 100644 index 8a5e6d9b6..000000000 --- a/src/client/spark_core/Services/SparkService.cs +++ /dev/null @@ -1,32 +0,0 @@ -#nullable enable -namespace Spark.Core.Services; - -using mbts.config; - -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; - -using System.Threading; -using System.Threading.Tasks; - -/// -/// An facade that initializes Spark protobuf config, logging, and -/// quickbus for both c# and cpp. -/// -public class SparkService : IHostedService { - readonly SparkConfig config; - - public SparkService(IOptions config) - : this(config.Value) { } - - public SparkService(SparkConfig config) => - this.config = config; - - public Task StartAsync(CancellationToken cancellationToken) { - Configuration.initialize(config.ConfigServer); - return Task.CompletedTask; - } - - public Task StopAsync(CancellationToken cancellationToken) => - Task.CompletedTask; -}