From 24bff221db8dc8121b437dec763dfd31355d6c85 Mon Sep 17 00:00:00 2001 From: Light Date: Fri, 13 Sep 2024 23:16:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BgServices/BaseBackgroundService.cs | 56 ++++--------------- .../BgServices/BaseScheduledService.cs | 48 ++++++++++------ .../BgServices/CollectionTRONService.cs | 2 - .../BgServices/OrderCheckEVMBaseService.cs | 2 - .../BgServices/OrderCheckEVMERC20Service.cs | 2 - .../BgServices/OrderCheckTRC20Service.cs | 4 +- .../BgServices/OrderCheckTRXService.cs | 4 +- .../BgServices/OrderExpiredService.cs | 2 - src/TokenPay/BgServices/OrderNotifyService.cs | 2 - .../BgServices/OrderPaySuccessService.cs | 2 - src/TokenPay/BgServices/UpdateRateService.cs | 2 - src/TokenPay/Program.cs | 4 +- 12 files changed, 47 insertions(+), 83 deletions(-) diff --git a/src/TokenPay/BgServices/BaseBackgroundService.cs b/src/TokenPay/BgServices/BaseBackgroundService.cs index 67d2cd5..99ac20b 100644 --- a/src/TokenPay/BgServices/BaseBackgroundService.cs +++ b/src/TokenPay/BgServices/BaseBackgroundService.cs @@ -3,61 +3,25 @@ namespace TokenPay.BgServices { - public abstract class BaseBackgroundService : IHostedService, IDisposable + public abstract class BaseBackgroundService : BackgroundService { - private Task? _executingTask; - protected readonly string? jobName; - protected readonly ILogger? __logger; - private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource(); - - protected BaseBackgroundService() - { - } + protected readonly string jobName; + protected readonly ILogger _logger; protected BaseBackgroundService(string JobName, ILogger logger) { - __logger = logger; + _logger = logger; jobName = JobName; } - protected abstract Task ExecuteAsync(CancellationToken stoppingToken); - - public virtual Task StartAsync(CancellationToken cancellationToken) - { - if (jobName != null && __logger != null) - __logger.LogInformation("Background Service {JobName} is starting.", jobName); - - _executingTask = ExecuteAsync(_stoppingCts.Token); - - if (_executingTask.IsCompleted) - { - return _executingTask; - } - - return Task.CompletedTask; - } - - public virtual async Task StopAsync(CancellationToken cancellationToken) + public override Task StartAsync(CancellationToken cancellationToken) { - if (_executingTask == null) - { - return; - } - - try - { - if (jobName != null && __logger != null) - __logger.LogInformation("Background Service {JobName} is stopping.", jobName); - _stoppingCts.Cancel(); - } - finally - { - await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken)); - } - + _logger.LogInformation("Background Service {JobName} is starting.", jobName); + return base.StartAsync(cancellationToken); } - public virtual void Dispose() + public override Task StopAsync(CancellationToken cancellationToken) { - _stoppingCts.Cancel(); + _logger.LogInformation("Background Service {JobName} is stopping.", jobName); + return base.StartAsync(cancellationToken); } } diff --git a/src/TokenPay/BgServices/BaseScheduledService.cs b/src/TokenPay/BgServices/BaseScheduledService.cs index 995273a..b90496a 100644 --- a/src/TokenPay/BgServices/BaseScheduledService.cs +++ b/src/TokenPay/BgServices/BaseScheduledService.cs @@ -1,37 +1,53 @@ +using System; + namespace TokenPay.BgServices { public abstract class BaseScheduledService : BackgroundService { protected readonly string jobName; - protected readonly ILogger Logger; - private readonly PeriodicTimer _timer; + private readonly TimeSpan period; + protected readonly ILogger _logger; + private PeriodicTimer? _timer; protected BaseScheduledService(string JobName, TimeSpan period, ILogger logger) { - Logger = logger; + _logger = logger; jobName = JobName; - _timer = new PeriodicTimer(period); + this.period = period; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - Logger.LogInformation("Service {JobName} is starting.", jobName); - do + _logger.LogInformation("Service {JobName} is starting.", jobName); + await Task.Delay(3000);//延迟3秒再启动任务 + _timer = new PeriodicTimer(period); + try { - try + do { - await ExecuteAsync(DateTime.Now, stoppingToken); - } - catch (Exception ex) - { - Logger.LogError(ex, $"定时任务[{jobName}]执行出现错误"); - } - } while (!stoppingToken.IsCancellationRequested && await _timer.WaitForNextTickAsync(stoppingToken)); + try + { + await ExecuteAsync(DateTime.Now, stoppingToken); + } + catch (Flurl.Http.FlurlHttpException ex) when (ex.StatusCode == 401 || ex.StatusCode == 403) + { + _logger.LogError(ex, "定时任务[{jobName}]执行Api请求出现错误,返回:{code},通常为Api鉴权出现问题或者调用次数超出限制", jobName, ex.StatusCode); + } + catch (Exception ex) + { + _logger.LogError(ex, $"定时任务[{jobName}]执行出现错误"); + } + } while (!stoppingToken.IsCancellationRequested && await _timer.WaitForNextTickAsync(stoppingToken)); + } + catch (OperationCanceledException) + { + _logger.LogInformation("Service {JobName} has been cancelled.", jobName); + } } protected abstract Task ExecuteAsync(DateTime RunTime, CancellationToken stoppingToken); public override Task StopAsync(CancellationToken cancellationToken) { - Logger.LogInformation("Service {JobName} is stopping.", jobName); - _timer.Dispose(); + _logger.LogInformation("Service {JobName} is stopping.", jobName); + _timer?.Dispose(); return base.StopAsync(cancellationToken); } } diff --git a/src/TokenPay/BgServices/CollectionTRONService.cs b/src/TokenPay/BgServices/CollectionTRONService.cs index 8041693..4576e97 100644 --- a/src/TokenPay/BgServices/CollectionTRONService.cs +++ b/src/TokenPay/BgServices/CollectionTRONService.cs @@ -17,7 +17,6 @@ public class CollectionTRONService : BaseScheduledService private readonly IConfiguration _configuration; private readonly TelegramBot _bot; private readonly IFreeSql freeSql; - private readonly ILogger _logger; /// /// 是否启用归集功能 /// @@ -67,7 +66,6 @@ public CollectionTRONService( ILogger logger) : base("TRON归集任务", TimeSpan.FromHours(configuration.GetValue("Collection:CheckTime", 1)), logger) { this._configuration = configuration; - this._logger = logger; this._bot = bot; this.freeSql = freeSql; } diff --git a/src/TokenPay/BgServices/OrderCheckEVMBaseService.cs b/src/TokenPay/BgServices/OrderCheckEVMBaseService.cs index 697cbcf..2081b1f 100644 --- a/src/TokenPay/BgServices/OrderCheckEVMBaseService.cs +++ b/src/TokenPay/BgServices/OrderCheckEVMBaseService.cs @@ -11,7 +11,6 @@ namespace TokenPay.BgServices { public class OrderCheckEVMBaseService : BaseScheduledService { - private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IHostEnvironment _env; private readonly Channel _channel; @@ -26,7 +25,6 @@ public OrderCheckEVMBaseService(ILogger logger, List Chains, IFreeSql freeSql) : base("ETH订单检测", TimeSpan.FromSeconds(15), logger) { - _logger = logger; this._configuration = configuration; this._env = env; this._channel = channel; diff --git a/src/TokenPay/BgServices/OrderCheckEVMERC20Service.cs b/src/TokenPay/BgServices/OrderCheckEVMERC20Service.cs index 491e741..9aa5bac 100644 --- a/src/TokenPay/BgServices/OrderCheckEVMERC20Service.cs +++ b/src/TokenPay/BgServices/OrderCheckEVMERC20Service.cs @@ -12,7 +12,6 @@ namespace TokenPay.BgServices { public class OrderCheckEVMERC20Service : BaseScheduledService { - private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IHostEnvironment _env; private readonly List _chains; @@ -27,7 +26,6 @@ public OrderCheckEVMERC20Service(ILogger logger, Channel channel, IFreeSql freeSql) : base("ERC20订单检测", TimeSpan.FromSeconds(15), logger) { - _logger = logger; this._configuration = configuration; this._env = env; _chains = Chains; diff --git a/src/TokenPay/BgServices/OrderCheckTRC20Service.cs b/src/TokenPay/BgServices/OrderCheckTRC20Service.cs index a6c1a59..882e244 100644 --- a/src/TokenPay/BgServices/OrderCheckTRC20Service.cs +++ b/src/TokenPay/BgServices/OrderCheckTRC20Service.cs @@ -11,7 +11,6 @@ namespace TokenPay.BgServices { public class OrderCheckTRC20Service : BaseScheduledService { - private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IHostEnvironment _env; private readonly Channel _channel; @@ -25,7 +24,6 @@ public OrderCheckTRC20Service(ILogger logger, Channel channel, IFreeSql freeSql) : base("TRC20订单检测", TimeSpan.FromSeconds(3), logger) { - _logger = logger; this._configuration = configuration; this._env = env; this._channel = channel; @@ -78,7 +76,7 @@ protected override async Task ExecuteAsync(DateTime RunTime, CancellationToken s .SetQueryParams(query) .WithTimeout(15); if (_env.IsProduction()) - req = req.WithHeader("TRON-PRO-API-KEY", _configuration.GetValue("TRON-PRO-API-KEY", "")); + req = req.WithHeader("TRON-PRO-API-KEY", _configuration.GetValue("TRON-PRO-API-KEY")); var result = await req .GetJsonAsync>(); diff --git a/src/TokenPay/BgServices/OrderCheckTRXService.cs b/src/TokenPay/BgServices/OrderCheckTRXService.cs index d828524..4e61db1 100644 --- a/src/TokenPay/BgServices/OrderCheckTRXService.cs +++ b/src/TokenPay/BgServices/OrderCheckTRXService.cs @@ -11,7 +11,6 @@ namespace TokenPay.BgServices { public class OrderCheckTRXService : BaseScheduledService { - private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IHostEnvironment _env; private readonly Channel _channel; @@ -25,7 +24,6 @@ public OrderCheckTRXService(ILogger logger, Channel channel, IFreeSql freeSql) : base("TRX订单检测", TimeSpan.FromSeconds(3), logger) { - _logger = logger; this._configuration = configuration; this._env = env; this._channel = channel; @@ -75,7 +73,7 @@ protected override async Task ExecuteAsync(DateTime RunTime, CancellationToken s .SetQueryParams(query) .WithTimeout(15); if (_env.IsProduction()) - req = req.WithHeader("TRON-PRO-API-KEY", _configuration.GetValue("TRON-PRO-API-KEY", "")); + req = req.WithHeader("TRON-PRO-API-KEY", _configuration.GetValue("TRON-PRO-API-KEY")); var result = await req .GetJsonAsync>(); diff --git a/src/TokenPay/BgServices/OrderExpiredService.cs b/src/TokenPay/BgServices/OrderExpiredService.cs index b5c83a4..7321bfd 100644 --- a/src/TokenPay/BgServices/OrderExpiredService.cs +++ b/src/TokenPay/BgServices/OrderExpiredService.cs @@ -5,7 +5,6 @@ namespace TokenPay.BgServices { public class OrderExpiredService : BaseScheduledService { - private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IFreeSql freeSql; @@ -13,7 +12,6 @@ public OrderExpiredService(ILogger logger, IConfiguration configuration, IFreeSql freeSql) : base("订单过期", TimeSpan.FromSeconds(10), logger) { - _logger = logger; this._configuration = configuration; this.freeSql = freeSql; } diff --git a/src/TokenPay/BgServices/OrderNotifyService.cs b/src/TokenPay/BgServices/OrderNotifyService.cs index 49b267d..2718e38 100644 --- a/src/TokenPay/BgServices/OrderNotifyService.cs +++ b/src/TokenPay/BgServices/OrderNotifyService.cs @@ -8,7 +8,6 @@ namespace TokenPay.BgServices { public class OrderNotifyService : BaseScheduledService { - private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IFreeSql freeSql; private readonly FlurlClient client; @@ -17,7 +16,6 @@ public OrderNotifyService(ILogger logger, IConfiguration configuration, IFreeSql freeSql) : base("订单通知", TimeSpan.FromSeconds(3), logger) { - _logger = logger; this._configuration = configuration; this.freeSql = freeSql; client = new FlurlClient(); diff --git a/src/TokenPay/BgServices/OrderPaySuccessService.cs b/src/TokenPay/BgServices/OrderPaySuccessService.cs index 2b07be9..2c8607c 100644 --- a/src/TokenPay/BgServices/OrderPaySuccessService.cs +++ b/src/TokenPay/BgServices/OrderPaySuccessService.cs @@ -13,7 +13,6 @@ public class OrderPaySuccessService : BaseBackgroundService private readonly TelegramBot _bot; private readonly List _chain; private readonly IConfiguration _configuration; - private readonly ILogger _logger; public OrderPaySuccessService( Channel channel, @@ -28,7 +27,6 @@ public OrderPaySuccessService( this._bot = bot; this._chain = chain; this._configuration = configuration; - this._logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) diff --git a/src/TokenPay/BgServices/UpdateRateService.cs b/src/TokenPay/BgServices/UpdateRateService.cs index bac8236..3285ff4 100644 --- a/src/TokenPay/BgServices/UpdateRateService.cs +++ b/src/TokenPay/BgServices/UpdateRateService.cs @@ -17,7 +17,6 @@ public class UpdateRateService : BaseScheduledService private readonly IConfiguration _configuration; private readonly List _chain; private readonly IFreeSql freeSql; - private readonly ILogger _logger; private FiatCurrency BaseCurrency => Enum.Parse(_configuration.GetValue("BaseCurrency", "CNY")!); public UpdateRateService( IConfiguration configuration, @@ -28,7 +27,6 @@ public UpdateRateService( this._configuration = configuration; this._chain = chain; this.freeSql = freeSql; - this._logger = logger; } private List GetActiveCurrency() diff --git a/src/TokenPay/Program.cs b/src/TokenPay/Program.cs index 22ff46f..0a09b2a 100644 --- a/src/TokenPay/Program.cs +++ b/src/TokenPay/Program.cs @@ -2,6 +2,7 @@ using Flurl.Http.Newtonsoft; using FreeSql; using Microsoft.AspNetCore.Mvc.Razor; +using Microsoft.Data.Sqlite; using Serilog; using Serilog.Events; using System.Diagnostics; @@ -72,8 +73,9 @@ var connectionString = Configuration.GetConnectionString("DB"); IFreeSql fsql = new FreeSqlBuilder() - .UseConnectionString(FreeSql.DataType.Sqlite, connectionString) + .UseConnectionString(DataType.Sqlite, connectionString) .UseAutoSyncStructure(true) //自动同步实体结构 + .UseAdoConnectionPool(true) .UseNoneCommandParameter(true) .Build();