allow CancellationToken

This commit is contained in:
Haiping Chen 2024-11-29 17:12:44 -06:00
parent 4841c43755
commit 039054ad9e
2 changed files with 14 additions and 2 deletions

View file

@ -1,8 +1,12 @@
using System.Threading;
namespace BotSharp.Abstraction.Infrastructures.Events;
public interface IEventSubscriber
{
Task SubscribeAsync(string channel, Func<string, string, Task> received);
Task SubscribeAsync(string channel, string group, bool priorityEnabled, Func<string, string, Task> received);
Task SubscribeAsync(string channel, string group, bool priorityEnabled,
Func<string, string, Task> received,
CancellationToken? stoppingToken = null);
}

View file

@ -25,7 +25,9 @@ public class RedisSubscriber : IEventSubscriber
});
}
public async Task SubscribeAsync(string channel, string group, bool priorityEnabled, Func<string, string, Task> received)
public async Task SubscribeAsync(string channel, string group, bool priorityEnabled,
Func<string, string, Task> received,
CancellationToken? stoppingToken = null)
{
var db = _redis.GetDatabase();
@ -44,6 +46,12 @@ public class RedisSubscriber : IEventSubscriber
{
await Task.Delay(100);
if (stoppingToken.HasValue && stoppingToken.Value.IsCancellationRequested)
{
_logger.LogInformation($"Stopping consumer channel & group: [{channel}, {group}]");
break;
}
if (priorityEnabled)
{
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)