DocSpace-client/common/services/ASC.ElasticSearch/Service/Service.cs

131 lines
4.6 KiB
C#
Raw Normal View History

2020-01-24 13:07:51 +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;
2020-10-19 15:53:15 +00:00
using System.Threading.Tasks;
2020-01-24 13:07:51 +00:00
2020-10-19 15:53:15 +00:00
using ASC.Common;
2020-05-05 17:40:24 +00:00
using ASC.Common.Caching;
2020-01-24 13:07:51 +00:00
using ASC.Core;
using ASC.Core.Common.Settings;
using ASC.ElasticSearch.Core;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.ElasticSearch.Service
2020-05-05 17:40:24 +00:00
{
[Singletone(Additional = typeof(ServiceExtension))]
2020-01-24 13:07:51 +00:00
public class Service
{
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; }
private ICacheNotify<ReIndexAction> CacheNotify { get; }
2020-01-24 13:07:51 +00:00
public Service(IServiceProvider serviceProvider, ICacheNotify<ReIndexAction> cacheNotify)
2020-01-24 13:07:51 +00:00
{
ServiceProvider = serviceProvider;
2020-05-05 17:40:24 +00:00
CacheNotify = cacheNotify;
}
public void Subscribe()
{
CacheNotify.Subscribe((a) =>
{
ReIndex(a.Names.ToList(), a.Tenant);
}, CacheNotifyAction.Any);
2020-01-24 13:07:51 +00:00
}
public bool Support(string table)
{
return ServiceProvider.GetService<IEnumerable<IFactoryIndexer>>().Any(r => r.IndexName == table);
2020-01-24 13:07:51 +00:00
}
public void ReIndex(List<string> toReIndex, int tenant)
{
var allItems = ServiceProvider.GetService<IEnumerable<IFactoryIndexer>>().ToList();
2020-01-24 13:07:51 +00:00
var tasks = new List<Task>(toReIndex.Count);
foreach (var item in toReIndex)
{
var index = allItems.FirstOrDefault(r => r.IndexName == item);
if (index == null) continue;
var generic = typeof(BaseIndexer<>);
var instance = (IIndexer)Activator.CreateInstance(generic.MakeGenericType(index.GetType()), index);
tasks.Add(instance.ReIndex());
}
2020-05-05 17:40:24 +00:00
2022-01-19 09:51:30 +00:00
if (tasks.Count == 0) return;
2020-01-24 13:07:51 +00:00
Task.WhenAll(tasks).ContinueWith(r =>
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<ServiceScope>();
2020-09-07 12:01:15 +00:00
var (tenantManager, settingsManager) = scopeClass;
2020-08-31 08:18:07 +00:00
tenantManager.SetCurrentTenant(tenant);
settingsManager.ClearCache<SearchSettings>();
2020-01-24 13:07:51 +00:00
});
}
//public State GetState()
//{
// return new State
// {
// Indexing = Launcher.Indexing,
// LastIndexed = Launcher.LastIndexed
// };
//}
}
2020-10-19 15:53:15 +00:00
[Scope]
2020-08-24 18:41:06 +00:00
public class ServiceScope
{
2020-08-31 08:18:07 +00:00
private TenantManager TenantManager { get; }
private SettingsManager SettingsManager { get; }
2020-08-24 18:41:06 +00:00
public ServiceScope(TenantManager tenantManager, SettingsManager settingsManager)
{
TenantManager = tenantManager;
SettingsManager = settingsManager;
}
2020-08-31 08:18:07 +00:00
public void Deconstruct(out TenantManager tenantManager, out SettingsManager settingsManager)
{
tenantManager = TenantManager;
settingsManager = SettingsManager;
}
}
2022-01-21 10:47:56 +00:00
internal static class ServiceExtension
{
public static void Register(DIHelper services)
{
services.TryAdd<ServiceScope>();
}
2020-08-24 18:41:06 +00:00
}
2020-01-24 13:07:51 +00:00
}