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

133 lines
5.4 KiB
C#
Raw Normal View History

2019-06-21 10:42:16 +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 System.Linq;
using System.Web;
using ASC.Common.Threading.Progress;
using ASC.Core.Users;
using ASC.MessagingSystem;
2019-08-01 08:47:15 +00:00
using ASC.Web.Studio.Core.Notify;
2019-06-21 10:42:16 +00:00
using Microsoft.AspNetCore.Http;
namespace ASC.Data.Reassigns
2019-06-21 12:42:27 +00:00
{
public class QueueWorker
{
public static Dictionary<string, string> GetHttpHeaders(HttpRequest httpRequest)
{
return httpRequest?.Headers.Keys.ToDictionary(key => key, key => httpRequest.Headers[key].ToString());
}
}
public class QueueWorker<T> where T : class, IProgressItem
2019-06-21 10:42:16 +00:00
{
2019-06-21 12:42:27 +00:00
protected static readonly ProgressQueue Queue = new ProgressQueue(1, TimeSpan.FromMinutes(5), true);
public IHttpContextAccessor HttpContextAccessor { get; }
public MessageService MessageService { get; }
2019-08-01 08:47:15 +00:00
public StudioNotifyService StudioNotifyService { get; }
public QueueWorker(IHttpContextAccessor httpContextAccessor, MessageService messageService, StudioNotifyService studioNotifyService)
2019-06-21 12:42:27 +00:00
{
HttpContextAccessor = httpContextAccessor;
MessageService = messageService;
2019-08-01 08:47:15 +00:00
StudioNotifyService = studioNotifyService;
2019-06-21 12:42:27 +00:00
}
public string GetProgressItemId(int tenantId, Guid userId)
2019-06-21 10:42:16 +00:00
{
2019-06-21 12:42:27 +00:00
return string.Format("{0}_{1}_{2}", tenantId, userId, typeof(T).Name);
2019-06-21 10:42:16 +00:00
}
2019-06-21 12:42:27 +00:00
public T GetProgressItemStatus(int tenantId, Guid userId)
2019-06-21 10:42:16 +00:00
{
2019-06-21 12:42:27 +00:00
var id = GetProgressItemId(tenantId, userId);
return Queue.GetStatus(id) as T;
2019-06-21 10:42:16 +00:00
}
2019-06-21 12:42:27 +00:00
public void Terminate(int tenantId, Guid userId)
2019-06-21 10:42:16 +00:00
{
2019-06-21 12:42:27 +00:00
var item = GetProgressItemStatus(tenantId, userId);
2019-06-21 10:42:16 +00:00
if (item != null)
Queue.Remove(item);
2019-06-21 12:42:27 +00:00
}
protected IProgressItem Start(int tenantId, Guid userId, Func<T> constructor)
{
2019-06-21 10:42:16 +00:00
lock (Queue.SynchRoot)
{
2019-06-21 12:42:27 +00:00
var task = GetProgressItemStatus(tenantId, userId);
2019-06-21 10:42:16 +00:00
if (task != null && task.IsCompleted)
{
Queue.Remove(task);
task = null;
}
if (task == null)
{
2019-06-21 12:42:27 +00:00
task = constructor();
2019-06-21 10:42:16 +00:00
Queue.Add(task);
}
if (!Queue.IsStarted)
Queue.Start(x => x.RunJob());
return task;
2019-06-21 12:42:27 +00:00
}
2019-06-21 10:42:16 +00:00
}
2019-06-21 12:42:27 +00:00
}
public class QueueWorkerReassign : QueueWorker<ReassignProgressItem>
{
public QueueWorkerRemove QueueWorkerRemove { get; }
2019-08-01 08:47:15 +00:00
public QueueWorkerReassign(IHttpContextAccessor httpContextAccessor,
MessageService messageService,
StudioNotifyService studioNotifyService,
QueueWorkerRemove queueWorkerRemove) : base(httpContextAccessor, messageService, studioNotifyService)
2019-06-21 12:42:27 +00:00
{
QueueWorkerRemove = queueWorkerRemove;
}
public ReassignProgressItem Start(int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
{
2019-08-01 08:47:15 +00:00
return Start(tenantId, fromUserId, () => new ReassignProgressItem(HttpContextAccessor.HttpContext, MessageService, this, QueueWorkerRemove, StudioNotifyService, tenantId, fromUserId, toUserId, currentUserId, deleteProfile)) as ReassignProgressItem;
2019-06-21 12:42:27 +00:00
}
}
public class QueueWorkerRemove : QueueWorker<RemoveProgressItem>
{
2019-08-01 08:47:15 +00:00
public QueueWorkerRemove(IHttpContextAccessor httpContextAccessor, MessageService messageService, StudioNotifyService studioNotifyService) : base(httpContextAccessor, messageService, studioNotifyService)
2019-06-21 12:42:27 +00:00
{
}
public RemoveProgressItem Start(int tenantId, UserInfo user, Guid currentUserId, bool notify)
{
2019-08-01 08:47:15 +00:00
return Start(tenantId, user.ID, () => new RemoveProgressItem(HttpContextAccessor.HttpContext, MessageService, this, StudioNotifyService, tenantId, user, currentUserId, notify)) as RemoveProgressItem;
2019-06-21 12:42:27 +00:00
}
2019-06-21 10:42:16 +00:00
}
}