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

172 lines
6.7 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.
*
*/
using System;
using System.Collections.Generic;
using ASC.Common;
using ASC.Common.Threading.Progress;
2019-10-22 11:21:44 +00:00
using ASC.Core.Users;
2020-02-17 08:58:14 +00:00
2019-06-21 10:42:16 +00:00
using Microsoft.AspNetCore.Http;
2019-10-31 11:28:30 +00:00
using Microsoft.Extensions.Options;
2020-06-03 15:04:14 +00:00
using Microsoft.Extensions.Primitives;
2019-06-21 10:42:16 +00:00
2020-02-17 08:58:14 +00:00
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
}
public class QueueWorker<T> where T : class, IProgressItem
{
2019-10-22 11:21:44 +00:00
protected readonly ProgressQueue<T> Queue;
2019-06-21 12:42:27 +00:00
public IHttpContextAccessor HttpContextAccessor { get; }
2019-09-18 12:14:15 +00:00
public IServiceProvider ServiceProvider { get; }
2019-09-09 12:56:33 +00:00
2019-10-22 11:21:44 +00:00
public QueueWorker(
IHttpContextAccessor httpContextAccessor,
IServiceProvider serviceProvider,
ProgressQueueOptionsManager<T> optionsQueue)
2019-06-21 12:42:27 +00:00
{
HttpContextAccessor = httpContextAccessor;
2019-09-18 12:14:15 +00:00
ServiceProvider = serviceProvider;
2019-10-22 11:21:44 +00:00
Queue = optionsQueue.Value;
2019-06-21 12:42:27 +00:00
}
2020-02-17 08:58:14 +00:00
public string GetProgressItemId(int tenantId, Guid userId)
{
return string.Format("{0}_{1}_{2}", tenantId, userId, typeof(T).Name);
}
public T GetProgressItemStatus(int tenantId, Guid userId)
{
var id = GetProgressItemId(tenantId, userId);
return Queue.GetStatus(id) as T;
}
public void Terminate(int tenantId, Guid userId)
{
var item = GetProgressItemStatus(tenantId, userId);
if (item != null)
Queue.Remove(item);
2019-06-21 12:42:27 +00:00
}
protected IProgressItem Start(int tenantId, Guid userId, Func<T> constructor)
{
2020-02-17 08:58:14 +00:00
lock (Queue.SynchRoot)
{
var task = GetProgressItemStatus(tenantId, userId);
if (task != null && task.IsCompleted)
{
Queue.Remove(task);
task = null;
}
if (task == null)
{
task = constructor();
Queue.Add(task);
}
if (!Queue.IsStarted)
Queue.Start(x => x.RunJob());
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
}
public class QueueWorkerReassign : QueueWorker<ReassignProgressItem>
{
public QueueWorkerRemove QueueWorkerRemove { get; }
2019-09-09 12:56:33 +00:00
public QueueWorkerReassign(
IHttpContextAccessor httpContextAccessor,
2019-09-18 12:14:15 +00:00
IServiceProvider serviceProvider,
2019-10-22 11:21:44 +00:00
QueueWorkerRemove queueWorkerRemove,
ProgressQueueOptionsManager<ReassignProgressItem> optionsQueue) :
base(httpContextAccessor, serviceProvider, optionsQueue)
2019-06-21 12:42:27 +00:00
{
QueueWorkerRemove = queueWorkerRemove;
}
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-02-17 08:58:14 +00:00
return Start(tenantId, fromUserId, () => new ReassignProgressItem(ServiceProvider, HttpContextAccessor.HttpContext, this, QueueWorkerRemove, tenantId, fromUserId, toUserId, currentUserId, deleteProfile)) as ReassignProgressItem;
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,
ProgressQueueOptionsManager<RemoveProgressItem> optionsQueue) :
base(httpContextAccessor, serviceProvider, optionsQueue)
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-02-17 08:58:14 +00:00
return Start(tenantId, user.ID, () => new RemoveProgressItem(ServiceProvider, HttpContextAccessor.HttpContext, this, tenantId, user, currentUserId, notify)) as RemoveProgressItem;
2019-06-21 12:42:27 +00:00
}
2019-10-31 11:28:30 +00:00
}
public static class QueueExtension
2019-10-31 11:28:30 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddQueueWorkerRemoveService(this DIHelper services)
2019-10-31 11:28:30 +00:00
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<QueueWorkerRemove>())
{
services.TryAddSingleton<ProgressQueueOptionsManager<RemoveProgressItem>>();
services.TryAddSingleton<ProgressQueue<RemoveProgressItem>>();
services.AddSingleton<IPostConfigureOptions<ProgressQueue<RemoveProgressItem>>, ConfigureProgressQueue<RemoveProgressItem>>();
}
2019-10-31 11:28:30 +00:00
2020-02-17 08:58:14 +00:00
return services;
2019-10-31 11:28:30 +00:00
}
2020-02-17 08:58:14 +00:00
public static DIHelper AddQueueWorkerReassignService(this DIHelper services)
2019-10-31 11:28:30 +00:00
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<QueueWorkerReassign>())
{
services.TryAddSingleton<ProgressQueueOptionsManager<ReassignProgressItem>>();
services.TryAddSingleton<ProgressQueue<ReassignProgressItem>>();
services.AddSingleton<IPostConfigureOptions<ProgressQueue<ReassignProgressItem>>, ConfigureProgressQueue<ReassignProgressItem>>();
2019-10-31 11:28:30 +00:00
2020-07-17 10:52:28 +00:00
return services
.AddQueueWorkerRemoveService();
}
return services;
2019-10-31 11:28:30 +00:00
}
2020-02-17 08:58:14 +00:00
}
}