Skip to content

Commit

Permalink
Switch Worker from BackgroundService to IHostedService
Browse files Browse the repository at this point in the history
  • Loading branch information
austins committed Feb 19, 2024
1 parent 0600b83 commit 4963896
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions DiscordTranslationBot/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DiscordTranslationBot;
/// <summary>
/// The main worker service.
/// </summary>
internal sealed partial class Worker : BackgroundService
internal sealed partial class Worker : IHostedService
{
private readonly DiscordSocketClient _client;
private readonly ICountryService _countryService;
Expand Down Expand Up @@ -52,7 +52,7 @@ public Worker(
/// Runs when app is started.
/// </summary>
/// <param name="cancellationToken">Cancellation token.</param>
public override async Task StartAsync(CancellationToken cancellationToken)
public async Task StartAsync(CancellationToken cancellationToken)
{
// Check if no translation providers are enabled.
if (!_translationProviders.Any())
Expand Down Expand Up @@ -81,32 +81,16 @@ public override async Task StartAsync(CancellationToken cancellationToken)

// Initialize the Discord event listener.
await _eventListener.InitializeEventsAsync();

await base.StartAsync(cancellationToken);
}

/// <summary>
/// Stops the app gracefully.
/// </summary>
/// <param name="cancellationToken">Cancellation token.</param>
public override async Task StopAsync(CancellationToken cancellationToken)
public async Task StopAsync(CancellationToken cancellationToken)
{
await _client.LogoutAsync();
await _client.StopAsync();

await base.StopAsync(cancellationToken);
}

/// <summary>
/// The main execution thread that monitors for a cancellation request to stop the app.
/// </summary>
/// <param name="stoppingToken">Cancellation token.</param>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}

private sealed partial class Log
Expand Down

0 comments on commit 4963896

Please sign in to comment.