DocSpace-buildtools/common/ASC.Common/Threading/DistributedTaskQueue.cs
pavelbannov 26947072c6 Merge branch 'feature/backend-refactor' into feature/warnings
# Conflicts:
#	common/ASC.Common/Threading/DistributedTaskProgress.cs
#	common/ASC.Common/Threading/DistributedTaskQueue.cs
#	common/ASC.Core.Common/Configuration/AmiPublicDnsSyncService.cs
#	common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs
#	common/ASC.Core.Common/Notify/Model/NotifyClientImpl.cs
#	common/ASC.Core.Common/Security/EmailValidationKeyProvider.cs
#	common/ASC.Data.Reassigns/QueueWorker.cs
#	common/ASC.Data.Reassigns/ReassignProgressItem.cs
#	common/ASC.Data.Reassigns/RemoveProgressItem.cs
#	common/ASC.Data.Storage/StaticUploader.cs
#	products/ASC.Files/Core/ApiModels/ResponseDto/FileDto.cs
#	products/ASC.Files/Core/ApiModels/ResponseDto/FolderDto.cs
#	products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs
#	products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs
#	products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs
#	products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs
#	products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs
#	products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs
#	products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs
#	products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs
#	products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs
#	products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs
#	products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs
#	products/ASC.Files/Core/HttpHandlers/ThirdPartyAppHandler.ashx.cs
#	products/ASC.Files/Core/HttpHandlers/docusignhandler.ashx.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/FileOperation.cs
#	products/ASC.Files/Core/Utils/EntryManager.cs
#	web/ASC.Web.Core/CollaboratorSettings.cs
#	web/ASC.Web.Core/CustomNavigationSettings.cs
#	web/ASC.Web.Core/EmailActivationSettings.cs
#	web/ASC.Web.Core/Notify/NotifyConfiguration.cs
#	web/ASC.Web.Core/Notify/SpamEmailSettings.cs
#	web/ASC.Web.Core/Notify/StudioNotifyHelper.cs
#	web/ASC.Web.Core/Notify/StudioNotifyService.cs
#	web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs
#	web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs
#	web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs
#	web/ASC.Web.Core/PersonalSettings.cs
#	web/ASC.Web.Core/PrivacyRoomSettings.cs
#	web/ASC.Web.Core/PromotionsSettings.cs
#	web/ASC.Web.Core/QuotaSync.cs
#	web/ASC.Web.Core/Sms/SmsSender.cs
#	web/ASC.Web.Core/Sms/StudioSmsNotificationSettings.cs
#	web/ASC.Web.Core/StudioAdminMessageSettings.cs
#	web/ASC.Web.Core/StudioDefaultPageSettings.cs
#	web/ASC.Web.Core/StudioTrustedDomainSettings.cs
#	web/ASC.Web.Core/TariffSettings.cs
#	web/ASC.Web.Core/Tfa/TfaAppAuthSettings.cs
#	web/ASC.Web.Core/Tfa/TfaAppUserSettings.cs
#	web/ASC.Web.Core/TipsSettings.cs
#	web/ASC.Web.Core/Users/CustomNamingPeople.cs
#	web/ASC.Web.Core/Users/UserHelpTourSettings.cs
#	web/ASC.Web.Core/Users/UserPhotoManager.cs
#	web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs
#	web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs
#	web/ASC.Web.Core/Utility/ColorThemesSettings.cs
#	web/ASC.Web.Core/Utility/PasswordSettings.cs
#	web/ASC.Web.Core/Utility/Settings/TenantAccessSettings.cs
#	web/ASC.Web.Core/Utility/Settings/WebItemSettings.cs
#	web/ASC.Web.Core/Utility/Settings/WizardSettings.cs
#	web/ASC.Web.Core/Utility/TenantExtra.cs
#	web/ASC.Web.Core/WhiteLabel/AdditionalWhiteLabelSettings.cs
#	web/ASC.Web.Core/WhiteLabel/CompanyWhiteLabelSettings.cs
#	web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs
#	web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs
2022-04-14 15:21:06 +03:00

360 lines
12 KiB
C#

