refactor: Tweak SparkCoreServices to synchronize Configuration.init globally
This commit is contained in:
1 parent
f922215cae
commit
8bb67dfba2
7 files changed
+76
-42
No files matched your search
@@ -0,0 +1,12 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Services;
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes Spark protobuf config.
|
||||
/// </summary>
|
||||
public interface ISparkConfigLoader {
|
||||
Task InitializeAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Services;
|
||||
|
||||
public class SparkConfig {
|
||||
public string? ConfigServer { get; set; }
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes Spark protobuf config.
|
||||
/// </summary>
|
||||
class SparkConfigLoader : ISparkConfigLoader {
|
||||
readonly SparkConfigLoaderOpts config;
|
||||
|
||||
public SparkConfigLoader(IOptions<SparkConfigLoaderOpts> 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));
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Services;
|
||||
|
||||
public class SparkConfigLoaderOpts {
|
||||
public string? ConfigServer { get; set; }
|
||||
|
||||
public static SparkConfigLoaderOpts Default() => new();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Services;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IHostedService"/> facade that initializes Spark protobuf config, logging, and
|
||||
/// quickbus for both c# and cpp.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
@@ -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<SparkConfig>(configurationSection)
|
||||
.AddSingleton<SparkService>()
|
||||
.AddHostedService(sp => sp.GetRequiredService<SparkService>());
|
||||
return services.Configure<SparkConfigLoaderOpts>(configurationSection)
|
||||
.AddSingleton<SparkConfigLoader>()
|
||||
.AddHostedService(sp => sp.GetRequiredService<SparkConfigLoaderService>());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IHostedService"/> facade that initializes Spark protobuf config, logging, and
|
||||
/// quickbus for both c# and cpp.
|
||||
/// </summary>
|
||||
public class SparkService : IHostedService {
|
||||
readonly SparkConfig config;
|
||||
|
||||
public SparkService(IOptions<SparkConfig> 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;
|
||||
}
|
||||
Reference in new issue
Block a user