refactor: Add Api Server service modules for Dependency Injection

* Part of a broader paradigm shift towards Dependency Injection and
  Factory Pattern.
* Adds "api-server" service to docker compose
* Replaces MarketDataClient w/ IMarketDataClient in a few streams
* Replaces ViewStreamDatabase w/ IWorkScheduler in a few streams
This commit is contained in:
John Colvin committed 2026-01-04 21:08:12 +00:00
1 parent 997647e917
commit 856cd552eb
82 files changed
+2368 -1079

No files matched your search

+61 -112
View File
@@ -1,121 +1,70 @@
namespace spark_api_server {
using BCG;
using bts;
using Bts.PubSub;
using Bts.Websocket;
using Bts.DataStreamCore.Services;
using Bts.Extensions.Hosting;
using Bts.PubSub.Services;
using bts;
using bts.api.core;
using CommandLine;
using CommandLine;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Spark.Api.Server;
using Spark.Api.Services;
using Spark.Core.Services;
using Spark.Api.Server;
using Spark.Api.Services;
using Spark.Core;
using Spark.Core.Services;
using Spark.WebSocket.Services;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Threading;
using bts.md;
using MathNet.Numerics.Optimization;
using System;
using System.Threading.Tasks;
class Program {
[Flags]
enum Errors {
None = 0,
Listen = 1,
// Add errors as needed
}
var res = Parser.Default.ParseArguments<RunOptions, DumpSecuritiesOptions, DumpBooksOptions>(args);
await res.MapResult<RunOptions, DumpSecuritiesOptions, DumpBooksOptions, Task<int>>(
Run, SecurityDumper.Run, BookDumper.Run, errs => Task.FromResult(1));
static async Task<int> Main(string[] args) {
var res = Parser.Default.ParseArguments<RunOptions, DumpSecuritiesOptions, DumpBooksOptions>(args);
return await res.MapResult<RunOptions, DumpSecuritiesOptions, DumpBooksOptions, Task<int>>(
_run, SecurityDumper.run, BookDumper.run, errs => Task.FromResult(1));
}
static async Task<int> _run(RunOptions opts) {
Console.WriteLine("Starting spark_api_server... ");
opts.MergeWithConfigFile();
opts.MergeWith(RunOptions.Default());
Log.notnull(opts.user);
Log.notnull(opts.password);
PubSubConfig.SetGlobalConfig(Log.notnull(opts.PubSub));
var cfgLoader = new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = opts.sparkd } );
var serviceCtx = new ServiceContext(cfgLoader);
var loginService = new SparkdLoginService(
serviceCtx, new SparkApiCredentials() { User = opts.user, Password = opts.password }, cfgLoader);
await loginService.LoginAsync();
PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5))));
var userDataPermissions = new UserDataPermissions(loginService);
var sync = new bts.websocket_core.WebsocketDataSync(serviceCtx, userDataPermissions);
var gwy = new bts.websocket_core.WebsocketConnectionGateway();
var prms = new WebsocketServerConfig {
DefaultEndPoint = "/",
Host = opts.host,
Scheme = "http",
Port = opts.port,
KeepAliveIntervalSecs = 0.1,
};
gwy.init(sync);
await sync.initAsync(gwy);
var errors = Errors.None;
Log.Api.info("Starting websocket server...");
// try {
// await gwy.start_listening(prms);
// Log.Api.info("Websocket server stopped without error");
// } catch (Exception ex) {
// Log.Api.error($"Websocket server threw ex: {ex}");
// errors |= Errors.Listen;
// }
//REMOVE IN PRODUCTION AND UNCOMMENT ABOVE CODE
var cancellationTokenSource = new CancellationTokenSource();
var listen = Task.Run(async () => {
Log.Api.info("Starting websocket server...");
try {
await gwy.start_listening(prms);
Log.Api.info("Websocket server stopped without error");
} catch (Exception ex) {
Log.Api.error($"Websocket server threw ex: {ex}");
errors |= Errors.Listen;
}
});
var vsdTask = Task.Run(() => {
Log.Gen.info("Starting ViewStreamDatabase work loop");
var cancellationToken = cancellationTokenSource.Token;
var counter = 0;
const int ERROR_LIMIT = 4;
while (!cancellationToken.IsCancellationRequested) {
try {
ViewStreamDatabase.instance.doWork();
counter = 0;
} catch (Exception ex) {
counter++;
Log.Gen.error(ex.Message);
}
if (counter >= ERROR_LIMIT) {
throw new Exception("counter limit reached");
}
Thread.Sleep(100);
}
}, cancellationTokenSource.Token);
// var task = Task.Run(() => {
// while (!cancellationTokenSource.IsCancellationRequested) {
// // Do something that can be interrupted
// }
// }, cancellationTokenSource.Token);
// Console.WriteLine("Press any key to stop the server...");
// Console.ReadKey();
// cancellationTokenSource.Cancel();
await Task.WhenAny(listen, vsdTask);
//UPTILL HERE
// These tasks aren't designed to finish naturally. If the code reaches here, it means one
// of them threw an exception. Cancel the other task to exit (somewhat) gracefully.
cancellationTokenSource.Cancel();
return (int)errors;
}
static Task<int> Run(RunOptions opts) {
const string appName = "spark_api_server";
var builder = WebApplication.CreateBuilder(new WebApplicationOptions {
Args = Environment.GetCommandLineArgs(),
ApplicationName = appName,
});
HostBuilderExtensions.ConfigureApp(builder.Configuration, builder.Environment.EnvironmentName, appName);
ConfigureServices(builder.Services, builder.Configuration);
var app = builder.Build();
app.UseWebSockets(app.Configuration.GetValue<WebSocketOptions>("WebSocketOptions") ?? new() { KeepAliveInterval = TimeSpan.FromMinutes(2) });
var gateway = new Lazy<ISparkApiWebSocketServer>(app.Services.GetRequiredService<ISparkApiWebSocketServer>);
app.Map("/", HandleRequest);
var errors = Errors.None;
Log.Api.info("Starting websocket server...");
try {
app.Run();
Log.Api.info("Websocket server stopped without error");
} catch (Exception ex) {
Log.Api.error($"Websocket server threw ex: {ex}");
errors |= Errors.Listen;
}
return Task.FromResult((int)errors);
Task HandleRequest(HttpContext context) =>
gateway.Value.HandleRequestAsync(context);
}
static void ConfigureServices(IServiceCollection services, IConfiguration config) {
services.Configure<WebSocketOptions>(config.GetSection("WebSocketOptions"))
.AddLogging(bd => HostBuilderExtensions.ConfigureLogging(bd, new SparkLoggerProvider()))
.AddSpark(config.GetSection("Spark"))
.AddSparkApi(config.GetSection("SparkApi"))
.AddPubSubClient(config.GetSection("PubSub"))
.AddViewStreamDatabase()
.AddMasterWorker(config.GetSection("MasterWorker"))
.AddWebSocketServer();
}
[Flags]
enum Errors {
None = 0,
Listen = 1,
// Add errors as needed
}