Fixed loading products, lazy loading webitemmanager

This commit is contained in:
pavelbannov 2021-01-26 12:29:50 +03:00
parent 64655a7da0
commit f0e227731f
21 changed files with 137 additions and 150 deletions

View File

@ -5,7 +5,10 @@ using ASC.Api.Core.Core;
using ASC.Api.Core.Middleware;
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.Logging;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using Autofac;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
@ -74,6 +77,8 @@ namespace ASC.Api.Core
DIHelper.TryAdd<ConfirmAuthHandler>();
DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
DIHelper.RegisterProducts(Configuration, HostEnvironment.ContentRootPath);
var builder = services.AddMvcCore(config =>
{
@ -128,6 +133,11 @@ namespace ASC.Api.Core
endpoints.MapControllers();
endpoints.MapCustom();
});
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration);
}
}
}

View File

@ -3,11 +3,13 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ASC.Common.DependencyInjection;
using ASC.Common.Threading.Progress;
using ASC.Common.Threading.Workers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
@ -142,6 +144,16 @@ namespace ASC.Common
ServiceCollection = serviceCollection;
}
public void RegisterProducts(IConfiguration configuration, string path)
{
var types = AutofacExtension.FindAndLoad(configuration, path);
foreach (var t in types)
{
TryAdd(Type.GetType(t));
}
}
public bool TryAdd<TService>() where TService : class
{
return TryAdd(typeof(TService));

View File

@ -23,28 +23,8 @@ namespace ASC.Common.DependencyInjection
public static class AutofacExtension
{
public static void Register(this ContainerBuilder builder, IConfiguration configuration, string currentDir, bool loadproducts = true, bool loadconsumers = true, params string[] intern)
public static void Register(this ContainerBuilder builder, IConfiguration configuration, bool loadproducts = true, bool loadconsumers = true, params string[] intern)
{
var folder = configuration["core:products:folder"];
var subfolder = configuration["core:products:subfolder"];
string productsDir;
if (!Path.IsPathRooted(folder))
{
if (currentDir.EndsWith(Path.Combine(Path.GetFileName(folder), Assembly.GetEntryAssembly().GetName().Name, subfolder)))
{
productsDir = Path.GetFullPath(Path.Combine("..", ".."));
}
else
{
productsDir = Path.GetFullPath(Path.Combine(currentDir, folder));
}
}
else
{
productsDir = folder;
}
var modules = new List<(bool, string)>
{
(true, "autofac.json")
@ -77,43 +57,64 @@ namespace ASC.Common.DependencyInjection
var root = config.Build();
var module = new ConfigurationModule(root);
builder.RegisterModule(module);
if (p.Item2 == "autofac.products.json")
{
FindAndLoad(root.GetSection("components"));
}
}
return;
}
void FindAndLoad(IConfigurationSection sectionSettings)
public static List<string> FindAndLoad(IConfiguration configuration, string currentDir, string section = "autofac.products.json")
{
var config = new ConfigurationBuilder();
config.SetBasePath(configuration["pathToConf"]);
config.AddJsonFile(section);
var root = config.Build();
var sectionSettings = root.GetSection("components");
if (sectionSettings == null)
{
if (sectionSettings == null)
return new List<string>();
}
var folder = configuration["core:products:folder"];
var subfolder = configuration["core:products:subfolder"];
string productsDir;
if (!Path.IsPathRooted(folder))
{
if (currentDir.EndsWith(Path.Combine(Path.GetFileName(folder), Assembly.GetEntryAssembly().GetName().Name, subfolder)))
{
return;
productsDir = Path.GetFullPath(Path.Combine("..", ".."));
}
var cs = new List<AutofacComponent>();
sectionSettings.Bind(cs);
foreach (var component in cs)
else
{
try
{
var types = new List<string>();
LoadAssembly(component.Type);
foreach (var s in component.Services)
{
//LoadAssembly(s.Type);
types.Add(s.Type);
}
}
catch (System.Exception)
{
//TODO
}
productsDir = Path.GetFullPath(Path.Combine(currentDir, folder));
}
}
else
{
productsDir = folder;
}
var cs = new List<AutofacComponent>();
sectionSettings.Bind(cs);
var types = new List<string>();
foreach (var component in cs)
{
try
{
LoadAssembly(component.Type);
types.Add(component.Type);
}
catch (System.Exception)
{
//TODO
}
}
return types;
void LoadAssembly(string type)
{
@ -139,6 +140,8 @@ namespace ASC.Common.DependencyInjection
return Directory.GetFiles(dirPath, $"{dll}.dll", searchOption).FirstOrDefault();
}
}
}
class Resolver

View File

@ -121,7 +121,7 @@ namespace ASC.ApiSystem
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath, false);
builder.Register(Configuration, false);
}
}
}

View File

