DocSpace-buildtools/common/services/ASC.ElasticSearch/Service/Launcher.cs

212 lines
7.1 KiB
C#
Raw Normal View History

2020-04-23 09:38:50 +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;
2020-04-29 14:11:31 +00:00
using System.Collections.Generic;
2020-04-23 09:38:50 +00:00
using System.Threading;
using System.Threading.Tasks;
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.Logging;
2020-05-03 15:50:40 +00:00
using ASC.ElasticSearch.Service;
2020-04-29 14:11:31 +00:00
using Autofac;
2020-04-23 09:38:50 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
2020-04-29 14:11:31 +00:00
namespace ASC.ElasticSearch
2020-04-23 09:38:50 +00:00
{
2020-10-19 15:53:15 +00:00
[Singletone]
2020-04-23 09:38:50 +00:00
public class ServiceLauncher : IHostedService
{
private ILog Log { get; }
private ICacheNotify<AscCacheItem> Notify { get; }
2020-08-12 09:58:08 +00:00
private ICacheNotify<IndexAction> IndexNotify { get; }
private IServiceProvider ServiceProvider { get; }
2020-04-29 14:11:31 +00:00
public IContainer Container { get; }
2020-04-23 09:38:50 +00:00
private bool IsStarted { get; set; }
private CancellationTokenSource CancellationTokenSource { get; set; }
private Timer Timer { get; set; }
2020-05-03 15:50:40 +00:00
private TimeSpan Period { get; set; }
2020-04-23 09:38:50 +00:00
2020-04-29 14:11:31 +00:00
public ServiceLauncher(
IOptionsMonitor<ILog> options,
ICacheNotify<AscCacheItem> notify,
2020-05-06 11:32:26 +00:00
ICacheNotify<IndexAction> indexNotify,
2020-04-29 14:11:31 +00:00
IServiceProvider serviceProvider,
2020-05-03 15:50:40 +00:00
IContainer container,
Settings settings)
2020-04-23 09:38:50 +00:00
{
Log = options.Get("ASC.Indexer");
Notify = notify;
2020-05-06 11:32:26 +00:00
IndexNotify = indexNotify;
2020-04-23 09:38:50 +00:00
ServiceProvider = serviceProvider;
2020-04-29 14:11:31 +00:00
Container = container;
2020-04-28 15:11:10 +00:00
CancellationTokenSource = new CancellationTokenSource();
2020-05-03 15:50:40 +00:00
Period = TimeSpan.FromMinutes(settings.Period.Value);
2020-04-23 09:38:50 +00:00
}
public Task StartAsync(CancellationToken cancellationToken)
{
try
{
Notify.Subscribe(async (item) =>
{
while (IsStarted)
{
await Task.Delay(10000);
}
IndexAll(true);
}, CacheNotifyAction.Any);
}
catch (Exception e)
{
Log.Error("Subscribe on start", e);
}
2020-05-05 17:40:24 +00:00
var task = new Task(async () =>
2020-04-23 09:38:50 +00:00
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<ServiceLauncherScope>();
2020-09-07 12:01:15 +00:00
var (factoryIndexer, service) = scopeClass;
2020-08-31 08:18:07 +00:00
while (!factoryIndexer.CheckState(false))
2020-04-23 09:38:50 +00:00
{
if (CancellationTokenSource.IsCancellationRequested)
{
return;
}
2020-05-05 17:40:24 +00:00
await Task.Delay(10000);
2020-04-23 09:38:50 +00:00
}
2020-08-31 08:18:07 +00:00
service.Subscribe();
2020-04-27 16:59:52 +00:00
Timer = new Timer(_ => IndexAll(), null, TimeSpan.Zero, TimeSpan.Zero);
2020-04-23 09:38:50 +00:00
}, CancellationTokenSource.Token, TaskCreationOptions.LongRunning);
task.Start();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
IsStarted = false;
if (Timer != null)
{
Timer.Dispose();
}
CancellationTokenSource.Cancel();
return Task.CompletedTask;
}
private void IndexAll(bool reindex = false)
{
Timer.Change(-1, -1);
IsStarted = true;
2020-04-29 14:11:31 +00:00
using var scope = Container.BeginLifetimeScope();
var wrappers = scope.Resolve<IEnumerable<IFactoryIndexer>>();
2020-04-23 09:38:50 +00:00
2020-04-29 14:11:31 +00:00
foreach (var w in wrappers)
{
IndexProduct(w, reindex);
}
2020-04-23 09:38:50 +00:00
2020-04-27 16:59:52 +00:00
Timer.Change(Period, Period);
2020-05-06 11:32:26 +00:00
IndexNotify.Publish(new IndexAction() { Indexing = "", LastIndexed = DateTime.Now.Ticks }, CacheNotifyAction.Any);
2020-04-27 16:59:52 +00:00
IsStarted = false;
}
2020-04-23 09:38:50 +00:00
2020-04-28 15:11:10 +00:00
public void IndexProduct(IFactoryIndexer product, bool reindex)
2020-04-27 16:59:52 +00:00
{
if (reindex)
2020-04-23 09:38:50 +00:00
{
try
{
if (!IsStarted) return;
2020-04-27 16:59:52 +00:00
Log.DebugFormat("Product reindex {0}", product.IndexName);
2020-04-28 15:11:10 +00:00
product.ReIndex();
2020-04-23 09:38:50 +00:00
}
catch (Exception e)
{
Log.Error(e);
2020-04-27 16:59:52 +00:00
Log.ErrorFormat("Product reindex {0}", product.IndexName);
2020-04-23 09:38:50 +00:00
}
2020-04-27 16:59:52 +00:00
}
2020-04-23 09:38:50 +00:00
2020-04-27 16:59:52 +00:00
try
{
if (!IsStarted) return;
Log.DebugFormat("Product {0}", product.IndexName);
2020-05-06 11:32:26 +00:00
IndexNotify.Publish(new IndexAction() { Indexing = product.IndexName, LastIndexed = 0 }, CacheNotifyAction.Any);
2020-04-27 16:59:52 +00:00
product.IndexAll();
}
catch (Exception e)
{
Log.Error(e);
Log.ErrorFormat("Product {0}", product.IndexName);
}
2020-04-23 09:38:50 +00:00
}
2020-08-24 18:41:06 +00:00
}
2020-07-29 21:57:58 +00:00
2020-08-24 18:41:06 +00:00
public class ServiceLauncherScope
{
2020-08-31 08:18:07 +00:00
private FactoryIndexer FactoryIndexer { get; }
private Service.Service Service { get; }
2020-07-29 21:57:58 +00:00
2020-08-24 18:41:06 +00:00
public ServiceLauncherScope(FactoryIndexer factoryIndexer, Service.Service service)
{
FactoryIndexer = factoryIndexer;
Service = service;
2020-07-29 21:57:58 +00:00
}
2020-08-31 08:18:07 +00:00
public void Deconstruct(out FactoryIndexer factoryIndexer, out Service.Service service)
{
factoryIndexer = FactoryIndexer;
service = Service;
}
2020-04-23 09:38:50 +00:00
}
public static class ServiceLauncherExtension
{
public static DIHelper AddServiceLauncher(this DIHelper services)
{
2020-08-24 18:41:06 +00:00
services.TryAddScoped<ServiceLauncherScope>();
2020-04-23 09:38:50 +00:00
2020-10-19 15:53:15 +00:00
return services;
2020-04-23 09:38:50 +00:00
}
}
}