mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-04-28 15:39:46 +00:00
Added temporary chat maintenance service
This commit is contained in:
parent
ba5b109bcf
commit
eae1944695
@ -67,6 +67,7 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver
|
|||||||
|
|
||||||
// Set the js runtime for the update service:
|
// Set the js runtime for the update service:
|
||||||
UpdateService.SetBlazorDependencies(this.JsRuntime, this.Snackbar);
|
UpdateService.SetBlazorDependencies(this.JsRuntime, this.Snackbar);
|
||||||
|
TemporaryChatService.Initialize();
|
||||||
|
|
||||||
await base.OnInitializedAsync();
|
await base.OnInitializedAsync();
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ builder.Services.AddMudMarkdownClipboardService<MarkdownClipboardService>();
|
|||||||
builder.Services.AddSingleton<SettingsManager>();
|
builder.Services.AddSingleton<SettingsManager>();
|
||||||
builder.Services.AddSingleton<Random>();
|
builder.Services.AddSingleton<Random>();
|
||||||
builder.Services.AddHostedService<UpdateService>();
|
builder.Services.AddHostedService<UpdateService>();
|
||||||
|
builder.Services.AddHostedService<TemporaryChatService>();
|
||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents()
|
.AddInteractiveServerComponents()
|
||||||
.AddHubOptions(options =>
|
.AddHubOptions(options =>
|
||||||
|
71
app/MindWork AI Studio/Tools/TemporaryChatService.cs
Normal file
71
app/MindWork AI Studio/Tools/TemporaryChatService.cs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
using AIStudio.Settings;
|
||||||
|
|
||||||
|
namespace AIStudio.Tools;
|
||||||
|
|
||||||
|
public class TemporaryChatService(SettingsManager settingsManager) : BackgroundService
|
||||||
|
{
|
||||||
|
private static readonly TimeSpan CHECK_INTERVAL = TimeSpan.FromDays(1);
|
||||||
|
private static bool IS_INITIALIZED;
|
||||||
|
|
||||||
|
#region Overrides of BackgroundService
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested && !IS_INITIALIZED)
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(3), stoppingToken);
|
||||||
|
|
||||||
|
await settingsManager.LoadSettings();
|
||||||
|
if(settingsManager.ConfigurationData.WorkspaceStorageTemporaryMaintenancePolicy is WorkspaceStorageTemporaryMaintenancePolicy.NO_AUTOMATIC_MAINTENANCE)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Automatic maintenance of temporary chat storage is disabled. Exiting maintenance service.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.StartMaintenance();
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
await Task.Delay(CHECK_INTERVAL, stoppingToken);
|
||||||
|
await this.StartMaintenance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private Task StartMaintenance()
|
||||||
|
{
|
||||||
|
var temporaryDirectories = Path.Join(SettingsManager.DataDirectory, "tempChats");
|
||||||
|
if(!Directory.Exists(temporaryDirectories))
|
||||||
|
return Task.CompletedTask;
|
||||||
|
|
||||||
|
foreach (var tempChatDirPath in Directory.EnumerateDirectories(temporaryDirectories))
|
||||||
|
{
|
||||||
|
var chatPath = Path.Join(tempChatDirPath, "thread.json");
|
||||||
|
var chatMetadata = new FileInfo(chatPath);
|
||||||
|
if (!chatMetadata.Exists)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var lastWriteTime = chatMetadata.LastWriteTimeUtc;
|
||||||
|
var deleteChat = settingsManager.ConfigurationData.WorkspaceStorageTemporaryMaintenancePolicy switch
|
||||||
|
{
|
||||||
|
WorkspaceStorageTemporaryMaintenancePolicy.DELETE_OLDER_THAN_7_DAYS => DateTime.UtcNow - lastWriteTime > TimeSpan.FromDays(7),
|
||||||
|
WorkspaceStorageTemporaryMaintenancePolicy.DELETE_OLDER_THAN_30_DAYS => DateTime.UtcNow - lastWriteTime > TimeSpan.FromDays(30),
|
||||||
|
WorkspaceStorageTemporaryMaintenancePolicy.DELETE_OLDER_THAN_90_DAYS => DateTime.UtcNow - lastWriteTime > TimeSpan.FromDays(90),
|
||||||
|
WorkspaceStorageTemporaryMaintenancePolicy.DELETE_OLDER_THAN_180_DAYS => DateTime.UtcNow - lastWriteTime > TimeSpan.FromDays(180),
|
||||||
|
WorkspaceStorageTemporaryMaintenancePolicy.DELETE_OLDER_THAN_365_DAYS => DateTime.UtcNow - lastWriteTime > TimeSpan.FromDays(365),
|
||||||
|
|
||||||
|
WorkspaceStorageTemporaryMaintenancePolicy.NO_AUTOMATIC_MAINTENANCE => false,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(deleteChat)
|
||||||
|
Directory.Delete(tempChatDirPath, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Initialize()
|
||||||
|
{
|
||||||
|
IS_INITIALIZED = true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user