Implement GenericHost
In an effort to streamline and standardize DI, logging, and the lifetime of the bot, I would like to propose changing to a GenericHost.
Items to discuss:
- [ ] Replace Dependency Injection with
Microsoft.Extensions.DependencyInjection - [ ] Design server runtime around a GenericHost
- [ ] Use Console Lifetime (naturally handles CTRL+C or SIGTERM and calls
IHostApplicationLifetime.StopApplication()to gracefully stop the bot as well as unblocking asynchronous runtimes). Works very well withsystemd's service management on the Linux side as well. - [ ] Lifetime managed by
IApplicationLifetimeandCancellationTokens
- [ ] Use Console Lifetime (naturally handles CTRL+C or SIGTERM and calls
- [ ] Use
Microsoft.Extensions.Loggingbase (compatible with existing logging library). - [ ] App Configuration (environment variables, appsettings.json, UserSecrets)
For those unfamiliar, the GenericHost is based off the WebHost class from ASP.NET Core. However, this has the web-specific features removed in favor of a more generic long-lived service lifetime. The HostBuilder provides a set of utilities for configuring the host, such as reading configuration items, setting up database connections, and registering services. The Obsidian server itself is registered as a BackgroundService which is responsible for handling startup (database migrations and so forth).
public override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Set up server
Console.WriteLine("Obsidian started successfully!");
stoppingToken.WaitHandle.WaitOne(); // Wait for a stop to be requested by the `IApplicationLifetime`.
Console.WriteLine("Obsidian shutting down...");
// Tear down
Console.WriteLine("Obsidian stopped.");
}
If we decide to move forward with this, I would be more than happy to be responsible for developing this and submitting a PR (or several).
Edit: I should point out that this will likely be a disruptive change since it involves redesigning the core of the bot as well as its lifetime management. However, I do believe that this will result in aiding future development and promote contributions by providing a well-known DI system.
Welcome Foxtrek! Thank you for your suggestion :) Gonna give my +1 to this one.
by the way, you're always welcome to join our discord if you want e.g. faster response time :p
Sounds good. I would definitely like it implemented for the core. Not sure about plugins, but we'll see. If you need help with understanding my questionable code, just @ me or come chat with us.
On a side note, I'm pretty sure that we are referencing Microsoft.Extensions.* libraries, but they might not be used optimally 🤔
The best of luck!
Closing this issue as generic host has mostly been implemented.