@ -2,13 +2,10 @@
using ASC.Api.Core;
using ASC.Common;
using ASC.Common.DependencyInjection;
using ASC.Data.Backup.Controllers;
using ASC.Data.Backup.Service;
using ASC.Web.Studio.Core.Notify;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -36,10 +33,5 @@ namespace ASC.Data.Backup
services.AddHostedService<BackupServiceLauncher>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
}
}

View File

@ -25,9 +25,6 @@
using ASC.Api.Core;
using ASC.Common;
using ASC.Common.DependencyInjection;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -51,11 +48,6 @@ namespace ASC.Data.Storage.Encryption
DIHelper.TryAdd<EncryptionServiceLauncher>();
services.AddHostedService<EncryptionServiceLauncher>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
}
}

View File

@ -54,13 +54,15 @@ namespace ASC.Data.Storage.Migration
var diHelper = new DIHelper(services);
LogNLogExtension.ConfigureLog(diHelper, "ASC.Data.Storage.Migration", "ASC.Migration");
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
diHelper.TryAdd<MigrationServiceLauncher>();
services.AddHostedService<MigrationServiceLauncher>();
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath);
builder.Register(context.Configuration);
})
.UseConsoleLifetime()
.Build()

View File

@ -107,7 +107,6 @@ namespace ASC.Notify
private void InitializeNotifySchedulers()
{
NotifyConfiguration.Configure();
WebItemManager.LoadItems();
foreach (var pair in NotifyServiceCfg.Schedulers.Where(r => r.MethodInfo != null))
{
Log.DebugFormat("Start scheduler {0} ({1})", pair.Name, pair.MethodInfo);

View File

@ -57,6 +57,7 @@ namespace ASC.Notify
LogNLogExtension.ConfigureLog(diHelper, "ASC.Notify", "ASC.Notify.Messages");
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
services.Configure<NotifyServiceCfg>(hostContext.Configuration.GetSection("notify"));
@ -70,7 +71,7 @@ namespace ASC.Notify
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath);
builder.Register(context.Configuration);
})
.UseConsoleLifetime()
.Build();

View File

@ -80,13 +80,15 @@ namespace ASC.Socket.IO.Svc
services.AddMemoryCache();
var diHelper = new DIHelper(services);
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
LogNLogExtension.ConfigureLog(diHelper, "ASC.Socket.IO.Svc");
services.AddHostedService<SocketServiceLauncher>();
diHelper.TryAdd<SocketServiceLauncher>();
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath, false, false);
builder.Register(context.Configuration, false, false);
})
.UseConsoleLifetime()
.Build();

View File

@ -56,6 +56,7 @@ namespace ASC.Studio.Notify
services.AddMemoryCache();
var diHelper = new DIHelper(services);
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
LogNLogExtension.ConfigureLog(diHelper, "ASC.Notify", "ASC.Notify.Messages");
services.AddHostedService<ServiceLauncher>();
diHelper.TryAdd<ServiceLauncher>();
@ -64,7 +65,7 @@ namespace ASC.Studio.Notify
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath);
builder.Register(context.Configuration);
})
.UseConsoleLifetime()
.Build();

View File

@ -55,7 +55,6 @@ namespace ASC.Notify
public Task StartAsync(CancellationToken cancellationToken)
{
NotifyConfiguration.Configure();
WebItemManager.LoadItems();
StudioNotifyServiceSender.RegisterSendMethod();

View File

@ -25,9 +25,6 @@
using ASC.Api.Core;
using ASC.Common;
using ASC.Common.DependencyInjection;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -51,11 +48,6 @@ namespace ASC.TelegramService
DIHelper.TryAdd<TelegramLauncher>();
services.AddHostedService<TelegramLauncher>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
}
}

View File

@ -87,7 +87,7 @@ namespace ASC.Thumbnails.Svc
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath, false, false);
builder.Register(context.Configuration, false, false);
})
.UseConsoleLifetime()
.Build();

View File

@ -83,7 +83,7 @@ namespace ASC.UrlShortener.Svc
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath, false, false);
builder.Register(context.Configuration, false, false);
})
.UseConsoleLifetime()
.Build();

View File

@ -3,13 +3,10 @@ using System.Text.Json.Serialization;
using ASC.Api.Core;
using ASC.Api.Documents;
using ASC.Common.DependencyInjection;
using ASC.Web.Files;
using ASC.Web.Files.HttpHandlers;
using ASC.Web.Studio.Core.Notify;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@ -85,10 +82,5 @@ namespace ASC.Files
appBranch.UseDocuSignHandler();
});
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
}
}

View File

@ -58,6 +58,8 @@ namespace ASC.Files.Service
var diHelper = new DIHelper(services);
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
services.AddHostedService<ServiceLauncher>();
diHelper.TryAdd<ServiceLauncher>();
@ -71,7 +73,7 @@ namespace ASC.Files.Service
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, context.HostingEnvironment.ContentRootPath, true, false, "search.json", "feed.json");
builder.Register(context.Configuration, true, false, "search.json", "feed.json");
})
.UseConsoleLifetime()
.Build();

