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