diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs index 31a708bc..58b18cba 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs @@ -15,4 +15,6 @@ public interface IEventPublisher Task ReDispatchAsync(string channel, int count = 10, string order = "asc"); Task ReDispatchPendingAsync(string channel, string group, int count = 10); + + Task RemoveAsync(string channel, int count = 10); } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs index 32889f4f..89de2453 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs @@ -158,4 +158,26 @@ public class RedisPublisher : IEventPublisher Console.WriteLine($"Redis error: {ex.Message}"); } } + + public async Task RemoveAsync(string channel, int count = 10) + { + var db = _redis.GetDatabase(); + + var entries = await db.StreamRangeAsync(channel, "-", "+", count: count, messageOrder: Order.Ascending); + foreach (var entry in entries) + { + _logger.LogInformation($"Fetched message: {channel} {entry.Values[0].Value} ({entry.Id})"); + + try + { + await db.StreamDeleteAsync(channel, [entry.Id]); + + _logger.LogWarning($"Deleted message: {channel} {entry.Values[0].Value} ({entry.Id})"); + } + catch (Exception ex) + { + _logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}\r\n{ex}"); + } + } + } }