2022-02-23 12:30:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2022-02-24 14:15:39 +00:00
|
|
|
|
using ASC.Common;
|
2022-02-23 12:30:08 +00:00
|
|
|
|
using ASC.Common.Logging;
|
2022-02-24 14:15:39 +00:00
|
|
|
|
using ASC.Core.Common.Hosting.Interfaces;
|
2022-02-23 12:30:08 +00:00
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
2022-02-24 14:15:39 +00:00
|
|
|
|
namespace ASC.Core.Common.Hosting;
|
2022-02-23 12:30:08 +00:00
|
|
|
|
|
|
|
|
|
[Singletone]
|
2022-02-24 14:15:39 +00:00
|
|
|
|
public class RegisterInstanceWorkerService<T> : BackgroundService where T : IHostedService
|
2022-02-23 12:30:08 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly ILog _logger;
|
2022-02-24 14:15:39 +00:00
|
|
|
|
private IRegisterInstanceManager<T> _registerInstanceService;
|
2022-02-23 12:30:08 +00:00
|
|
|
|
private readonly IHostApplicationLifetime _applicationLifetime;
|
2022-02-24 14:15:39 +00:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
public static readonly string InstanceId =
|
2022-02-23 12:30:08 +00:00
|
|
|
|
$"{typeof(T).Name}{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
|
|
|
|
|
|
2022-02-24 14:15:39 +00:00
|
|
|
|
public RegisterInstanceWorkerService(IOptionsMonitor<ILog> options,
|
2022-02-23 12:30:08 +00:00
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
IHostApplicationLifetime applicationLifetime)
|
|
|
|
|
{
|
|
|
|
|
_logger = options.CurrentValue;
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
_applicationLifetime = applicationLifetime;
|
2022-02-24 14:15:39 +00:00
|
|
|
|
}
|
2022-02-23 12:30:08 +00:00
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _serviceProvider.CreateScope();
|
|
|
|
|
|
2022-02-24 14:15:39 +00:00
|
|
|
|
_registerInstanceService = scope.ServiceProvider.GetService<IRegisterInstanceManager<T>>();
|
|
|
|
|
|
2022-02-23 12:30:08 +00:00
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-24 14:15:39 +00:00
|
|
|
|
await _registerInstanceService.Register(InstanceId);
|
2022-02-23 12:30:08 +00:00
|
|
|
|
await _registerInstanceService.DeleteOrphanInstances();
|
|
|
|
|
|
|
|
|
|
_logger.InfoFormat("Worker running at: {time}", DateTimeOffset.Now);
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1000, stoppingToken);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorFormat("Critical error forced worker to shutdown", ex);
|
|
|
|
|
_applicationLifetime.StopApplication();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-24 14:15:39 +00:00
|
|
|
|
await _registerInstanceService.UnRegister(InstanceId);
|
|
|
|
|
_logger.InfoFormat("UnRegister Instance {instanceName} running at: {time}.", InstanceId, DateTimeOffset.Now);
|
2022-02-23 12:30:08 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2022-02-24 14:15:39 +00:00
|
|
|
|
_logger.ErrorFormat("Unable to UnRegister Instance {instanceName} running at: {time}.", InstanceId, DateTimeOffset.Now);
|
2022-02-23 12:30:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await base.StopAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|