12
12
using AzzyBot . Services . Interfaces ;
13
13
using AzzyBot . Utilities ;
14
14
using AzzyBot . Utilities . Encryption ;
15
- using AzzyBot . Utilities . Enums ;
16
15
using AzzyBot . Utilities . Records . AzuraCast ;
17
16
using DSharpPlus . Entities ;
18
- using Microsoft . Extensions . Hosting ;
19
17
using Microsoft . Extensions . Logging ;
20
18
21
19
namespace AzzyBot . Services . Modules ;
22
20
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 )
24
22
{
25
23
private readonly ILogger < AzuraCastFileService > _logger = logger ;
26
24
private readonly IQueuedBackgroundTask _taskQueue = taskQueue ;
27
25
private readonly AzuraCastApiService _azuraCast = azuraCast ;
28
26
private readonly DbActions _dbActions = dbActions ;
29
27
private readonly DiscordBotService _botService = discordBotService ;
30
- private readonly CancellationToken _cancellationToken = applicationLifetime . ApplicationStopping ;
31
28
private readonly JsonSerializerOptions _serializerOptions = new ( ) { WriteIndented = true } ;
32
29
33
- public void StartAzuraCastFileService ( AzuraCastChecks check )
30
+ public async ValueTask QueueFileChangesChecksAsync ( )
34
31
{
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 ! ) )
41
34
{
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
+ }
48
39
}
49
40
}
50
41
51
- private async ValueTask CheckForFileChangesAsync ( CancellationToken cancellationToken )
42
+ private async ValueTask CheckForFileChangesAsync ( AzuraCastStationEntity station , CancellationToken cancellationToken )
52
43
{
53
44
_logger . AzuraCastFileServiceWorkItem ( ) ;
54
45
@@ -59,19 +50,12 @@ private async ValueTask CheckForFileChangesAsync(CancellationToken cancellationT
59
50
60
51
try
61
52
{
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 ;
68
54
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 ) ;
71
57
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 ) ;
75
59
}
76
60
catch ( OperationCanceledException )
77
61
{
0 commit comments