/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using Microsoft.Extensions.Caching.Distributed;
namespace ASC.Common.Threading;
[Transient]
public class DistributedTaskQueue
{
public const string QUEUE_DEFAULT_PREFIX = "asc_distributed_task_queue_";
public static readonly int INSTANCE_ID = Process.GetCurrentProcess().Id;
private readonly ConcurrentDictionary<string, CancellationTokenSource> _cancelations;
private readonly IServiceProvider _serviceProvider;
private readonly ICacheNotify<DistributedTaskCancelation> _cancellationCacheNotify;
private readonly IDistributedCache _distributedCache;
private readonly ILog _logger;
/// <summary>
/// setup -1 for infinity thread counts
/// </summary>
private int _maxThreadsCount = 1;
private string _name;
private readonly int _timeUntilUnregisterInSeconds = 60;
private TaskScheduler Scheduler { get; set; } = TaskScheduler.Default;
public DistributedTaskQueue(IServiceProvider serviceProvider,
ICacheNotify<DistributedTaskCancelation> cancelTaskNotify,
IDistributedCache distributedCache,
IOptionsMonitor<ILog> options)
{
_distributedCache = distributedCache;
_serviceProvider = serviceProvider;
_cancellationCacheNotify = cancelTaskNotify;
_cancelations = new ConcurrentDictionary<string, CancellationTokenSource>();
_logger = options.CurrentValue;
_cancellationCacheNotify.Subscribe((c) =>
{
if (_cancelations.TryGetValue(c.Id, out var s))
{
s.Cancel();
}
}, CacheNotifyAction.Remove);
}
public string Name
{
get => _name;
set => _name = QUEUE_DEFAULT_PREFIX + value;
}
public int MaxThreadsCount
{
get
{
return _maxThreadsCount;
}
set
{
Scheduler = value <= 0
? TaskScheduler.Default
: new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, value).ConcurrentScheduler;
if (value > 0)
{
_maxThreadsCount = value;
}
}
}
public void EnqueueTask(DistributedTaskProgress taskProgress)
{
EnqueueTask((a, b) => taskProgress.RunJob(), taskProgress);
}
public void EnqueueTask(Action<DistributedTask, CancellationToken> action, DistributedTask distributedTask = null)
{
if (distributedTask == null)
{
distributedTask = new DistributedTask();
}
distributedTask.InstanceId = INSTANCE_ID;
var cancelation = new CancellationTokenSource();
var token = cancelation.Token;
_cancelations[distributedTask.Id] = cancelation;
var task = new Task(() => { action(distributedTask, token); }, token, TaskCreationOptions.LongRunning);
task.ConfigureAwait(false)
.GetAwaiter()
.OnCompleted(() => OnCompleted(task, distributedTask.Id));
distributedTask.Status = DistributedTaskStatus.Running;
if (distributedTask.Publication == null)
{
distributedTask.Publication = GetPublication();
}
distributedTask.PublishChanges();
task.Start(Scheduler);
_logger.TraceFormat("EnqueueTask '{DistributedTaskId}' by instanse id '{InstanceId}'", distributedTask.Id, INSTANCE_ID);
}
public void EnqueueTask(Func<DistributedTask, CancellationToken, Task> action, DistributedTask distributedTask = null)
{
if (distributedTask == null)
{
distributedTask = new DistributedTask();
}
distributedTask.InstanceId = INSTANCE_ID;
var cancelation = new CancellationTokenSource();
var token = cancelation.Token;
_cancelations[distributedTask.Id] = cancelation;
var task = new Task(() =>
{
var t = action(distributedTask, token);
t.ConfigureAwait(false)
.GetAwaiter()
.OnCompleted(() => OnCompleted(t, distributedTask.Id));
}, token, TaskCreationOptions.LongRunning);
task.ConfigureAwait(false);
distributedTask.Status = DistributedTaskStatus.Running;
if (distributedTask.Publication == null)
{
distributedTask.Publication = GetPublication();
}
distributedTask.PublishChanges();
task.Start(Scheduler);
_logger.TraceFormat("EnqueueTask '{DistributedTaskId}' by instanse id '{InstanceId}'", distributedTask.Id, INSTANCE_ID);
}
public IEnumerable<DistributedTask> GetAllTasks(int? instanceId = null)
{
var queueTasks = LoadFromCache();
queueTasks = DeleteOrphanCacheItem(queueTasks);
if (instanceId.HasValue)
{
queueTasks = queueTasks.Where(x => x.InstanceId == instanceId.Value);
}
foreach (var task in queueTasks)
{
if (task.Publication == null)
{
task.Publication = GetPublication();
}
}
return queueTasks;
}
public IEnumerable<T> GetAllTasks<T>() where T : DistributedTask
{
return GetAllTasks().Select(x => Map(x, _serviceProvider.GetService<T>()));
}
public T PeekTask<T>(string id) where T : DistributedTask
{
var taskById = GetAllTasks().FirstOrDefault(x => x.Id == id);
if (taskById == null) return null;
return Map(taskById, _serviceProvider.GetService<T>());
}
public void DequeueTask(string id)
{
var queueTasks = GetAllTasks().ToList();
if (!queueTasks.Exists(x => x.Id == id)) return;
_cancellationCacheNotify.Publish(new DistributedTaskCancelation() { Id = id }, CacheNotifyAction.Remove);
queueTasks = queueTasks.FindAll(x => x.Id != id);
if (queueTasks.Count == 0)
{
_distributedCache.Remove(_name);
}
else
{
SaveToCache(queueTasks);
}
_logger.TraceFormat("DequeueTask '{DistributedTaskId}' by instanse id '{InstanceId}'", id, INSTANCE_ID);
}
private void OnCompleted(Task task, string id)
{
var distributedTask = GetAllTasks().FirstOrDefault(x => x.Id == id);
if (distributedTask != null)
{
distributedTask.Status = DistributedTaskStatus.Completed;
if (task.Exception != null)
{
distributedTask.Exception = task.Exception;
}
if (task.IsFaulted)
{
distributedTask.Status = DistributedTaskStatus.Failted;
}
if (task.IsCanceled)
{
distributedTask.Status = DistributedTaskStatus.Canceled;
}
_cancelations.TryRemove(id, out _);
distributedTask.PublishChanges();
}
}
private Action<DistributedTask> GetPublication()
{
return (task) =>
{
var queueTasks = GetAllTasks().ToList().FindAll(x => x.Id != task.Id);
task.LastModifiedOn = DateTime.UtcNow;
queueTasks.Add(task);
SaveToCache(queueTasks);
_logger.TraceFormat("Publication DistributedTask '{DistributedTaskId}' by instanse id '{InstanceId}' ", task.Id, task.InstanceId);
};
}
private void SaveToCache(IEnumerable<DistributedTask> queueTasks)
{
using var ms = new MemoryStream();
Serializer.Serialize(ms, queueTasks);
_distributedCache.Set(_name, ms.ToArray(), new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromMinutes(15)
});
}
private IEnumerable<DistributedTask> LoadFromCache()
{
var serializedObject = _distributedCache.Get(_name);
if (serializedObject == null) return new List<DistributedTask>();
using var ms = new MemoryStream(serializedObject);
return Serializer.Deserialize<List<DistributedTask>>(ms);
}
private IEnumerable<DistributedTask> DeleteOrphanCacheItem(IEnumerable<DistributedTask> queueTasks)
{
if (!queueTasks.Any()) return queueTasks;
var orphans = queueTasks.Where(IsOrphanCacheItem);
if (!orphans.Any()) return queueTasks;
queueTasks = queueTasks.Except(queueTasks);
SaveToCache(queueTasks);
return queueTasks;
}
private bool IsOrphanCacheItem(DistributedTask obj)
{
return obj.LastModifiedOn.AddSeconds(_timeUntilUnregisterInSeconds) < DateTime.UtcNow;
}
/// <summary>
/// Maps the source object to destination object.
/// </summary>
/// <typeparam name="T">Type of destination object.</typeparam>
/// <typeparam name="TU">Type of source object.</typeparam>
/// <param name="destination">Destination object.</param>
/// <param name="source">Source object.</param>
/// <returns>Updated destination object.</returns>
private T Map<T, TU>(TU source, T destination)
{
destination.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.ToList()
.ForEach(field =>
{
var sf = source.GetType().GetField(field.Name, BindingFlags.NonPublic | BindingFlags.Instance);
if (sf != null)
{
var value = sf.GetValue(source);
destination.GetType().GetField(field.Name, BindingFlags.NonPublic | BindingFlags.Instance).SetValue(destination, value);
}
});
destination.GetType().GetProperties().Where(p => p.CanWrite == true && !p.GetIndexParameters().Any())
.ToList()
.ForEach(prop =>
{
var sp = source.GetType().GetProperty(prop.Name);
if (sp != null)
{
var value = sp.GetValue(source, null);
destination.GetType().GetProperty(prop.Name).SetValue(destination, value, null);
}
});
return destination;
}
}