DocSpace-client/common/ASC.Data.Reassigns/QueueWorker.cs

133 lines
4.9 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2020-02-17 08:58:14 +00:00
2022-03-17 15:13:38 +00:00
namespace ASC.Data.Reassigns;
public class QueueWorker<T> where T : DistributedTaskProgress
{
2022-04-15 09:08:06 +00:00
protected IServiceScopeFactory _serviceProvider;
2022-03-17 15:13:38 +00:00
private readonly object _synchRoot = new object();
2022-03-25 16:26:06 +00:00
protected readonly DistributedTaskQueue _queue;
2022-04-15 09:08:06 +00:00
protected readonly IDictionary<string, StringValues> _httpHeaders;
2022-03-17 15:13:38 +00:00
public QueueWorker(
IHttpContextAccessor httpContextAccessor,
2022-03-24 11:57:39 +00:00
IServiceScopeFactory serviceProvider,
2022-04-14 19:42:15 +00:00
IDistributedTaskQueueFactory queueFactory,
string queueName)
2020-02-17 08:58:14 +00:00
{
2022-04-15 09:08:06 +00:00
_serviceProvider = serviceProvider;
2022-04-14 19:42:15 +00:00
_queue = queueFactory.CreateQueue(queueName);
2022-04-15 09:08:06 +00:00
_httpHeaders = httpContextAccessor.HttpContext.Request?.Headers;
2022-03-17 15:13:38 +00:00
}
2019-09-09 12:56:33 +00:00
2022-03-17 15:13:38 +00:00
public static string GetProgressItemId(int tenantId, Guid userId)
{
return string.Format("{0}_{1}_{2}", tenantId, userId, typeof(T).Name);
}
2020-08-07 15:42:58 +00:00
2022-03-17 15:13:38 +00:00
public T GetProgressItemStatus(int tenantId, Guid userId)
{
var id = GetProgressItemId(tenantId, userId);
2019-06-21 12:42:27 +00:00
2022-04-14 19:42:15 +00:00
return _queue.PeekTask<T>(id);
2022-03-17 15:13:38 +00:00
}
2020-02-17 08:58:14 +00:00
2022-03-17 15:13:38 +00:00
public void Terminate(int tenantId, Guid userId)
{
var item = GetProgressItemStatus(tenantId, userId);
2022-02-10 08:44:59 +00:00
2022-03-17 15:13:38 +00:00
if (item != null)
{
2022-04-14 19:42:15 +00:00
_queue.DequeueTask(item.Id);
2020-02-17 08:58:14 +00:00
}
2022-03-17 15:13:38 +00:00
}
2020-02-17 08:58:14 +00:00
2022-04-14 19:42:15 +00:00
protected T Start(int tenantId, Guid userId, T newTask)
2022-03-17 15:13:38 +00:00
{
lock (_synchRoot)
2020-02-17 08:58:14 +00:00
{
2022-03-17 15:13:38 +00:00
var task = GetProgressItemStatus(tenantId, userId);
2020-02-17 08:58:14 +00:00
2022-03-17 15:13:38 +00:00
if (task != null && task.IsCompleted)
2020-08-07 15:42:58 +00:00
{
2022-04-14 19:42:15 +00:00
_queue.DequeueTask(task.Id);
2022-03-17 15:13:38 +00:00
task = null;
2020-08-07 15:42:58 +00:00
}
2019-06-21 12:42:27 +00:00
2022-03-17 15:13:38 +00:00
if (task == null)
2020-02-17 08:58:14 +00:00
{
2022-04-14 19:42:15 +00:00
task = newTask;
_queue.EnqueueTask(task);
2019-06-21 12:42:27 +00:00
}
2022-03-17 15:13:38 +00:00
return task;
2020-02-17 08:58:14 +00:00
}
2019-06-21 12:42:27 +00:00
}
2022-03-17 15:13:38 +00:00
}
2019-06-21 12:42:27 +00:00
2022-03-17 15:13:38 +00:00
[Scope(Additional = typeof(ReassignProgressItemExtension))]
public class QueueWorkerReassign : QueueWorker<ReassignProgressItem>
{
2022-04-14 19:42:15 +00:00
public const string CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME = "user_data_reassign";
2022-03-17 15:13:38 +00:00
public QueueWorkerReassign(
IHttpContextAccessor httpContextAccessor,
2022-03-24 11:57:39 +00:00
IServiceScopeFactory serviceProvider,
2022-03-22 12:10:05 +00:00
IDistributedTaskQueueFactory queueFactory) :
base(httpContextAccessor, serviceProvider, queueFactory, CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME)
2019-06-21 12:42:27 +00:00
{
2022-03-17 15:13:38 +00:00
}
2019-06-21 12:42:27 +00:00
2022-03-17 15:13:38 +00:00
public ReassignProgressItem Start(int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
{
2022-04-15 09:08:06 +00:00
var result = new ReassignProgressItem(_serviceProvider, _httpHeaders, tenantId, fromUserId, toUserId, currentUserId, deleteProfile);
2022-04-14 19:42:15 +00:00
return Start(tenantId, fromUserId, result);
2019-06-21 12:42:27 +00:00
}
2022-03-17 15:13:38 +00:00
}
2020-08-07 15:42:58 +00:00
2022-03-17 15:13:38 +00:00
[Scope(Additional = typeof(RemoveProgressItemExtension))]
public class QueueWorkerRemove : QueueWorker<RemoveProgressItem>
{
2022-04-14 19:42:15 +00:00
public const string CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME = "user_data_remove";
2022-03-17 15:13:38 +00:00
public QueueWorkerRemove(
IHttpContextAccessor httpContextAccessor,
2022-03-24 11:57:39 +00:00
IServiceScopeFactory serviceProvider,
2022-03-22 12:10:05 +00:00
IDistributedTaskQueueFactory queueFactory) :
base(httpContextAccessor, serviceProvider, queueFactory, CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME)
2019-06-21 12:42:27 +00:00
{
2022-03-17 15:13:38 +00:00
}
2019-06-21 12:42:27 +00:00
2022-03-17 15:13:38 +00:00
public RemoveProgressItem Start(int tenantId, UserInfo user, Guid currentUserId, bool notify)
{
2022-04-15 09:08:06 +00:00
var result = new RemoveProgressItem(_serviceProvider, _httpHeaders, tenantId, user, currentUserId, notify);
2022-04-14 19:42:15 +00:00
return Start(tenantId, user.Id, result);
2019-10-31 11:28:30 +00:00
}
2020-02-17 08:58:14 +00:00
}