namespace Adnc.Shared.Application.IdGenerater; using Easy.Realization; using Easy.Snowflakes; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; public class WorkerNodeHostedService : BackgroundService { private readonly ILogger _logger; private readonly string _serviceName; private readonly WorkerNode _workerNode; private readonly int _millisecondsDelay = 1000 * 60; public WorkerNodeHostedService(ILogger logger , WorkerNode workerNode , SnowflakeOptions options) { _serviceName = options.ServiceName; _workerNode = workerNode; _logger = logger; } public override async Task StartAsync(CancellationToken cancellationToken) { await _workerNode.InitWorkerNodesAsync(_serviceName); var workerId = await _workerNode.GetWorkerIdAsync(_serviceName); DriftingSnowflakeIdGenerator.SetWorkerId((ushort)workerId); await base.StartAsync(cancellationToken); } public override async Task StopAsync(CancellationToken cancellationToken) { await base.StopAsync(cancellationToken); _logger.LogInformation("stopping service {0}", _serviceName); var subtractionMilliseconds = 0 - (_millisecondsDelay * 1.5); var score = new DateTimeOffset(DateTime.Now.AddMilliseconds(subtractionMilliseconds)).ToUnixTimeMilliseconds(); await _workerNode.RefreshWorkerIdScoreAsync(_serviceName, DriftingSnowflakeIdGenerator.CurrentWorkerId, score); _logger.LogInformation("stopped service {0}:{1}", _serviceName, score); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { await Task.Delay(_millisecondsDelay, stoppingToken); if (stoppingToken.IsCancellationRequested) break; await _workerNode.RefreshWorkerIdScoreAsync(_serviceName, DriftingSnowflakeIdGenerator.CurrentWorkerId); } catch (Exception ex) { _logger.LogError(ex.Message, ex); await Task.Delay(_millisecondsDelay / 3, stoppingToken); } } } }