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