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

120 lines
3.9 KiB
C#
Raw Normal View History

2020-02-17 08:58:14 +00:00
namespace ASC.Data.Reassigns
2019-06-21 12:42:27 +00:00
{
2022-01-21 10:47:56 +00:00
public static class QueueWorker
2019-06-21 12:42:27 +00:00
{
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-12 09:58:08 +00:00
protected IHttpContextAccessor HttpContextAccessor { get; }
protected IServiceProvider ServiceProvider { get; }
2019-09-09 12:56:33 +00:00
2022-01-21 09:19:57 +00:00
private readonly object _synchRoot = new object();
protected readonly DistributedTaskQueue Queue;
2020-08-07 15:42:58 +00:00
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);
2022-02-10 08:44:59 +00:00
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
{
2022-01-21 09:19:57 +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);
2020-08-07 15:42:58 +00:00
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
{
return Start(tenantId, user.Id, () =>
2020-08-07 15:42:58 +00:00
{
var result = ServiceProvider.GetService<RemoveProgressItem>();
result.Init(tenantId, user, currentUserId, notify);
2020-08-07 15:42:58 +00:00
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
}