forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationLogCleanupCrontabHook.cs
More file actions
69 lines (59 loc) · 2.65 KB
/
Copy pathConversationLogCleanupCrontabHook.cs
File metadata and controls
69 lines (59 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using BotSharp.Abstraction.Conversations.Settings;
using BotSharp.Abstraction.Crontab;
using BotSharp.Abstraction.Crontab.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.OpenAPI.RuleTriggers;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace BotSharp.OpenAPI.Hooks
{
public class ConversationLogCleanupCrontabHook : ICrontabHook
{
private readonly ConversationSetting _settings;
private readonly IBotSharpRepository _db;
private readonly ILogger<ConversationLogCleanupCrontabHook> _logger;
private readonly IHostApplicationLifetime _appLifetime;
public string[]? Triggers => new[] { nameof(ConversationLogCleanupRuleTrigger) };
public ConversationLogCleanupCrontabHook(
ConversationSetting settings,
IBotSharpRepository db,
ILogger<ConversationLogCleanupCrontabHook> logger,
IHostApplicationLifetime appLifetime)
{
_settings = settings;
_db = db;
_logger = logger;
_appLifetime = appLifetime;
}
public async Task OnCronTriggered(CrontabItem item)
{
var cleanSetting = _settings.CleanSetting;
if (cleanSetting == null || !cleanSetting.Enable || cleanSetting.LogRetentionDays <= 0) return;
int totalDeleted = 0;
var cancellationToken = _appLifetime.ApplicationStopping;
while (!cancellationToken.IsCancellationRequested)
{
var deletedCount = await _db.DeleteOldConversationLogs(cleanSetting.LogRetentionDays, cleanSetting.LogBatchSize);
if (deletedCount == 0) break;
totalDeleted += deletedCount;
_logger.LogInformation($"Cleaned {deletedCount} conversation logs older than {cleanSetting.LogRetentionDays} days in this batch.");
try
{
// Sleep slightly to yield database resources, will throw TaskCanceledException on shutdown
await Task.Delay(1000, cancellationToken);
}
catch (OperationCanceledException)
{
_logger.LogWarning("Conversation log cleanup was interrupted due to application shutdown.");
break;
}
}
if (totalDeleted > 0)
{
_logger.LogInformation($"Successfully cleaned a total of {totalDeleted} conversation logs older than {cleanSetting.LogRetentionDays} days.");
}
}
}
}