DocSpace-client/common/ASC.Common/Threading/DistributedTaskQueue.cs

290 lines
10 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +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.Common.Threading
2019-10-14 08:23:45 +00:00
{
2020-10-19 15:53:15 +00:00
[Singletone]
2019-10-14 08:23:45 +00:00
public class DistributedTaskCacheNotify
{
public ConcurrentDictionary<string, CancellationTokenSource> Cancelations { get; }
public ICache Cache { get; }
private readonly IEventBus<DistributedTaskCancelation> _eventBusNotify;
private readonly IEventBus<DistributedTaskCache> _eventBusCache;
2019-10-14 08:23:45 +00:00
2021-01-12 17:51:14 +00:00
public DistributedTaskCacheNotify(
IEventBus<DistributedTaskCancelation> eventBusNotify,
IEventBus<DistributedTaskCache> eventBusCache,
2021-01-12 17:51:14 +00:00
ICache cache)
2019-10-14 08:23:45 +00:00
{
Cancelations = new ConcurrentDictionary<string, CancellationTokenSource>();
2021-01-12 17:51:14 +00:00
Cache = cache;
2020-03-04 07:42:43 +00:00
_eventBusNotify = eventBusNotify;
2019-10-14 08:23:45 +00:00
eventBusNotify.Subscribe((c) =>
2019-10-14 08:23:45 +00:00
{
if (Cancelations.TryGetValue(c.Id, out var s)) s.Cancel();
}, EventType.Remove);
2019-10-14 08:23:45 +00:00
_eventBusCache = eventBusCache;
2019-10-14 08:23:45 +00:00
eventBusCache.Subscribe((c) =>
2019-10-14 08:23:45 +00:00
{
Cache.HashSet(c.Key, c.Id, (DistributedTaskCache)null);
}, EventType.Remove);
2019-10-14 08:23:45 +00:00
eventBusCache.Subscribe((c) =>
2019-10-14 08:23:45 +00:00
{
Cache.HashSet(c.Key, c.Id, c);
}, EventType.InsertOrUpdate);
2019-10-14 08:23:45 +00:00
}
public void CancelTask(string id) =>
_eventBusNotify.Publish(new DistributedTaskCancelation() { Id = id }, EventType.Remove);
2019-10-14 08:23:45 +00:00
public void SetTask(DistributedTask task) =>
_eventBusCache.Publish(task.DistributedTaskCache, EventType.InsertOrUpdate);
2019-10-14 08:23:45 +00:00
public void RemoveTask(string id, string key) =>
_eventBusCache.Publish(new DistributedTaskCache() { Id = id, Key = key }, EventType.Remove);
2019-10-14 08:23:45 +00:00
}
2020-07-13 15:26:21 +00:00
[Singletone(typeof(ConfigureDistributedTaskQueue))]
2020-07-13 15:26:21 +00:00
public class DistributedTaskQueueOptionsManager : OptionsManager<DistributedTaskQueue>
{
public DistributedTaskQueueOptionsManager(IOptionsFactory<DistributedTaskQueue> factory) : base(factory) { }
public DistributedTaskQueue Get<T>() where T : DistributedTask => Get(typeof(T).FullName);
2020-07-13 15:26:21 +00:00
}
[Scope]
2020-07-13 15:26:21 +00:00
public class ConfigureDistributedTaskQueue : IConfigureNamedOptions<DistributedTaskQueue>
{
private DistributedTaskCacheNotify DistributedTaskCacheNotify { get; }
2020-08-06 18:07:27 +00:00
public IServiceProvider ServiceProvider { get; }
public ConfigureDistributedTaskQueue(DistributedTaskCacheNotify distributedTaskCacheNotify, IServiceProvider serviceProvider)
2020-07-13 15:26:21 +00:00
{
DistributedTaskCacheNotify = distributedTaskCacheNotify;
ServiceProvider = serviceProvider;
2020-07-13 15:26:21 +00:00
}
public void Configure(DistributedTaskQueue queue)
{
2020-08-06 18:07:27 +00:00
queue.DistributedTaskCacheNotify = DistributedTaskCacheNotify;
queue.ServiceProvider = ServiceProvider;
2020-07-13 15:26:21 +00:00
}
public void Configure(string name, DistributedTaskQueue options)
{
Configure(options);
options.Name = name;
}
}
2019-10-14 08:23:45 +00:00
2019-05-15 14:56:09 +00:00
public class DistributedTaskQueue
{
2020-08-06 18:07:27 +00:00
public IServiceProvider ServiceProvider { get; set; }
public DistributedTaskCacheNotify DistributedTaskCacheNotify { get; set; }
public string Name
{
get => _name;
set => _name = value + GetType().Name;
}
2020-07-13 15:26:21 +00:00
public int MaxThreadsCount
{
set => Scheduler = value <= 0
2020-07-13 15:26:21 +00:00
? TaskScheduler.Default
: new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, value).ConcurrentScheduler;
2020-07-13 15:26:21 +00:00
}
2019-10-14 08:23:45 +00:00
public static readonly int InstanceId;
private TaskScheduler Scheduler { get; set; } = TaskScheduler.Default;
private ICache Cache => DistributedTaskCacheNotify.Cache;
private ConcurrentDictionary<string, CancellationTokenSource> Cancelations =>
DistributedTaskCacheNotify.Cancelations;
private string _name;
static DistributedTaskQueue() => InstanceId = Process.GetCurrentProcess().Id;
2019-05-15 14:56:09 +00:00
public void QueueTask(DistributedTaskProgress taskProgress) =>
QueueTask((a, b) => taskProgress.RunJob(), taskProgress);
2020-10-02 07:19:02 +00:00
2019-05-15 14:56:09 +00:00
public void QueueTask(Action<DistributedTask, CancellationToken> action, DistributedTask distributedTask = null)
{
if (distributedTask == null) distributedTask = new DistributedTask();
2019-05-15 14:56:09 +00:00
distributedTask.InstanceId = InstanceId;
2019-05-15 14:56:09 +00:00
var cancelation = new CancellationTokenSource();
var token = cancelation.Token;
2020-07-13 15:26:21 +00:00
Cancelations[distributedTask.Id] = cancelation;
2019-05-15 14:56:09 +00:00
2021-01-13 09:41:29 +00:00
var task = new Task(() => { action(distributedTask, token); }, token, TaskCreationOptions.LongRunning);
task.ConfigureAwait(false)
2019-05-15 14:56:09 +00:00
.GetAwaiter()
.OnCompleted(() => OnCompleted(task, distributedTask.Id));
distributedTask.Status = DistributedTaskStatus.Running;
if (distributedTask.Publication == null)
distributedTask.Publication = GetPublication();
2019-05-15 14:56:09 +00:00
distributedTask.PublishChanges();
2020-07-13 15:26:21 +00:00
task.Start(Scheduler);
2019-05-15 14:56:09 +00:00
}
public IEnumerable<DistributedTask> GetTasks()
2020-03-04 07:42:43 +00:00
{
var tasks = Cache.HashGetAll<DistributedTaskCache>(_name).Values.Select(r => new DistributedTask(r)).ToList();
2020-08-06 18:07:27 +00:00
tasks.ForEach(t =>
{
if (t.Publication == null) t.Publication = GetPublication();
});
2020-08-06 18:07:27 +00:00
return tasks;
}
public IEnumerable<T> GetTasks<T>() where T : DistributedTask
{
var tasks = Cache.HashGetAll<DistributedTaskCache>(_name).Values.Select(r =>
2020-08-06 18:07:27 +00:00
{
var result = ServiceProvider.GetService<T>();
result.DistributedTaskCache = r;
2020-08-06 18:07:27 +00:00
return result;
}).ToList();
2019-05-15 14:56:09 +00:00
tasks.ForEach(t =>
{
if (t.Publication == null) t.Publication = GetPublication();
});
2019-05-15 14:56:09 +00:00
return tasks;
}
2020-08-07 15:42:58 +00:00
public T GetTask<T>(string id) where T : DistributedTask
{
var cache = Cache.HashGet<DistributedTaskCache>(_name, id);
2020-08-07 15:42:58 +00:00
if (cache != null)
{
using var scope = ServiceProvider.CreateScope();
var task = scope.ServiceProvider.GetService<T>();
task.DistributedTaskCache = cache;
2020-08-07 15:42:58 +00:00
if (task != null && task.Publication == null)
task.Publication = GetPublication();
2020-08-07 15:42:58 +00:00
return task;
}
return null;
}
2019-05-15 14:56:09 +00:00
public DistributedTask GetTask(string id)
2020-08-07 15:42:58 +00:00
{
var cache = Cache.HashGet<DistributedTaskCache>(_name, id);
2020-08-07 15:42:58 +00:00
if (cache != null)
{
var task = new DistributedTask();
2020-10-13 14:28:10 +00:00
task.DistributedTaskCache = cache;
if (task != null && task.Publication == null) task.Publication = GetPublication();
2020-08-07 15:42:58 +00:00
return task;
}
return null;
2020-02-07 11:34:25 +00:00
}
public void SetTask(DistributedTask task) => DistributedTaskCacheNotify.SetTask(task);
2019-05-15 14:56:09 +00:00
public void RemoveTask(string id) => DistributedTaskCacheNotify.RemoveTask(id, _name);
2020-02-07 11:34:25 +00:00
public void CancelTask(string id) => DistributedTaskCacheNotify.CancelTask(id);
2020-02-07 11:34:25 +00:00
2019-05-15 14:56:09 +00:00
private void OnCompleted(Task task, string id)
{
var distributedTask = GetTask(id);
if (distributedTask != null)
{
distributedTask.Status = DistributedTaskStatus.Completed;
distributedTask.Exception = task.Exception;
if (task.IsFaulted) distributedTask.Status = DistributedTaskStatus.Failted;
if (task.IsCanceled) distributedTask.Status = DistributedTaskStatus.Canceled;
2019-08-16 08:44:03 +00:00
2020-07-13 15:26:21 +00:00
Cancelations.TryRemove(id, out _);
2019-05-15 14:56:09 +00:00
distributedTask.PublishChanges();
}
}
private Action<DistributedTask> GetPublication()
{
2019-10-14 08:23:45 +00:00
return (t) =>
{
if (t.DistributedTaskCache != null) t.DistributedTaskCache.Key = _name;
2019-10-14 08:23:45 +00:00
DistributedTaskCacheNotify.SetTask(t);
};
2019-05-15 14:56:09 +00:00
}
}
public static class DistributedTaskQueueExtention
{
public static DIHelper AddDistributedTaskQueueService<T>(this DIHelper services, int maxThreadsCount) where T : DistributedTask
{
services.TryAdd<DistributedTaskCacheNotify>();
services.TryAdd<DistributedTaskQueueOptionsManager>();
services.TryAdd<DistributedTaskQueue>();
2021-05-19 08:31:50 +00:00
var type = typeof(T);
if (!type.IsAbstract) services.TryAdd<T>();
services.TryAddSingleton<IConfigureOptions<DistributedTaskQueue>, ConfigureDistributedTaskQueue>();
2021-05-19 08:31:50 +00:00
_ = services.Configure<DistributedTaskQueue>(type.Name, r =>
{
r.MaxThreadsCount = maxThreadsCount;
//r.errorCount = 1;
});
return services;
}
}
2019-05-15 14:56:09 +00:00
}