Merge branch 'feature/backend-refactor' into feature/people-refactor

This commit is contained in:
pavelbannov 2022-03-03 11:22:16 +03:00
commit 5f68d00182
6 changed files with 160 additions and 137 deletions

View File

@ -83,14 +83,14 @@ public abstract class DIAttribute : Attribute
public Type Service { get; }
public Type Additional { get; set; }
protected DIAttribute() { }
protected DIAttribute() { }
protected DIAttribute(Type service)
protected DIAttribute(Type service)
{
Service = service;
}
protected DIAttribute(Type service, Type implementation)
protected DIAttribute(Type service, Type implementation)
{
Implementation = implementation;
Service = service;
@ -139,6 +139,14 @@ public class DIHelper
}
}
public void AddControllers()
{
foreach (var a in Assembly.GetCallingAssembly().GetTypes().Where(r => r.IsAssignableTo<ControllerBase>()))
{
_ = TryAdd(a);
}
}
public bool TryAdd<TService>() where TService : class
{
return TryAdd(typeof(TService));
@ -232,7 +240,7 @@ public class DIHelper
}
else
{
Type c;
Type c;
var a1 = a.GetGenericTypeDefinition();
var b = a.GetGenericArguments().FirstOrDefault();
@ -295,7 +303,7 @@ public class DIHelper
}
else
{
Type c;
Type c;
var a1 = a.GetGenericTypeDefinition();
var b = a.GetGenericArguments().FirstOrDefault();

View File

@ -74,7 +74,7 @@ public static class AutofacExtension
if (!Path.IsPathRooted(folder))
{
if (currentDir.EndsWith(CrossPlatform.PathCombine(Path.GetFileName(folder), Assembly.GetEntryAssembly().GetName().Name, subfolder)))
if (currentDir.TrimEnd('\\').EndsWith(CrossPlatform.PathCombine(Path.GetFileName(folder), Assembly.GetEntryAssembly().GetName().Name, subfolder)))
{
productsDir = Path.GetFullPath(CrossPlatform.PathCombine("..", ".."));
}
@ -110,7 +110,7 @@ public static class AutofacExtension
void LoadAssembly(string type)
{
var dll = type.Substring(type.IndexOf(',') + 1).Trim();
var dll = type.Substring(type.IndexOf(',') + 1).Trim();
var path = GetFullPath(dll);
if (!string.IsNullOrEmpty(path))

View File

@ -187,16 +187,16 @@ public class FeedAggregateDataProvider
return feeds.Take(filterLimit).SelectMany(group => group.Value).ToList();
}
private List<FeedResultItem> GetFeedsInternal(FeedApiFilter filter)
{
var q = FeedDbContext.FeedAggregates
.Where(r => r.Tenant == _tenantManager.GetCurrentTenant().Id)
.Where(r => r.ModifiedBy != _authContext.CurrentAccount.ID)
.Join(FeedDbContext.FeedUsers, a => a.Id, b => b.FeedId, (aggregates, users) => new { aggregates, users })
.Where(r => r.users.UserId == _authContext.CurrentAccount.ID)
.OrderByDescending(r => r.aggregates.ModifiedDate)
.Skip(filter.Offset)
.Take(filter.Max);
private List<FeedResultItem> GetFeedsInternal(FeedApiFilter filter)
{
var q = FeedDbContext.FeedAggregates
.Where(r => r.Tenant == _tenantManager.GetCurrentTenant().Id)
.Where(r => r.ModifiedBy != _authContext.CurrentAccount.ID)
.Join(FeedDbContext.FeedUsers, a => a.Id, b => b.FeedId, (aggregates, users) => new { aggregates, users })
.Where(r => r.users.UserId == _authContext.CurrentAccount.ID)
.OrderByDescending(r => r.aggregates.ModifiedDate)
.Skip(filter.Offset)
.Take(filter.Max);
if (filter.OnlyNew)
{
@ -239,13 +239,13 @@ public class FeedAggregateDataProvider
return _mapper.Map<IEnumerable<FeedAggregate>, List<FeedResultItem>>(news);
}
public int GetNewFeedsCount(DateTime lastReadedTime, AuthContext authContext, TenantManager tenantManager)
{
var count = FeedDbContext.FeedAggregates
.Where(r => r.Tenant == tenantManager.GetCurrentTenant().Id)
.Where(r => r.ModifiedBy != authContext.CurrentAccount.ID)
.Join(FeedDbContext.FeedUsers, r => r.Id, u => u.FeedId, (agg, user) => new { agg, user })
.Where(r => r.user.UserId == authContext.CurrentAccount.ID);
public int GetNewFeedsCount(DateTime lastReadedTime, AuthContext authContext, TenantManager tenantManager)
{
var count = FeedDbContext.FeedAggregates
.Where(r => r.Tenant == tenantManager.GetCurrentTenant().Id)
.Where(r => r.ModifiedBy != authContext.CurrentAccount.ID)
.Join(FeedDbContext.FeedUsers, r => r.Id, u => u.FeedId, (agg, user) => new { agg, user })
.Where(r => r.user.UserId == authContext.CurrentAccount.ID);
if (1 < lastReadedTime.Year)
{
@ -303,6 +303,8 @@ public class FeedResultItem : IMapFrom<FeedAggregate>
public DateTime ModifiedDate { get; private set; }
public DateTime AggregatedDate { get; private set; }
public FeedResultItem() { }
public FeedResultItem(
string json,
string module,

View File

@ -1,25 +1,28 @@
global using System.Collections.Generic;
global using System.IO;
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Reflection;
global using System.Runtime.InteropServices;
global using System.Threading;
global using System.Threading.Tasks;
global using ASC.Api.Core;
global using ASC.Common;
global using ASC.Common.Caching;
global using ASC.Common.DependencyInjection;
global using ASC.Common.Caching;
global using ASC.Common.Mapping;
global using ASC.Common.Utils;
global using ASC.Core.Notify;
global using ASC.Notify;
global using ASC.Web.Core;
global using ASC.Web.Studio.Core.Notify;
global using Autofac;
global using Autofac.Extensions.DependencyInjection;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Hosting.WindowsServices;
global using StackExchange.Redis.Extensions.Core.Configuration;
global using StackExchange.Redis.Extensions.Newtonsoft;

View File

@ -1,85 +1,105 @@
namespace ASC.Studio.Notify
var options = new WebApplicationOptions
{
public static class Program
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(options);
builder.Host.UseWindowsService();
builder.Host.UseSystemd();
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.WebHost.ConfigureKestrel((hostingContext, serverOptions) =>
{
var kestrelConfig = hostingContext.Configuration.GetSection("Kestrel");
if (!kestrelConfig.Exists()) return;
var unixSocket = kestrelConfig.GetValue<string>("ListenUnixSocket");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
public async static Task Main(string[] args)
if (!string.IsNullOrWhiteSpace(unixSocket))
{
var host = CreateHostBuilder(args).Build();
unixSocket = string.Format(unixSocket, hostingContext.HostingEnvironment.ApplicationName.Replace("ASC.", "").Replace(".", ""));
await host.RunAsync();
serverOptions.ListenUnixSocket(unixSocket);
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSystemd()
.UseWindowsService()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<BaseWorkerStartup>())
.ConfigureAppConfiguration((hostContext, config) =>
{
var buided = config.Build();
var path = buided["pathToConf"];
if (!Path.IsPathRooted(path))
{
path = Path.GetFullPath(CrossPlatform.PathCombine(hostContext.HostingEnvironment.ContentRootPath, path));
}
config.SetBasePath(path);
var env = hostContext.Configuration.GetValue("ENVIRONMENT", "Production");
config
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env}.json", true)
.AddJsonFile($"appsettings.services.json", true)
.AddJsonFile("storage.json")
.AddJsonFile("notify.json")
.AddJsonFile($"notify.{env}.json", true)
.AddJsonFile("kafka.json")
.AddJsonFile($"kafka.{env}.json", true)
.AddJsonFile("redis.json")
.AddJsonFile($"redis.{env}.json", true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddInMemoryCollection(new Dictionary<string, string>
{
{"pathToConf", path }
}
);
})
.ConfigureServices((hostContext, services) =>
{
services.AddMemoryCache();
services.AddHttpClient();
var diHelper = new DIHelper(services);
var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = hostContext.Configuration.GetSection("kafka").Get<KafkaSettings>();
if (kafkaConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCacheNotify<>));
}
else if (redisConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCacheNotify<>));
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
}
else
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
}
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
services.AddHostedService<ServiceLauncher>();
diHelper.TryAdd<ServiceLauncher>();
NotifyConfigurationExtension.Register(diHelper);
diHelper.TryAdd<EmailSenderSink>();
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration);
})
.ConfigureNLogLogging();
}
}
});
builder.Host.ConfigureAppConfiguration((hostContext, config) =>
{
var buided = config.Build();
var path = buided["pathToConf"];
if (!Path.IsPathRooted(path))
{
path = Path.GetFullPath(CrossPlatform.PathCombine(hostContext.HostingEnvironment.ContentRootPath, path));
}
config.SetBasePath(path);
var env = hostContext.Configuration.GetValue("ENVIRONMENT", "Production");
config
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env}.json", true)
.AddJsonFile($"appsettings.services.json", true)
.AddJsonFile("storage.json")
.AddJsonFile("notify.json")
.AddJsonFile($"notify.{env}.json", true)
.AddJsonFile("kafka.json")
.AddJsonFile($"kafka.{env}.json", true)
.AddJsonFile("redis.json")
.AddJsonFile($"redis.{env}.json", true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddInMemoryCollection(new Dictionary<string, string>
{
{"pathToConf", path }
}
);
});
builder.Host.ConfigureServices((hostContext, services) =>
{
services.AddMemoryCache();
services.AddHttpClient();
var diHelper = new DIHelper(services);
var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = hostContext.Configuration.GetSection("kafka").Get<KafkaSettings>();
if (kafkaConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCacheNotify<>));
}
else if (redisConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCacheNotify<>));
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
}
else
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
}
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
services.AddHostedService<ServiceLauncher>();
diHelper.TryAdd<ServiceLauncher>();
NotifyConfigurationExtension.Register(diHelper);
diHelper.TryAdd<EmailSenderSink>();
services.AddAutoMapper(Assembly.GetAssembly(typeof(MappingProfile)));
});
builder.Host.ConfigureNLogLogging();
var startup = new BaseWorkerStartup(builder.Configuration);
startup.ConfigureServices(builder.Services);
var app = builder.Build();
startup.Configure(app);
app.Run();

