2021-07-26 17:03:55 +00:00
|
|
|
|
using System;
|
2021-07-31 17:12:41 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-07-26 17:03:55 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
2021-07-31 17:12:41 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2021-07-26 17:03:55 +00:00
|
|
|
|
using ASC.Common.Logging;
|
2021-07-29 11:04:42 +00:00
|
|
|
|
using ASC.Web.Webhooks;
|
2021-07-26 17:03:55 +00:00
|
|
|
|
|
|
|
|
|
namespace ASC.Webhooks
|
|
|
|
|
{
|
|
|
|
|
internal class WorkerService
|
|
|
|
|
{
|
|
|
|
|
private readonly int threadCount = 10;
|
|
|
|
|
private readonly CancellationToken cancellationToken;
|
|
|
|
|
private readonly WebhookSender webhookSender;
|
2021-07-29 11:04:42 +00:00
|
|
|
|
private readonly ConcurrentQueue<WebhookRequest> queue;
|
2021-07-26 17:03:55 +00:00
|
|
|
|
private ILog logger;
|
|
|
|
|
private Timer timer;
|
|
|
|
|
|
|
|
|
|
public WorkerService(CancellationToken cancellationToken,
|
|
|
|
|
WebhookSender webhookSender,
|
2021-07-29 11:04:42 +00:00
|
|
|
|
ILog logger,
|
|
|
|
|
ConcurrentQueue<WebhookRequest> queue)
|
2021-07-26 17:03:55 +00:00
|
|
|
|
{
|
|
|
|
|
this.cancellationToken = cancellationToken;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.webhookSender = webhookSender;
|
2021-07-29 11:04:42 +00:00
|
|
|
|
this.queue = queue;
|
2021-07-26 17:03:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
timer = new Timer(Procedure, null, 0, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
if (timer != null)
|
|
|
|
|
{
|
|
|
|
|
timer.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
timer.Dispose();
|
|
|
|
|
timer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Procedure(object _)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timer.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
logger.Debug("Procedure: Start.");
|
2021-07-29 11:04:42 +00:00
|
|
|
|
var queueSize = queue.Count;
|
2021-07-26 17:03:55 +00:00
|
|
|
|
|
|
|
|
|
if (queueSize == 0)// change to "<= threadCount"
|
|
|
|
|
{
|
|
|
|
|
logger.TraceFormat("Procedure: Waiting for data. Sleep {0}.", 5);
|
|
|
|
|
timer.Change(TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(-1));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//var queueEntrys = new List<WebhookRequest>();
|
|
|
|
|
//for (int i = 0; i < queueSize; i++)
|
|
|
|
|
//{
|
|
|
|
|
// WebhookHostedService.Queue.TryDequeue(out var entry);
|
|
|
|
|
// queueEntrys.Add(entry);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
var tasks = new List<Task>();
|
|
|
|
|
var counter = 0;
|
|
|
|
|
for (int i = 0; i < queueSize; i++)
|
|
|
|
|
{
|
2021-07-29 11:04:42 +00:00
|
|
|
|
queue.TryDequeue(out var entry);
|
2021-07-26 17:03:55 +00:00
|
|
|
|
tasks.Add(webhookSender.Send(entry));
|
|
|
|
|
counter++;
|
|
|
|
|
|
|
|
|
|
if (counter >= threadCount)
|
|
|
|
|
{
|
|
|
|
|
Task.WaitAll(tasks.ToArray());
|
|
|
|
|
tasks.Clear();
|
|
|
|
|
counter = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Debug("Procedure: Finish.");
|
|
|
|
|
timer.Change(0, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|