Add PubSub service modules designed for Dependency Injection
* Get rid of JsonPropertyName attributes in PubSubConfig
- The Dependency Injection framework (based around dotnet ApplicationBuilder)
does not support this attribute, and maps property names verbatim to json fields.
* Change PubSubServer json fields to match cs class property names verbatim
This commit is contained in:
1 parent
b9125fb44b
commit
f51d0ae973
40 files changed
+979
-363
No files matched your search
@@ -1,83 +1,46 @@
|
||||
using BCG;
|
||||
using Bts.PubSub.Services;
|
||||
|
||||
using bts;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSubServer;
|
||||
|
||||
using CommandLine;
|
||||
|
||||
using mbts.config;
|
||||
|
||||
using Spark.Core;
|
||||
using Spark.Core.Services;
|
||||
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
Parser.Default.ParseArguments<PubSubServerConfig>(args)
|
||||
.MapResult(Run, errs => 1);
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration(ConfigureApp)
|
||||
.ConfigureServices(ConfigureServices)
|
||||
.Build();
|
||||
try {
|
||||
Console.Write($"Starting Application");
|
||||
host.Run();
|
||||
Console.Write($"Application exited gracefully.");
|
||||
} catch (Exception ex) {
|
||||
Console.Error.Write($"Application crashed: {ex}");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
int Run(PubSubServerConfig cfg) {
|
||||
try {
|
||||
cfg.MergeWithConfigFile();
|
||||
cfg.MergeWith(PubSubServerConfig.Default());
|
||||
PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub));
|
||||
Configuration.initialize(cfg.Sparkd);
|
||||
bool run_echo_server = false;
|
||||
bool echo_msg_back = false;
|
||||
cfg.SimpleEchoServer = run_echo_server;
|
||||
return cfg.SimpleEchoServer ?? false ?
|
||||
RunEchoServer(cfg.PubSub.ServerPort.assert_value(), echo_msg_back) :
|
||||
RunPubSubServer();
|
||||
} catch (Exception ex) {
|
||||
// Write the error to console first, in case the logging system is compromised.
|
||||
var error_msg = $"PubSubServer threw exception:\n{ex}";
|
||||
Console.Error.WriteLine(error_msg);
|
||||
Console.Error.Flush();
|
||||
Log.Gen.error(error_msg);
|
||||
return 1;
|
||||
static void ConfigureApp(HostBuilderContext context, IConfigurationBuilder configBuilder) {
|
||||
configBuilder.AddCommandLine(Environment.GetCommandLineArgs());
|
||||
var cfgRoot = configBuilder.Build();
|
||||
var cfg = cfgRoot["config"];
|
||||
if (cfg is not null) {
|
||||
configBuilder.AddJsonFile(cfg, false);
|
||||
}
|
||||
}
|
||||
|
||||
int RunPubSubServer() {
|
||||
var config = PubSubConfig.Instance;
|
||||
long currentMemory = 0;
|
||||
int defaultWaitTime = 100;
|
||||
bool gced = false;
|
||||
Task.Run(() => {
|
||||
while (true) {
|
||||
gced = false;
|
||||
currentMemory = GC.GetTotalMemory(false);
|
||||
if (config.GcActivationThreshold > 0 && currentMemory > config.GcActivationThreshold) {
|
||||
Console.WriteLine($"GCing memory, mem before GC = {currentMemory}");
|
||||
GC.Collect();
|
||||
gced = true;
|
||||
}
|
||||
Thread.Sleep(gced ? config.WaitPeriodAfterGc.assert_value() : defaultWaitTime);
|
||||
}
|
||||
});
|
||||
new PubSubMessageReactor(config.ServerPort.assert_value()).start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RunEchoServer(short port, bool echo) {
|
||||
string log_msg = $"Running as TCP Echo server, port: {port} echo: {echo} Hit enter to continue:";
|
||||
Console.WriteLine(log_msg);
|
||||
Console.ReadLine();
|
||||
var ready_msg = $"ready to listen to port: {port}";
|
||||
Log.Gen.info(ready_msg);
|
||||
Console.WriteLine(ready_msg);
|
||||
var sts = new SimpleTCPServer(port, echo);
|
||||
sts.init();
|
||||
Console.CancelKeyPress += OnCancelKeyPress;
|
||||
while (true) {
|
||||
Thread.Sleep(TimeSpan.FromSeconds(3));
|
||||
}
|
||||
|
||||
void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
|
||||
Log.Gen.error("Cancel key press. Disposing echo server");
|
||||
sts.Dispose();
|
||||
static void ConfigureServices(HostBuilderContext context, IServiceCollection services) {
|
||||
var echoServerCfg = context.Configuration.GetSection("SimpleEchoServer");
|
||||
if (echoServerCfg.Get<EchoServerConfig>() is not null) {
|
||||
services.AddPubSubEchoServer(echoServerCfg);
|
||||
return;
|
||||
}
|
||||
/// Bind configuration json fields to strong types. This allows us to add services with
|
||||
/// constructors that accept these types as arguments.
|
||||
services.AddSpark(context.Configuration.GetSection("Spark"))
|
||||
.AddMasterWorker(context.Configuration.GetSection("MasterWorker"))
|
||||
.AddPubSubServer(context.Configuration.GetSection("PubSub"))
|
||||
.AddGarbageCollector(context.Configuration.GetSection("GarbageCollector"));
|
||||
}
|
||||
Reference in new issue
Block a user