View File

@ -1,11 +1,8 @@

using ASC.Api.Core;
using ASC.Common;
using ASC.Common.DependencyInjection;
using ASC.Employee.Core.Controllers;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -28,10 +25,5 @@ namespace ASC.People
DIHelper.TryAdd<PeopleController>();
DIHelper.TryAdd<GroupController>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
}
}

View File

@ -1,11 +1,8 @@

using ASC.Api.Core;
using ASC.Api.Settings;
using ASC.Common.DependencyInjection;
using ASC.Web.Api.Controllers;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -36,10 +33,5 @@ namespace ASC.Web.Api
DIHelper.TryAdd<SmtpSettingsController>();
DIHelper.TryAdd<ThirdPartyController>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
}
}

View File

@ -25,6 +25,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
@ -53,7 +54,20 @@ namespace ASC.Web.Core
{
private readonly ILog log;
private readonly Dictionary<Guid, IWebItem> items = new Dictionary<Guid, IWebItem>();
private ConcurrentDictionary<Guid, IWebItem> items;
private ConcurrentDictionary<Guid, IWebItem> Items
{
get
{
if (lazyItems.IsValueCreated)
{
return items;
}
return items = lazyItems.Value;
}
}
private readonly Lazy<ConcurrentDictionary<Guid, IWebItem>> lazyItems;
private readonly List<string> disableItem;
public static Guid CommunityProductID
@ -106,14 +120,15 @@ namespace ASC.Web.Core
get { return new Guid("{46CFA73A-F320-46CF-8D5B-CD82E1D67F26}"); }
}
public ILifetimeScope Container { get; }
private ILifetimeScope Container { get; }
private IConfiguration Configuration { get; }
public IWebItem this[Guid id]
{
get
{
items.TryGetValue(id, out var i);
Items.TryGetValue(id, out var i);
return i;
}
}
@ -124,12 +139,14 @@ namespace ASC.Web.Core
Configuration = configuration;
log = options.Get("ASC.Web");
disableItem = (Configuration["web:disabled-items"] ?? "").Split(",").ToList();
LoadItems();
lazyItems = new Lazy<ConcurrentDictionary<Guid, IWebItem>>(LoadItems, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
}
public void LoadItems()
private ConcurrentDictionary<Guid, IWebItem> LoadItems()
{
if (Container == null) return;
var result = new ConcurrentDictionary<Guid, IWebItem>();
if (Container == null) return result;
foreach (var webitem in Container.Resolve<IEnumerable<IWebItem>>())
{
@ -138,46 +155,41 @@ namespace ASC.Web.Core
{
if (DisabledWebItem(file)) continue;
if (RegistryItem(webitem))
{
log.DebugFormat("Web item {0} loaded", webitem.Name);
}
RegistryItem(result, webitem);
}
catch (Exception exc)
{
log.Error(string.Format("Couldn't load web item {0}", file), exc);
}
}
return result;
}
public bool RegistryItem(IWebItem webitem)
private void RegistryItem(ConcurrentDictionary<Guid, IWebItem> result, IWebItem webitem)
{
lock (items)
if (webitem != null && !result.TryGetValue(webitem.ID, out _))
{
if (webitem != null && this[webitem.ID] == null)
if (webitem is IAddon addon)
{
if (webitem is IAddon addon)
{
addon.Init();
}
if (webitem is IProduct product)
{
product.Init();
}
if (webitem is IModule module)
{
if (module.Context != null && module.Context.SearchHandler != null)
{
//TODO
//SearchHandlerManager.Registry(module.Context.SearchHandler);
}
}
items.Add(webitem.ID, webitem);
return true;
addon.Init();
}
return false;
if (webitem is IProduct product)
{
product.Init();
}
if (webitem is IModule module)
{
if (module.Context != null && module.Context.SearchHandler != null)
{
//TODO
//SearchHandlerManager.Registry(module.Context.SearchHandler);
}
}
result.TryAdd(webitem.ID, webitem);
log.DebugFormat("Web item {0} loaded", webitem.Name);
}
}
@ -193,7 +205,7 @@ namespace ASC.Web.Core
public List<IWebItem> GetItemsAll()
{
var list = items.Values.ToList();
var list = Items.Values.ToList();
list.Sort((x, y) => GetSortOrder(x).CompareTo(GetSortOrder(y)));
return list;
}

View File

@ -1,12 +1,9 @@
using ASC.Api.Core;
using ASC.Common.DependencyInjection;
using ASC.Data.Storage;
using ASC.Data.Storage.DiscStorage;
using ASC.FederatedLogin;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
@ -36,11 +33,6 @@ namespace ASC.Web.Studio
DIHelper.TryAdd<Login>();
DIHelper.TryAdd<PathUtils>();
DIHelper.TryAdd<StorageHandlerScope>();
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.Register(Configuration, HostEnvironment.ContentRootPath);
}
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)