2022-02-10 14:03:44 +00:00
|
|
|
|
namespace ASC.Webhooks.Service.Services;
|
2022-02-03 13:41:09 +00:00
|
|
|
|
|
|
|
|
|
[Singletone]
|
2022-02-14 14:34:04 +00:00
|
|
|
|
public class BuildQueueService : BackgroundService
|
2022-02-02 14:23:15 +00:00
|
|
|
|
{
|
2022-02-17 21:43:30 +00:00
|
|
|
|
internal readonly ConcurrentQueue<WebhookRequest> Queue;
|
2022-02-03 13:41:09 +00:00
|
|
|
|
private readonly ICacheNotify<WebhookRequest> _webhookNotify;
|
2022-02-11 14:39:34 +00:00
|
|
|
|
|
2022-02-03 13:41:09 +00:00
|
|
|
|
public BuildQueueService(ICacheNotify<WebhookRequest> webhookNotify)
|
|
|
|
|
{
|
|
|
|
|
_webhookNotify = webhookNotify;
|
2022-02-17 21:43:30 +00:00
|
|
|
|
Queue = new ConcurrentQueue<WebhookRequest>();
|
2022-02-03 13:41:09 +00:00
|
|
|
|
}
|
2022-02-11 14:33:00 +00:00
|
|
|
|
public void BuildWebhooksQueue(WebhookRequest request)
|
|
|
|
|
{
|
2022-02-17 21:43:30 +00:00
|
|
|
|
Queue.Enqueue(request);
|
2022-02-11 14:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 14:34:04 +00:00
|
|
|
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
2022-02-03 13:41:09 +00:00
|
|
|
|
{
|
|
|
|
|
_webhookNotify.Subscribe(BuildWebhooksQueue, CacheNotifyAction.Update);
|
|
|
|
|
|
2022-02-14 14:34:04 +00:00
|
|
|
|
stoppingToken.Register(() =>
|
|
|
|
|
{
|
|
|
|
|
_webhookNotify.Unsubscribe(CacheNotifyAction.Update);
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-03 13:41:09 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2022-02-10 14:03:44 +00:00
|
|
|
|
}
|