2022-02-02 14:23:15 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using ASC.Common;
|
|
|
|
|
using ASC.Common.Caching;
|
|
|
|
|
using ASC.Web.Webhooks;
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
2022-02-03 13:41:09 +00:00
|
|
|
|
namespace ASC.Webhooks.Service.Services;
|
|
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
|
public class BuildQueueService : IHostedService
|
2022-02-02 14:23:15 +00:00
|
|
|
|
{
|
2022-02-03 13:41:09 +00:00
|
|
|
|
internal ConcurrentQueue<WebhookRequest> Queue { get; }
|
|
|
|
|
private readonly ICacheNotify<WebhookRequest> _webhookNotify;
|
|
|
|
|
|
|
|
|
|
public BuildQueueService(ICacheNotify<WebhookRequest> webhookNotify)
|
|
|
|
|
{
|
|
|
|
|
_webhookNotify = webhookNotify;
|
|
|
|
|
Queue = new ConcurrentQueue<WebhookRequest>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_webhookNotify.Subscribe(BuildWebhooksQueue, CacheNotifyAction.Update);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_webhookNotify.Unsubscribe(CacheNotifyAction.Update);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BuildWebhooksQueue(WebhookRequest request)
|
2022-02-02 14:23:15 +00:00
|
|
|
|
{
|
2022-02-03 13:41:09 +00:00
|
|
|
|
Queue.Enqueue(request);
|
2022-02-02 14:23:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|