Skip to content

Commit bac4d26

Browse files
committed
Rework the services to be more "fire and forget"
1 parent 22af774 commit bac4d26

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

AzzyBot-Next/Logging/LoggerActions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public static partial class LoggerActions
2323
[LoggerMessage(10, LogLevel.Debug, "AzuraCastFileServiceHost started")]
2424
public static partial void AzuraCastFileServiceHostStart(this ILogger<AzuraCastFileServiceHost> logger);
2525

26-
[LoggerMessage(11, LogLevel.Debug, "AzuraCastFileService started")]
27-
public static partial void AzuraCastFileServiceStart(this ILogger<AzuraCastFileService> logger);
26+
[LoggerMessage(11, LogLevel.Debug, "AzuraCastBackgroundService started")]
27+
public static partial void AzuraCastBackgroundServiceStart(this ILogger<AzuraCastBackgroundService> logger);
2828

2929
[LoggerMessage(12, LogLevel.Debug, "AzuraCastFileServiceHost running")]
3030
public static partial void AzuraCastFileServiceHostRun(this ILogger<AzuraCastFileServiceHost> logger);
3131

32-
[LoggerMessage(13, LogLevel.Debug, "Creating work item for AzuraCastFileService")]
32+
[LoggerMessage(13, LogLevel.Debug, "Creating work items...")]
3333
public static partial void AzuraCastFileServiceWorkItem(this ILogger<AzuraCastFileService> logger);
3434

3535
[LoggerMessage(90, LogLevel.Debug, "Stopping global timer")]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using AzzyBot.Logging;
4+
using AzzyBot.Utilities.Enums;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace AzzyBot.Services.Modules;
9+
10+
public sealed class AzuraCastBackgroundService(IHostApplicationLifetime applicationLifetime, ILogger<AzuraCastBackgroundService> logger, AzuraCastFileService azuraCastFileService)
11+
{
12+
private readonly ILogger<AzuraCastBackgroundService> _logger = logger;
13+
private readonly AzuraCastFileService _azuraCastFileService = azuraCastFileService;
14+
private readonly CancellationToken _cancellationToken = applicationLifetime.ApplicationStopping;
15+
16+
public async Task StartAzuraCastBackgroundServiceAsync(AzuraCastChecks checks)
17+
{
18+
_logger.AzuraCastBackgroundServiceStart();
19+
20+
if (_cancellationToken.IsCancellationRequested)
21+
return;
22+
23+
switch (checks)
24+
{
25+
case AzuraCastChecks.CheckForUpdates:
26+
break;
27+
28+
case AzuraCastChecks.CheckForFileChanges:
29+
await _azuraCastFileService.QueueFileChangesChecksAsync();
30+
break;
31+
}
32+
}
33+
}

AzzyBot-Next/Services/Modules/AzuraCastFileService.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,34 @@
1212
using AzzyBot.Services.Interfaces;
1313
using AzzyBot.Utilities;
1414
using AzzyBot.Utilities.Encryption;
15-
using AzzyBot.Utilities.Enums;
1615
using AzzyBot.Utilities.Records.AzuraCast;
1716
using DSharpPlus.Entities;
18-
using Microsoft.Extensions.Hosting;
1917
using Microsoft.Extensions.Logging;
2018

2119
namespace AzzyBot.Services.Modules;
2220

