AI-Studio/app/MindWork AI Studio/Tools/Services/UpdateService.cs

119 lines
3.6 KiB
C#
Raw Normal View History

2024-06-30 13:26:28 +00:00
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
2024-06-30 13:26:28 +00:00
using Microsoft.AspNetCore.Components;
2024-08-21 06:30:01 +00:00
namespace AIStudio.Tools.Services;
2024-06-30 13:26:28 +00:00
public sealed class UpdateService : BackgroundService, IMessageBusReceiver
{
private static bool IS_INITIALIZED;
private static ISnackbar? SNACKBAR;
2024-06-30 13:26:28 +00:00
private readonly SettingsManager settingsManager;
private readonly MessageBus messageBus;
2024-09-01 18:10:03 +00:00
private readonly RustService rust;
2024-06-30 13:26:28 +00:00
private TimeSpan updateInterval;
2024-09-01 18:10:03 +00:00
public UpdateService(MessageBus messageBus, SettingsManager settingsManager, RustService rust)
2024-06-30 13:26:28 +00:00
{
this.settingsManager = settingsManager;
this.messageBus = messageBus;
this.rust = rust;
2024-06-30 13:26:28 +00:00
this.messageBus.RegisterComponent(this);
this.ApplyFilters([], [ Event.USER_SEARCH_FOR_UPDATE ]);
}
#region Overrides of BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested && !IS_INITIALIZED)
await Task.Delay(TimeSpan.FromSeconds(3), stoppingToken);
2024-08-05 19:12:52 +00:00
this.updateInterval = this.settingsManager.ConfigurationData.App.UpdateBehavior switch
2024-06-30 13:26:28 +00:00
{
UpdateBehavior.NO_CHECK => Timeout.InfiniteTimeSpan,
UpdateBehavior.ONCE_STARTUP => Timeout.InfiniteTimeSpan,
UpdateBehavior.HOURLY => TimeSpan.FromHours(1),
UpdateBehavior.DAILY => TimeSpan.FromDays(1),
UpdateBehavior.WEEKLY => TimeSpan.FromDays(7),
_ => TimeSpan.FromHours(1)
};
2024-08-05 19:12:52 +00:00
if(this.settingsManager.ConfigurationData.App.UpdateBehavior is UpdateBehavior.NO_CHECK)
return;
await this.CheckForUpdate();
2024-06-30 13:26:28 +00:00
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(this.updateInterval, stoppingToken);
await this.CheckForUpdate();
}
}
#endregion
#region Implementation of IMessageBusReceiver
public async Task ProcessMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data)
{
switch (triggeredEvent)
{
case Event.USER_SEARCH_FOR_UPDATE:
await this.CheckForUpdate(notifyUserWhenNoUpdate: true);
2024-06-30 13:26:28 +00:00
break;
}
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
2024-06-30 13:26:28 +00:00
#endregion
#region Overrides of BackgroundService
public override async Task StopAsync(CancellationToken cancellationToken)
{
this.messageBus.Unregister(this);
await base.StopAsync(cancellationToken);
}
#endregion
private async Task CheckForUpdate(bool notifyUserWhenNoUpdate = false)
2024-06-30 13:26:28 +00:00
{
if(!IS_INITIALIZED)
2024-06-30 13:26:28 +00:00
return;
2024-09-01 18:10:03 +00:00
var response = await this.rust.CheckForUpdate();
2024-06-30 13:26:28 +00:00
if (response.UpdateIsAvailable)
{
await this.messageBus.SendMessage(null, Event.UPDATE_AVAILABLE, response);
}
else
{
if (notifyUserWhenNoUpdate)
{
SNACKBAR!.Add("No update found.", Severity.Normal, config =>
{
config.Icon = Icons.Material.Filled.Update;
config.IconSize = Size.Large;
config.IconColor = Color.Primary;
});
}
}
2024-06-30 13:26:28 +00:00
}
2024-09-01 18:10:03 +00:00
public static void SetBlazorDependencies(ISnackbar snackbar)
2024-06-30 13:26:28 +00:00
{
SNACKBAR = snackbar;
IS_INITIALIZED = true;
2024-06-30 13:26:28 +00:00
}
}