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

143 lines
5.3 KiB
C#
Raw Normal View History

2020-02-17 08:58:14 +00:00
/*
*
* (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.
*
*/
namespace ASC.Data.Reassigns
2019-06-21 12:42:27 +00:00
{
public class QueueWorker
{
2020-06-03 15:04:14 +00:00
public static IDictionary<string, StringValues> GetHttpHeaders(HttpRequest httpRequest)
2020-02-17 08:58:14 +00:00
{
2020-06-03 15:04:14 +00:00
return httpRequest?.Headers;
2019-06-21 12:42:27 +00:00
}
2020-02-17 08:58:14 +00:00
}
2020-10-01 07:22:08 +00:00
2020-09-30 10:54:49 +00:00
public class QueueWorker<T> where T : DistributedTaskProgress
2020-02-17 08:58:14 +00:00
{
2020-08-07 15:42:58 +00:00
protected readonly DistributedTaskQueue Queue;
2019-06-21 12:42:27 +00:00
2020-08-12 09:58:08 +00:00
protected IHttpContextAccessor HttpContextAccessor { get; }
protected IServiceProvider ServiceProvider { get; }
2019-09-09 12:56:33 +00:00
2020-08-07 15:42:58 +00:00
public object SynchRoot = new object();
2019-10-22 11:21:44 +00:00
public QueueWorker(
IHttpContextAccessor httpContextAccessor,
IServiceProvider serviceProvider,
2020-08-07 15:42:58 +00:00
DistributedTaskQueueOptionsManager options)
2019-06-21 12:42:27 +00:00
{
HttpContextAccessor = httpContextAccessor;
2019-09-18 12:14:15 +00:00
ServiceProvider = serviceProvider;
Queue = options.Get<T>();
2019-06-21 12:42:27 +00:00
}
2020-10-01 07:22:08 +00:00
public static string GetProgressItemId(int tenantId, Guid userId)
2020-02-17 08:58:14 +00:00
{
2020-10-01 07:22:08 +00:00
return string.Format("{0}_{1}_{2}", tenantId, userId, typeof(T).Name);
2020-02-17 08:58:14 +00:00
}
public T GetProgressItemStatus(int tenantId, Guid userId)
{
2020-10-01 07:22:08 +00:00
var id = GetProgressItemId(tenantId, userId);
2020-08-07 15:42:58 +00:00
return Queue.GetTask<T>(id);
2020-02-17 08:58:14 +00:00
}
public void Terminate(int tenantId, Guid userId)
{
var item = GetProgressItemStatus(tenantId, userId);
if (item != null)
2020-08-07 15:42:58 +00:00
{
Queue.CancelTask(item.Id);
}
2019-06-21 12:42:27 +00:00
}
2020-09-30 10:54:49 +00:00
protected DistributedTaskProgress Start(int tenantId, Guid userId, Func<T> constructor)
2019-06-21 12:42:27 +00:00
{
2020-08-07 15:42:58 +00:00
lock (SynchRoot)
2020-02-17 08:58:14 +00:00
{
var task = GetProgressItemStatus(tenantId, userId);
if (task != null && task.IsCompleted)
{
2020-08-07 15:42:58 +00:00
Queue.RemoveTask(task.Id);
2020-02-17 08:58:14 +00:00
task = null;
}
if (task == null)
{
task = constructor();
2020-10-02 07:19:02 +00:00
Queue.QueueTask(task);
2020-02-17 08:58:14 +00:00
}
return task;
2019-06-21 12:42:27 +00:00
}
2020-02-17 08:58:14 +00:00
}
2019-06-21 12:42:27 +00:00
}
2020-10-27 15:34:22 +00:00
[Scope(Additional = typeof(ReassignProgressItemExtension))]
2019-06-21 12:42:27 +00:00
public class QueueWorkerReassign : QueueWorker<ReassignProgressItem>
{
2019-09-09 12:56:33 +00:00
public QueueWorkerReassign(
IHttpContextAccessor httpContextAccessor,
2019-09-18 12:14:15 +00:00
IServiceProvider serviceProvider,
2020-08-07 15:42:58 +00:00
DistributedTaskQueueOptionsManager options) :
base(httpContextAccessor, serviceProvider, options)
2019-06-21 12:42:27 +00:00
{
}
2020-02-17 08:58:14 +00:00
public ReassignProgressItem Start(int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
2019-06-21 12:42:27 +00:00
{
2020-08-07 15:42:58 +00:00
return Start(tenantId, fromUserId, () =>
{
var result = ServiceProvider.GetService<ReassignProgressItem>();
result.Init(tenantId, fromUserId, toUserId, currentUserId, deleteProfile);
return result;
}) as ReassignProgressItem;
2019-06-21 12:42:27 +00:00
}
}
2020-08-07 15:42:58 +00:00
2020-10-27 15:34:22 +00:00
[Scope(Additional = typeof(RemoveProgressItemExtension))]
2019-06-21 12:42:27 +00:00
public class QueueWorkerRemove : QueueWorker<RemoveProgressItem>
{
2019-09-18 12:14:15 +00:00
public QueueWorkerRemove(
IHttpContextAccessor httpContextAccessor,
2019-10-22 11:21:44 +00:00
IServiceProvider serviceProvider,
2020-08-07 15:42:58 +00:00
DistributedTaskQueueOptionsManager options) :
base(httpContextAccessor, serviceProvider, options)
2019-06-21 12:42:27 +00:00
{
}
2020-02-17 08:58:14 +00:00
public RemoveProgressItem Start(int tenantId, UserInfo user, Guid currentUserId, bool notify)
2019-06-21 12:42:27 +00:00
{
2020-08-07 15:42:58 +00:00
return Start(tenantId, user.ID, () =>
{
var result = ServiceProvider.GetService<RemoveProgressItem>();
result.Init(tenantId, user, currentUserId, notify);
return result;
}) as RemoveProgressItem;
2019-06-21 12:42:27 +00:00
}
2019-10-31 11:28:30 +00:00
}
2020-02-17 08:58:14 +00:00
}