Skip to content

Commit 37e9024

Browse files
committed
feat: add crontab-based conversation cleanup jobs
1 parent 2e192dd commit 37e9024

4 files changed

Lines changed: 81 additions & 79 deletions

File tree

src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BotSharp.Abstraction.Messaging.JsonConverters;
22
using BotSharp.Core.MCP.Settings;
33
using BotSharp.Core.Users.Services;
4-
using BotSharp.OpenAPI.BackgroundServices;
54
using BotSharp.OpenAPI.Hooks;
65
using BotSharp.OpenAPI.RuleTriggers;
76
using Microsoft.AspNetCore.Authentication;
@@ -40,7 +39,6 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
4039
bool enableValidation)
4140
{
4241
services.AddScoped<IUserIdentity, UserIdentity>();
43-
services.AddHostedService<ConversationTimeoutService>();
4442

4543
services.AddCrontabServices();
4644

@@ -231,6 +229,8 @@ private static async Task OnTicketReceivedContext(TicketReceivedContext context)
231229

232230
public static IServiceCollection AddCrontabServices(this IServiceCollection services)
233231
{
232+
services.AddScoped<ICrontabHook, IdleConversationCleanupCrontabHook>();
233+
services.AddScoped<ICrontabSource, IdleConversationCleanupRuleTrigger>();
234234
services.AddScoped<ICrontabHook, ConversationLogCleanupCrontabHook>();
235235
services.AddScoped<ICrontabSource, ConversationLogCleanupRuleTrigger>();
236236
return services;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using BotSharp.Abstraction.Conversations;
2+
using BotSharp.Abstraction.Conversations.Settings;
3+
using BotSharp.Abstraction.Crontab;
4+
using BotSharp.Abstraction.Crontab.Models;
5+
using BotSharp.OpenAPI.RuleTriggers;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace BotSharp.OpenAPI.Hooks
9+
{
10+
public class IdleConversationCleanupCrontabHook : ICrontabHook
11+
{
12+
private readonly ConversationSetting _settings;
13+
private readonly IConversationService _conversationService;
14+
private readonly ILogger<IdleConversationCleanupCrontabHook> _logger;
15+
16+
public string[]? Triggers => new[] { nameof(IdleConversationCleanupRuleTrigger) };
17+
18+
public IdleConversationCleanupCrontabHook(
19+
ConversationSetting settings,
20+
IConversationService conversationService,
21+
ILogger<IdleConversationCleanupCrontabHook> logger)
22+
{
23+
_settings = settings;
24+
_conversationService = conversationService;
25+
_logger = logger;
26+
}
27+
28+
public async Task OnCronTriggered(CrontabItem item)
29+
{
30+
var cleanSetting = _settings.CleanSetting;
31+
32+
if (cleanSetting == null || !cleanSetting.Enable) return;
33+
34+
try
35+
{
36+
var batchSize = cleanSetting.BatchSize;
37+
var limit = cleanSetting.MessageLimit;
38+
var bufferHours = cleanSetting.BufferHours;
39+
var excludeAgentIds = cleanSetting.ExcludeAgentIds ?? new List<string>();
40+
var conversationIds = await _conversationService.GetIdleConversations(batchSize, limit, bufferHours, excludeAgentIds);
41+
42+
if (!conversationIds.IsNullOrEmpty())
43+
{
44+
await _conversationService.DeleteConversations(conversationIds);
45+
}
46+
}
47+
catch (Exception ex)
48+
{
49+
_logger.LogError(ex, "Error occurred closing conversations.");
50+
}
51+
}
52+
}
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using BotSharp.Abstraction.Conversations.Enums;
2+
using BotSharp.Abstraction.Crontab;
3+
using BotSharp.Abstraction.Crontab.Models;
4+
using BotSharp.Abstraction.Rules;
5+
6+
namespace BotSharp.OpenAPI.RuleTriggers
7+
{
8+
public class IdleConversationCleanupRuleTrigger : IRuleTrigger, ICrontabSource
9+
{
10+
public string Channel => ConversationChannel.Crontab;
11+
public string Name => nameof(IdleConversationCleanupRuleTrigger);
12+
public string EntityType { get; set; } = string.Empty;
13+
public string EntityId { get; set; } = string.Empty;
14+
15+
public CrontabItem GetCrontabItem()
16+
{
17+
return new CrontabItem
18+
{
19+
Title = nameof(IdleConversationCleanupRuleTrigger),
20+
Description = "Clean up idle conversations hourly",
21+
Cron = "0 * * * *",
22+
TriggerType = CronTabItemTriggerType.MessageQueue
23+
};
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)