View File

@ -23,37 +23,27 @@
*
*/
namespace ASC.Notify
namespace ASC.Notify;
[Singletone]
public class ServiceLauncher : BackgroundService
{
[Singletone]
public class ServiceLauncher : IHostedService
private readonly StudioNotifyServiceSender _studioNotifyServiceSender;
private readonly NotifyConfiguration _notifyConfiguration;
public ServiceLauncher(
StudioNotifyServiceSender studioNotifyServiceSender,
NotifyConfiguration notifyConfiguration)
{
private WebItemManager WebItemManager { get; }
private StudioNotifyServiceSender StudioNotifyServiceSender { get; }
private NotifyConfiguration NotifyConfiguration { get; }
_studioNotifyServiceSender = studioNotifyServiceSender;
_notifyConfiguration = notifyConfiguration;
}
public ServiceLauncher(
WebItemManager webItemManager,
StudioNotifyServiceSender studioNotifyServiceSender,
NotifyConfiguration notifyConfiguration)
{
WebItemManager = webItemManager;
StudioNotifyServiceSender = studioNotifyServiceSender;
NotifyConfiguration = notifyConfiguration;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_notifyConfiguration.Configure();
_studioNotifyServiceSender.RegisterSendMethod();
public Task StartAsync(CancellationToken cancellationToken)
{
NotifyConfiguration.Configure();
StudioNotifyServiceSender.RegisterSendMethod();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}