Obsidian icon indicating copy to clipboard operation
Obsidian copied to clipboard

Implement GenericHost

Open TheBrambleShark opened this issue 4 years ago • 3 comments

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 with systemd's service management on the Linux side as well.
    • [ ] Lifetime managed by IApplicationLifetime and CancellationTokens
  • [ ] Use Microsoft.Extensions.Logging base (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.

TheBrambleShark avatar Jun 07 '21 16:06 TheBrambleShark

Welcome Foxtrek! Thank you for your suggestion :) Gonna give my +1 to this one.

Naamloos avatar Jun 07 '21 16:06 Naamloos

by the way, you're always welcome to join our discord if you want e.g. faster response time :p

Naamloos avatar Jun 07 '21 17:06 Naamloos

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!

Seb-stian avatar Jun 07 '21 17:06 Seb-stian

Closing this issue as generic host has mostly been implemented.

Tides avatar Dec 28 '22 06:12 Tides