DocSpace-buildtools/common/services/ASC.Webhooks.Service/WorkerService.cs
pavelbannov 320a1f2250 Merge branch 'develop' into feature/backend-refactor
# Conflicts:
#	common/ASC.Api.Core/Core/BaseStartup.cs
#	common/ASC.Common/Caching/AscCache.cs
#	common/ASC.Common/Data/StreamExtension.cs
#	common/ASC.Common/Utils/RandomString.cs
#	common/ASC.Core.Common/Billing/CouponManager.cs
#	common/ASC.Core.Common/Billing/License/LicenseReader.cs
#	common/ASC.Core.Common/Core/UserGroupRef.cs
#	common/ASC.Core.Common/Data/DbTenantService.cs
#	common/ASC.Core.Common/Notify/Jabber/JabberServiceClientWcf.cs
#	common/ASC.Core.Common/Notify/Telegram/Dao/CachedTelegramDao.cs
#	common/ASC.Data.Backup.Core/Core/DbHelper.cs
#	common/ASC.Data.Backup.Core/Storage/BackupRepository.cs
#	common/ASC.Data.Backup.Core/Tasks/Data/TableInfo.cs
#	common/ASC.Data.Storage/BaseStorage.cs
#	common/ASC.Data.Storage/DiscStorage/DiscDataStore.cs
#	common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs
#	common/ASC.Data.Storage/RackspaceCloud/RackspaceCloudStorage.cs
#	common/ASC.Data.Storage/S3/S3Storage.cs
#	common/ASC.Notify.Textile/JabberStyler.cs
#	common/ASC.Textile/Blocks/GlyphBlockModifier.cs
#	common/ASC.Textile/States/TableRowFormatterState.cs
#	common/services/ASC.ApiSystem/Classes/CommonMethods.cs
#	common/services/ASC.ApiSystem/Controllers/PortalController.cs
#	common/services/ASC.ClearEvents/Program.cs
#	common/services/ASC.TelegramService/Startup.cs
#	common/services/ASC.UrlShortener.Svc/Program.cs
#	products/ASC.Files/Core/Core/Entries/File.cs
#	products/ASC.Files/Core/Core/Entries/FileEntry.cs
#	products/ASC.Files/Core/Core/Entries/FileHelper.cs
#	products/ASC.Files/Core/Core/Entries/Folder.cs
#	products/ASC.Files/Core/Core/FileStorageService.cs
#	products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderDaoBase.cs
#	products/ASC.Files/Core/Helpers/ThirdpartyConfiguration.cs
#	products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs
#	products/ASC.Files/Core/Services/DocumentService/Configuration.cs
#	products/ASC.Files/Core/Services/DocumentService/DocumentServiceConnector.cs
#	products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs
#	products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs
#	products/ASC.Files/Core/Services/WCFService/FileOperations/FileMoveCopyOperation.cs
#	products/ASC.Files/Core/Utils/EntryManager.cs
#	products/ASC.Files/Server/Helpers/FilesControllerHelper.cs
#	products/ASC.Files/Server/Startup.cs
#	products/ASC.Files/Service/Thumbnail/Builder.cs
#	products/ASC.Files/Service/Thumbnail/FileDataProvider.cs
#	products/ASC.People/Server/Startup.cs
#	web/ASC.Web.Core/Files/DocumentService.cs
#	web/ASC.Web.Core/Files/DocumentServiceLicense.cs
#	web/ASC.Web.Core/QuotaSync.cs
#	web/ASC.Web.Core/Sms/SmsKeyStorage.cs
#	web/ASC.Web.Core/Users/UserManagerWrapper.cs
#	web/ASC.Web.HealthChecks.UI/Program.cs
#	web/ASC.Web.Studio/Startup.cs
2022-02-10 13:16:33 +03:00

90 lines
2.6 KiB
C#

namespace ASC.Webhooks.Service
{
[Singletone]
public class WorkerService
{
private readonly int? threadCount;
private readonly WebhookSender webhookSender;
private readonly ConcurrentQueue<WebhookRequest> queue;
private CancellationToken cancellationToken;
private ILog logger;
private Timer timer;
public WorkerService(WebhookSender webhookSender,
ILog logger,
BuildQueueService buildQueueService,
Settings settings)
{
this.logger = logger;
this.webhookSender = webhookSender;
queue = buildQueueService.Queue;
threadCount = settings.ThreadCount;
}
public void Start(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
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.");
var queueSize = queue.Count;
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 tasks = new List<Task>();
var counter = 0;
for (int i = 0; i < queueSize; i++)
{
if (cancellationToken.IsCancellationRequested)
{
Stop();
return;
}
if (!queue.TryDequeue(out var entry))
break;
tasks.Add(webhookSender.Send(entry, cancellationToken));
counter++;
if (counter >= threadCount)
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
counter = 0;
}
}
Task.WaitAll(tasks.ToArray());
logger.Debug("Procedure: Finish.");
timer.Change(0, Timeout.Infinite);
}
}
}