refactor: Load IDataStreamFactory(s) with dependency injection
So we can create ViewStreams that depend on other services, config, etc. Previously, the PrimitiveManager relied purely on static construction of types through reflection.
This commit is contained in:
1 parent
7566b2560f
commit
ea0a1fbcd3
59 files changed
+2124
-161
No files matched your search
@@ -4,6 +4,7 @@ using BCG;
|
||||
|
||||
using bts;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.DataStreamExpressions;
|
||||
using Bts.PubSub;
|
||||
@@ -28,13 +29,28 @@ public sealed class DataStreamPublisherService : IHostedService, IDisposable {
|
||||
readonly DataStreamPublisherConfig config;
|
||||
readonly IPubTableBroker pubTableBroker;
|
||||
readonly IWorkScheduler workScheduler;
|
||||
readonly IDataStreamFactory streamFactory;
|
||||
List<IDisposable>? resources = new();
|
||||
|
||||
public DataStreamPublisherService(IPubTableBroker pubTableBroker, IWorkScheduler workScheduler, IOptions<DataStreamPublisherConfig> config)
|
||||
: this(pubTableBroker, workScheduler, config.Value) { }
|
||||
public DataStreamPublisherService(
|
||||
IPubTableBroker pubTableBroker,
|
||||
IEnumerable<IDataStreamFactory> streamFactories,
|
||||
IWorkScheduler workScheduler,
|
||||
IOptions<DataStreamPublisherConfig> config
|
||||
)
|
||||
: this(pubTableBroker, streamFactories, workScheduler, config.Value) { }
|
||||
|
||||
public DataStreamPublisherService(IPubTableBroker pubTableBroker, IWorkScheduler workScheduler, DataStreamPublisherConfig config) {
|
||||
public DataStreamPublisherService(
|
||||
IPubTableBroker pubTableBroker,
|
||||
IEnumerable<IDataStreamFactory> streamFactories,
|
||||
IWorkScheduler workScheduler,
|
||||
DataStreamPublisherConfig config
|
||||
) {
|
||||
this.pubTableBroker = pubTableBroker;
|
||||
var streamFactoryList = streamFactories.ToList();
|
||||
streamFactory = streamFactoryList.Count == 1
|
||||
? streamFactoryList[0]
|
||||
: new CompositeDataStreamFactory(streamFactoryList);
|
||||
this.workScheduler = workScheduler;
|
||||
this.config = config;
|
||||
}
|
||||
@@ -48,7 +64,7 @@ public sealed class DataStreamPublisherService : IHostedService, IDisposable {
|
||||
Log.Gen.warn("Skipping PublishedStream because it doesn't have a name (must define 'Name' in config json).");
|
||||
continue;
|
||||
}
|
||||
var vs = CreateViewStream(publishedStream);
|
||||
var vs = CreateViewStream(streamFactory, publishedStream);
|
||||
if (vs is null) {
|
||||
continue;
|
||||
}
|
||||
@@ -72,7 +88,7 @@ public sealed class DataStreamPublisherService : IHostedService, IDisposable {
|
||||
resources?.Clear();
|
||||
}
|
||||
|
||||
static ViewStream? CreateViewStream(PublishedStream ps) {
|
||||
static ViewStream? CreateViewStream(IDataStreamFactory streamFactory, PublishedStream ps) {
|
||||
Log.Gen.info(
|
||||
$"Instantiating ViewPublisher for: {JsonSerializer.Serialize(ps)}");
|
||||
if (string.IsNullOrEmpty(ps.Name)) {
|
||||
@@ -80,7 +96,7 @@ public sealed class DataStreamPublisherService : IHostedService, IDisposable {
|
||||
$"Failed to instantiate ViewPublisher. Null/empty 'name': {JsonSerializer.Serialize(ps)}");
|
||||
return null;
|
||||
}
|
||||
var vs = buildStream(ps.Expression);
|
||||
var vs = buildStream(streamFactory, ps.Expression);
|
||||
if (vs is null) {
|
||||
Log.Gen.warn(
|
||||
$"Failed to instantiate ViewPublisher. Invalid 'expression': {JsonSerializer.Serialize(ps)}");
|
||||
@@ -89,14 +105,14 @@ public sealed class DataStreamPublisherService : IHostedService, IDisposable {
|
||||
return vs;
|
||||
}
|
||||
|
||||
static ViewStream? buildStream(string? streamExpression) {
|
||||
static ViewStream? buildStream(IDataStreamFactory streamFactory, string? streamExpression) {
|
||||
if (streamExpression is null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Log.Gen.info($"Building ViewStream: {streamExpression}");
|
||||
var exp = DataStreamExpressionsInterpreter.Evaluate(streamExpression);
|
||||
return exp.getView(new Dictionary<string, StatementExpression>(0));
|
||||
return exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
||||
} catch (Exception ex) {
|
||||
Log.assert(false, $"Could not build stream. expression: {streamExpression}; ex: {ex}");
|
||||
return null;
|
||||
|
||||
@@ -19,6 +19,7 @@ public static class DataStreamPublisherServices {
|
||||
.AddPubSubClient(configurationSection.GetSection("PubSub"))
|
||||
.AddPubTableBroker()
|
||||
.AddRemoteQueryManager()
|
||||
.AddPrimitiveStreams(configurationSection.GetSection("PrimitiveStream"))
|
||||
.Configure<DataStreamPublisherConfig>(configurationSection.GetSection("DataStreamPublisher"))
|
||||
.AddHostedService<DataStreamPublisherService>();
|
||||
}
|
||||
Reference in new issue
Block a user