DocSpace-buildtools/common/services/ASC.Notify/NotifyService.cs

111 lines
3.7 KiB
C#
Raw Normal View History

2022-02-21 12:29:16 +00:00
namespace ASC.Notify;
[Singletone]
public class NotifyService : IHostedService
2019-07-29 09:15:48 +00:00
{
2022-02-16 13:14:31 +00:00
private readonly DbWorker _db;
2022-02-21 12:29:16 +00:00
private readonly ICacheNotify<NotifyInvoke> _cacheInvoke;
private readonly ICacheNotify<NotifyMessage> _cacheNotify;
private readonly ILog _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
2022-02-21 12:29:16 +00:00
private readonly NotifyConfiguration _notifyConfiguration;
private readonly NotifyServiceCfg _notifyServiceCfg;
public NotifyService(
IOptions<NotifyServiceCfg> notifyServiceCfg,
DbWorker db,
ICacheNotify<NotifyInvoke> cacheInvoke,
ICacheNotify<NotifyMessage> cacheNotify,
IOptionsMonitor<ILog> options,
IServiceScopeFactory serviceScopeFactory,
NotifyConfiguration notifyConfiguration)
2019-07-29 09:15:48 +00:00
{
2022-02-21 12:29:16 +00:00
_cacheInvoke = cacheInvoke;
_cacheNotify = cacheNotify;
2022-02-16 13:14:31 +00:00
_db = db;
2022-02-21 12:29:16 +00:00
_logger = options.Get("ASC.NotifyService");
_notifyConfiguration = notifyConfiguration;
_notifyServiceCfg = notifyServiceCfg.Value;
_serviceScopeFactory = serviceScopeFactory;
2022-02-16 13:14:31 +00:00
}
2019-09-13 11:18:27 +00:00
2022-02-21 12:29:16 +00:00
public Task StartAsync(CancellationToken cancellationToken)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:29:16 +00:00
_logger.Info("Notify Service running.");
_cacheNotify.Subscribe((n) => SendNotifyMessage(n), CacheNotifyAction.InsertOrUpdate);
_cacheInvoke.Subscribe((n) => InvokeSendMethod(n), CacheNotifyAction.InsertOrUpdate);
2022-02-21 12:29:16 +00:00
if (0 < _notifyServiceCfg.Schedulers.Count)
{
InitializeNotifySchedulers();
}
return Task.CompletedTask;
2022-02-16 13:14:31 +00:00
}
2019-08-01 08:47:15 +00:00
2022-02-21 12:29:16 +00:00
public Task StopAsync(CancellationToken cancellationToken)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:29:16 +00:00
_logger.Info("Notify Service is stopping.");
_cacheNotify.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
2022-02-21 12:29:16 +00:00
_cacheInvoke.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
return Task.CompletedTask;
2022-02-16 13:14:31 +00:00
}
2019-08-01 08:47:15 +00:00
2022-02-21 12:29:16 +00:00
private void SendNotifyMessage(NotifyMessage notifyMessage)
2022-02-16 13:14:31 +00:00
{
try
2019-08-01 08:47:15 +00:00
{
2022-02-16 13:14:31 +00:00
_db.SaveMessage(notifyMessage);
2019-07-29 09:15:48 +00:00
}
2022-02-16 13:14:31 +00:00
catch (Exception e)
2019-07-29 09:15:48 +00:00
{
2022-02-16 13:14:31 +00:00
_logger.Error(e);
2019-07-29 09:15:48 +00:00
}
2022-02-16 13:14:31 +00:00
}
2022-02-21 12:29:16 +00:00
private void InvokeSendMethod(NotifyInvoke notifyInvoke)
2022-02-16 13:14:31 +00:00
{
var service = notifyInvoke.Service;
var method = notifyInvoke.Method;
var tenant = notifyInvoke.Tenant;
var parameters = notifyInvoke.Parameters;
var serviceType = Type.GetType(service, true);
2020-09-16 10:50:53 +00:00
using var scope = _serviceScopeFactory.CreateScope();
2022-02-16 13:14:31 +00:00
var instance = scope.ServiceProvider.GetService(serviceType);
if (instance == null)
2019-07-29 09:15:48 +00:00
{
2022-02-16 13:14:31 +00:00
throw new Exception("Service instance not found.");
2019-07-29 09:15:48 +00:00
}
2019-08-01 08:47:15 +00:00
2022-02-16 13:14:31 +00:00
var methodInfo = serviceType.GetMethod(method);
if (methodInfo == null)
2019-08-01 08:47:15 +00:00
{
2022-02-16 13:14:31 +00:00
throw new Exception("Method not found.");
2019-08-01 08:47:15 +00:00
}
2022-02-16 13:14:31 +00:00
2022-02-21 12:29:16 +00:00
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var tenantWhiteLabelSettingsHelper = scope.ServiceProvider.GetService<TenantWhiteLabelSettingsHelper>();
var settingsManager = scope.ServiceProvider.GetService<SettingsManager>();
2022-02-16 13:14:31 +00:00
tenantManager.SetCurrentTenant(tenant);
tenantWhiteLabelSettingsHelper.Apply(settingsManager.Load<TenantWhiteLabelSettings>(), tenant);
methodInfo.Invoke(instance, parameters.ToArray());
2019-07-29 09:15:48 +00:00
}
2019-11-01 08:49:08 +00:00
2022-02-21 12:29:16 +00:00
private void InitializeNotifySchedulers()
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:29:16 +00:00
_notifyConfiguration.Configure();
foreach (var pair in _notifyServiceCfg.Schedulers.Where(r => r.MethodInfo != null))
{
_logger.DebugFormat("Start scheduler {0} ({1})", pair.Name, pair.MethodInfo);
pair.MethodInfo.Invoke(null, null);
}
2019-11-01 08:49:08 +00:00
}
2019-07-29 09:15:48 +00:00
}