| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace NLightning.Daemon.Services; |
| | | 7 | | |
| | | 8 | | using Domain.Bitcoin.Interfaces; |
| | | 9 | | using Domain.Client.Interfaces; |
| | | 10 | | using Domain.Node.Interfaces; |
| | | 11 | | using Domain.Node.Options; |
| | | 12 | | using Domain.Protocol.Interfaces; |
| | | 13 | | using Infrastructure.Bitcoin.Wallet.Interfaces; |
| | | 14 | | |
| | | 15 | | public class NltgDaemonService : BackgroundService |
| | | 16 | | { |
| | | 17 | | private readonly IBlockchainMonitor _blockchainMonitor; |
| | | 18 | | private readonly IConfiguration _configuration; |
| | | 19 | | private readonly IFeeService _feeService; |
| | | 20 | | private readonly ILogger<NltgDaemonService> _logger; |
| | | 21 | | private readonly INamedPipeIpcService _namedPipeIpcService; |
| | | 22 | | private readonly IPeerManager _peerManager; |
| | | 23 | | private readonly NodeOptions _nodeOptions; |
| | | 24 | | private readonly ISecureKeyManager _secureKeyManager; |
| | | 25 | | |
| | 0 | 26 | | public NltgDaemonService(IBlockchainMonitor blockchainMonitor, IConfiguration configuration, IFeeService feeService, |
| | 0 | 27 | | ILogger<NltgDaemonService> logger, INamedPipeIpcService namedPipeIpcService, |
| | 0 | 28 | | IOptions<NodeOptions> nodeOptions, IPeerManager peerManager, |
| | 0 | 29 | | ISecureKeyManager secureKeyManager) |
| | | 30 | | { |
| | 0 | 31 | | _blockchainMonitor = blockchainMonitor; |
| | 0 | 32 | | _configuration = configuration; |
| | 0 | 33 | | _feeService = feeService; |
| | 0 | 34 | | _logger = logger; |
| | 0 | 35 | | _namedPipeIpcService = namedPipeIpcService; |
| | 0 | 36 | | _peerManager = peerManager; |
| | 0 | 37 | | _nodeOptions = nodeOptions.Value; |
| | 0 | 38 | | _secureKeyManager = secureKeyManager; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 42 | | { |
| | 0 | 43 | | var network = _configuration["network"] ?? _configuration["n"] ?? _nodeOptions.BitcoinNetwork; |
| | 0 | 44 | | var isDaemon = _configuration.GetValue<bool?>("daemon") |
| | 0 | 45 | | ?? _configuration.GetValue<bool?>("daemon-child") |
| | 0 | 46 | | ?? _nodeOptions.Daemon; |
| | | 47 | | |
| | 0 | 48 | | if (_logger.IsEnabled(LogLevel.Information)) |
| | 0 | 49 | | _logger.LogInformation("NLTG Daemon started on {Network} network", network); |
| | | 50 | | |
| | 0 | 51 | | if (_logger.IsEnabled(LogLevel.Debug)) |
| | | 52 | | { |
| | 0 | 53 | | _logger.LogDebug("Running in daemon mode: {IsDaemon}", isDaemon); |
| | | 54 | | |
| | 0 | 55 | | var pubKey = _secureKeyManager.GetNodePubKey(); |
| | 0 | 56 | | _logger.LogDebug("Our PubKey is {pubKey}", pubKey.ToString()); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | try |
| | | 60 | | { |
| | | 61 | | // Start the fee service |
| | 0 | 62 | | await _feeService.StartAsync(stoppingToken); |
| | | 63 | | |
| | | 64 | | // Start the peer manager service |
| | 0 | 65 | | await _peerManager.StartAsync(stoppingToken); |
| | | 66 | | |
| | | 67 | | // Start the blockchain monitor service |
| | 0 | 68 | | await _blockchainMonitor.StartAsync(_secureKeyManager.HeightOfBirth, stoppingToken); |
| | | 69 | | |
| | | 70 | | // Start the IPC server |
| | 0 | 71 | | await _namedPipeIpcService.StartAsync(stoppingToken); |
| | | 72 | | |
| | 0 | 73 | | while (!stoppingToken.IsCancellationRequested) |
| | 0 | 74 | | await Task.Delay(1000, stoppingToken); |
| | 0 | 75 | | } |
| | 0 | 76 | | catch (OperationCanceledException) |
| | | 77 | | { |
| | 0 | 78 | | _logger.LogInformation("Stopping NLTG daemon service"); |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public override async Task StopAsync(CancellationToken cancellationToken) |
| | | 83 | | { |
| | 0 | 84 | | _logger.LogInformation("NLTG shutdown requested"); |
| | | 85 | | |
| | 0 | 86 | | await Task.WhenAll(_blockchainMonitor.StopAsync(), _feeService.StopAsync(), _peerManager.StopAsync(), |
| | 0 | 87 | | _namedPipeIpcService.StopAsync(), base.StopAsync(cancellationToken)); |
| | | 88 | | |
| | 0 | 89 | | _logger.LogInformation("NLTG daemon service stopped"); |
| | 0 | 90 | | } |
| | | 91 | | } |