Skip to content

Commit

Permalink
Add AzuraCast Update Check to UpdaterService.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Jun 10, 2024
1 parent 4dcbf50 commit 4489a45
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions AzzyBot-Next/Logging/LoggerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public static partial class LoggerActions
[LoggerMessage(3, LogLevel.Debug, "Global timer checking for AzuraCast files changes")]
public static partial void GlobalTimerCheckForAzuraCastFiles(this ILogger<TimerServiceHost> logger);

[LoggerMessage(4, LogLevel.Debug, "Global timer checking for AzuraCast updates")]
public static partial void GlobalTimerCheckForAzuraCastUpdates(this ILogger<TimerServiceHost> logger);

[LoggerMessage(10, LogLevel.Debug, "BackgroundServiceHost started")]
public static partial void BackgroundServiceHostStart(this ILogger<AzzyBackgroundServiceHost> logger);

Expand Down
4 changes: 3 additions & 1 deletion AzzyBot-Next/Services/AzzyBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

namespace AzzyBot.Services.Modules;

public sealed class AzzyBackgroundService(IHostApplicationLifetime applicationLifetime, ILogger<AzzyBackgroundService> logger, AzuraCastFileService azuraCastFileService)
public sealed class AzzyBackgroundService(IHostApplicationLifetime applicationLifetime, ILogger<AzzyBackgroundService> logger, AzuraCastFileService azuraCastFileService, UpdaterService updaterService)
{
private readonly ILogger<AzzyBackgroundService> _logger = logger;
private readonly AzuraCastFileService _azuraCastFileService = azuraCastFileService;
private readonly UpdaterService _updaterService = updaterService;
private readonly CancellationToken _cancellationToken = applicationLifetime.ApplicationStopping;

public async Task StartAzuraCastBackgroundServiceAsync(AzuraCastChecks checks)
Expand All @@ -30,6 +31,7 @@ public async Task StartAzuraCastBackgroundServiceAsync(AzuraCastChecks checks)
break;

case AzuraCastChecks.CheckForUpdates:
await _updaterService.QueueAzuraCastUpdatesAsync();
break;
}
}
Expand Down
19 changes: 14 additions & 5 deletions AzzyBot-Next/Services/TimerServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@

namespace AzzyBot.Services;