23-
public sealed class AzuraCastFileService(IHostApplicationLifetime applicationLifetime, ILogger<AzuraCastFileService> logger, IQueuedBackgroundTask taskQueue, AzuraCastApiService azuraCast, DbActions dbActions, DiscordBotService discordBotService)
21+
public sealed class AzuraCastFileService(ILogger<AzuraCastFileService> logger, IQueuedBackgroundTask taskQueue, AzuraCastApiService azuraCast, DbActions dbActions, DiscordBotService discordBotService)
2422
{
2523
private readonly ILogger<AzuraCastFileService> _logger = logger;
2624
private readonly IQueuedBackgroundTask _taskQueue = taskQueue;
2725
private readonly AzuraCastApiService _azuraCast = azuraCast;
2826
private readonly DbActions _dbActions = dbActions;
2927
private readonly DiscordBotService _botService = discordBotService;
30-
private readonly CancellationToken _cancellationToken = applicationLifetime.ApplicationStopping;
3128
private readonly JsonSerializerOptions _serializerOptions = new() { WriteIndented = true };
3229

33-
public void StartAzuraCastFileService(AzuraCastChecks check)
30+
public async ValueTask QueueFileChangesChecksAsync()
3431
{
35-
_logger.AzuraCastFileServiceStart();
36-
37-
if (_cancellationToken.IsCancellationRequested)
38-
return;
39-
40-
switch (check)
32+
List<GuildsEntity> guilds = await _dbActions.GetGuildsAsync();
33+
foreach (AzuraCastEntity azuraCast in guilds.Where(g => g.AzuraCast is not null).Select(g => g.AzuraCast!))
4134
{
42-
case AzuraCastChecks.CheckForUpdates:
43-
break;
44-
45-
case AzuraCastChecks.CheckForFileChanges:
46-
Task.Run(async () => await _taskQueue.QueueBackgroundWorkItemAsync(CheckForFileChangesAsync));
47-
break;
35+
foreach (AzuraCastStationEntity station in azuraCast.Stations.Where(s => s.Checks.FileChanges))
36+
{
37+
_ = Task.Run(async () => await _taskQueue.QueueBackgroundWorkItemAsync(async ct => await CheckForFileChangesAsync(station, ct)));
38+
}
4839
}
4940
}
5041

51-
private async ValueTask CheckForFileChangesAsync(CancellationToken cancellationToken)
42+
private async ValueTask CheckForFileChangesAsync(AzuraCastStationEntity station, CancellationToken cancellationToken)
5243
{
5344
_logger.AzuraCastFileServiceWorkItem();
5445

@@ -59,19 +50,12 @@ private async ValueTask CheckForFileChangesAsync(CancellationToken cancellationT
5950

6051
try
6152
{
62-
List<GuildsEntity> guilds = await _dbActions.GetGuildsAsync();
63-
foreach (AzuraCastEntity azuraCast in guilds.Where(g => g.AzuraCast is not null).Select(g => g.AzuraCast!))
64-
{
65-
foreach (AzuraCastStationEntity station in azuraCast.Stations.Where(s => s.Checks.FileChanges))
66-
{
67-
string apiKey = (string.IsNullOrWhiteSpace(station.ApiKey)) ? azuraCast.AdminApiKey : station.ApiKey;
53+
string apiKey = (string.IsNullOrWhiteSpace(station.ApiKey)) ? station.AzuraCast.AdminApiKey : station.ApiKey;
6854

69-
IReadOnlyList<FilesRecord> onlineFiles = await _azuraCast.GetFilesOnlineAsync(new(Crypto.Decrypt(azuraCast.BaseUrl)), Crypto.Decrypt(apiKey), station.StationId);
70-
IReadOnlyList<FilesRecord> localFiles = await _azuraCast.GetFilesLocalAsync(station.Id, station.StationId);
55+
IReadOnlyList<FilesRecord> onlineFiles = await _azuraCast.GetFilesOnlineAsync(new(Crypto.Decrypt(station.AzuraCast.BaseUrl)), Crypto.Decrypt(apiKey), station.StationId);
56+
IReadOnlyList<FilesRecord> localFiles = await _azuraCast.GetFilesLocalAsync(station.Id, station.StationId);
7157

72-
await CheckIfFilesWereModifiedAsync(onlineFiles, localFiles, station.Id, station.StationId, Crypto.Decrypt(station.Name), azuraCast.NotificationChannelId);
73-
}
74-
}
58+
await CheckIfFilesWereModifiedAsync(onlineFiles, localFiles, station.Id, station.StationId, Crypto.Decrypt(station.Name), station.AzuraCast.NotificationChannelId);
7559
}
7660
catch (OperationCanceledException)
7761
{

AzzyBot-Next/Services/TimerServiceHost.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace AzzyBot.Services;
1313

14-
public sealed class TimerServiceHost(ILogger<TimerServiceHost> logger, AzuraCastFileService azuraCastFileService, DiscordBotService discordBotService, UpdaterService updaterService) : IAsyncDisposable, IHostedService
14+
public sealed class TimerServiceHost(ILogger<TimerServiceHost> logger, AzuraCastBackgroundService azuraCastBackgroundService, DiscordBotService discordBotService, UpdaterService updaterService) : IAsyncDisposable, IHostedService
1515
{
1616
private readonly ILogger<TimerServiceHost> _logger = logger;
17-
private readonly AzuraCastFileService _azuraCastFileService = azuraCastFileService;
17+
private readonly AzuraCastBackgroundService _azuraCastBackgroundService = azuraCastBackgroundService;
1818
private readonly DiscordBotService _discordBotService = discordBotService;
1919
private readonly UpdaterService _updaterService = updaterService;
2020
private readonly bool _isDev = AzzyStatsSoftware.GetBotEnvironment == Environments.Development;
@@ -69,7 +69,7 @@ private async void TimerTimeoutAsync(object? o)
6969
_logger.GlobalTimerCheckForAzuraCastFiles();
7070
_lastAzuraCastFileCheck = now;
7171

72-
_azuraCastFileService.StartAzuraCastFileService(AzuraCastChecks.CheckForFileChanges);
72+
await _azuraCastBackgroundService.StartAzuraCastBackgroundServiceAsync(AzuraCastChecks.CheckForFileChanges);
7373
}
7474
}
7575
catch (Exception ex)

0 commit comments

Comments
 (0)