From 50f8db5284c344512ecf010bbe58857b69c5535f Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 23 Apr 2025 23:39:17 +0200 Subject: Allow early return in SyncHelper, trim access token if path used, fix shutdown of command listener --- Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs | 2 +- .../Services/CommandListenerHostedService.cs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'Utilities') diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs index 4947394..56ceb65 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs @@ -32,7 +32,7 @@ public class BotInstaller(IServiceCollection services) { if (!string.IsNullOrWhiteSpace(config.AccessTokenPath)) { var token = File.ReadAllText(config.AccessTokenPath); - config.AccessToken = token; + config.AccessToken = token.Trim(); } var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result; diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index d07090f..f9b46d2 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -19,6 +19,7 @@ public class CommandListenerHostedService : IHostedService { private readonly Func? _commandResultHandler; private Task? _listenerTask; + private CancellationTokenSource _cts = new(); public CommandListenerHostedService(AuthenticatedHomeserverGeneric hs, ILogger logger, IServiceProvider services, LibMatrixBotConfiguration config, Func? commandResultHandler = null) { @@ -35,7 +36,7 @@ public class CommandListenerHostedService : IHostedService { /// Triggered when the application host is ready to start the service. /// Indicates that the start process has been aborted. public Task StartAsync(CancellationToken cancellationToken) { - _listenerTask = Run(cancellationToken); + _listenerTask = Run(_cts.Token); _logger.LogInformation("Command listener started (StartAsync)!"); return Task.CompletedTask; } @@ -54,7 +55,7 @@ public class CommandListenerHostedService : IHostedService { }); var syncHelper = new SyncHelper(_hs, _logger) { - Timeout = 300_000, + Timeout = 30_000, FilterId = filter }; @@ -96,7 +97,7 @@ public class CommandListenerHostedService : IHostedService { } }); - await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken); + await syncHelper.RunSyncLoopAsync(cancellationToken: _cts.Token); } /// Triggered when the application host is performing a graceful shutdown. @@ -108,7 +109,7 @@ public class CommandListenerHostedService : IHostedService { return; } - await _listenerTask.WaitAsync(cancellationToken); + await _cts.CancelAsync(); } private async Task GetUsedPrefix(StateEventResponse evt) { -- cgit 1.5.1