public sealed class TimerServiceHost(ILogger<TimerServiceHost> logger, Modules.AzzyBackgroundService azuraCastBackgroundService, DiscordBotService discordBotService, UpdaterService updaterService) : IAsyncDisposable, IHostedService
public sealed class TimerServiceHost(ILogger<TimerServiceHost> logger, AzzyBackgroundService azuraCastBackgroundService, DiscordBotService discordBotService, UpdaterService updaterService) : IAsyncDisposable, IHostedService
{
private readonly ILogger<TimerServiceHost> _logger = logger;
private readonly Modules.AzzyBackgroundService _azuraCastBackgroundService = azuraCastBackgroundService;
private readonly AzzyBackgroundService _azuraCastBackgroundService = azuraCastBackgroundService;
private readonly DiscordBotService _discordBotService = discordBotService;
private readonly UpdaterService _updaterService = updaterService;
private readonly bool _isDev = AzzyStatsSoftware.GetBotEnvironment == Environments.Development;
private readonly Task _completedTask = Task.CompletedTask;
private Timer? _timer;
private DateTime _lastAzuraCastFileCheck = DateTime.MinValue;
private DateTime _lastBotUpdateCheck = DateTime.MinValue;
private DateTime _lastAzuraCastUpdateCheck = DateTime.MinValue;
private DateTime _lastAzzyBotUpdateCheck = DateTime.MinValue;

public Task StartAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -56,10 +57,10 @@ private async void TimerTimeoutAsync(object? o)
{
DateTime now = DateTime.Now;

if (!_isDev && now - _lastBotUpdateCheck >= TimeSpan.FromHours(5.98))
if (!_isDev && now - _lastAzzyBotUpdateCheck >= TimeSpan.FromHours(5.98))
{
_logger.GlobalTimerCheckForUpdates();
_lastBotUpdateCheck = now;
_lastAzzyBotUpdateCheck = now;

await _updaterService.CheckForAzzyUpdatesAsync();
}
Expand All @@ -71,6 +72,14 @@ private async void TimerTimeoutAsync(object? o)

await _azuraCastBackgroundService.StartAzuraCastBackgroundServiceAsync(AzuraCastChecks.CheckForFileChanges);
}

if (now - _lastAzuraCastUpdateCheck >= TimeSpan.FromHours(12))
{
_logger.GlobalTimerCheckForAzuraCastUpdates();
_lastAzuraCastUpdateCheck = now;

await _azuraCastBackgroundService.StartAzuraCastBackgroundServiceAsync(AzuraCastChecks.CheckForUpdates);
}
}
catch (Exception ex)
{
Expand Down
14 changes: 13 additions & 1 deletion AzzyBot-Next/Services/UpdaterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
using AzzyBot.Database.Entities;
using AzzyBot.Logging;
using AzzyBot.Services.Interfaces;
using AzzyBot.Services.Modules;
using AzzyBot.Settings;
using AzzyBot.Utilities;
using AzzyBot.Utilities.Encryption;
using AzzyBot.Utilities.Records;
using AzzyBot.Utilities.Records.AzuraCast;
using DSharpPlus.Entities;
using Microsoft.Extensions.Logging;

namespace AzzyBot.Services;

public sealed class UpdaterService(ILogger<UpdaterService> logger, IQueuedBackgroundTask taskQueue, AzzyBotSettingsRecord settings, DbActions dbActions, DiscordBotService botService, WebRequestService webService)
public sealed class UpdaterService(ILogger<UpdaterService> logger, IQueuedBackgroundTask taskQueue, AzuraCastApiService azuraCastApiService, AzzyBotSettingsRecord settings, DbActions dbActions, DiscordBotService botService, WebRequestService webService)
{
private readonly ILogger<UpdaterService> _logger = logger;
private readonly IQueuedBackgroundTask _taskQueue = taskQueue;
private readonly AzuraCastApiService _azuraCastApiService = azuraCastApiService;
private readonly AzzyBotSettingsRecord _settings = settings;
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = botService;
Expand Down Expand Up @@ -50,7 +53,16 @@ private async ValueTask CheckForAzuraCastUpdatesAsync(AzuraCastEntity azuraCast,
{
string apiKey = Crypto.Decrypt(azuraCast.AdminApiKey);

AzuraUpdateRecord update = await _azuraCastApiService.GetUpdatesAsync(new(Crypto.Decrypt(azuraCast.BaseUrl)), apiKey);

if (!update.NeedsReleaseUpdate && !update.NeedsRollingUpdate)
return;

List<DiscordEmbed> embeds = [EmbedBuilder.BuildAzuraCastUpdatesAvailableEmbed(update)];
if (_settings.Updater.DisplayChangelog)
embeds.Add(EmbedBuilder.BuildAzuraCastUpdatesChangelogEmbed(update.RollingUpdatesList, update.NeedsRollingUpdate));

await _botService.SendMessageAsync(azuraCast.NotificationChannelId, null, embeds);
}
catch (OperationCanceledException)
{
Expand Down
2 changes: 1 addition & 1 deletion AzzyBot-Next/Utilities/EmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static DiscordEmbed BuildAzuraCastUpdatesAvailableEmbed(AzuraUpdateRecord
return CreateBasicEmbed(title, description, DiscordColor.White, AzuraCastPic, null, null, fields);
}

public static DiscordEmbed BuildAzuraCastUpdatesAvailableEmbed(IReadOnlyList<string> changelog, bool isRolling)
public static DiscordEmbed BuildAzuraCastUpdatesChangelogEmbed(IReadOnlyList<string> changelog, bool isRolling)
{
ArgumentNullException.ThrowIfNull(changelog, nameof(changelog));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(changelog.Count, nameof(changelog));
Expand Down

0 comments on commit 4489a45

Please sign in to comment.