From 0ec4c803e55d9dedee5c7c1c4c33ec6a641d172c Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Mon, 26 May 2025 13:54:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectBusApplicationModule.cs | 11 +++--- .../JiShe.CollectBus.Application.csproj | 1 - ...eduledMeterReadingBackGroundWorkService.cs | 34 +++++++++++++++++++ .../SystemBackGroundWorkService.cs | 5 ++- 4 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 services/JiShe.CollectBus.Application/Workers/ScheduledMeterReadingBackGroundWorkService.cs diff --git a/services/JiShe.CollectBus.Application/CollectBusApplicationModule.cs b/services/JiShe.CollectBus.Application/CollectBusApplicationModule.cs index 8fa7033..2d14fe9 100644 --- a/services/JiShe.CollectBus.Application/CollectBusApplicationModule.cs +++ b/services/JiShe.CollectBus.Application/CollectBusApplicationModule.cs @@ -7,6 +7,7 @@ using JiShe.CollectBus.IotSystems.MeterReadingRecords; using JiShe.CollectBus.Kafka; using JiShe.CollectBus.Protocol; using JiShe.CollectBus.ScheduledMeterReading; +using JiShe.CollectBus.Workers; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; @@ -44,6 +45,8 @@ public class CollectBusApplicationModule : AbpModule context.Services.AddAutoMapperObjectMapper(); Configure(options => { options.AddMaps(true); }); + context.Services.AddHostedService(); + //context.Services.AddSingleton(new MappingConfiguration() // .Define(new CollectBusMapping())); @@ -59,13 +62,7 @@ public class CollectBusApplicationModule : AbpModule public override async Task OnApplicationInitializationAsync( ApplicationInitializationContext context) { - //var assembly = Assembly.GetExecutingAssembly(); - //var types = assembly.GetTypes().Where(t => typeof(ICollectWorker).IsAssignableFrom(t) && !t.IsInterface) - // .ToList(); - //foreach (var type in types) - //{ - // await context.AddBackgroundWorkerAsync(type); - //} + //下发任务通道构建 DataChannelManage.TaskDataChannel = Channel.CreateUnbounded>>(); diff --git a/services/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj b/services/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj index 5dae431..a893230 100644 --- a/services/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj +++ b/services/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj @@ -32,7 +32,6 @@ - diff --git a/services/JiShe.CollectBus.Application/Workers/ScheduledMeterReadingBackGroundWorkService.cs b/services/JiShe.CollectBus.Application/Workers/ScheduledMeterReadingBackGroundWorkService.cs new file mode 100644 index 0000000..252316d --- /dev/null +++ b/services/JiShe.CollectBus.Application/Workers/ScheduledMeterReadingBackGroundWorkService.cs @@ -0,0 +1,34 @@ +using JiShe.CollectBus.Common.WorkService; +using JiShe.CollectBus.ScheduledMeterReading; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace JiShe.CollectBus.Workers +{ + /// + /// 定时抄表 + /// + /// 抄读服务 + /// + public class ScheduledMeterReadingBackGroundWorkService(IScheduledMeterReadingService scheduledMeterReadingService, ILogger logger) : SystemBackGroundWorkService(logger) + { + protected override TimeSpan GetInterval() + { + // 返回执行间隔时间 + return TimeSpan.FromSeconds(10); + } + + protected override async Task DoWorkAsync(CancellationToken cancellationToken) + { + // 这里编写具体的后台任务逻辑,例如从数据库中读取数据、发送网络请求等 + // 这里的逻辑会每隔一段时间执行一次,时间间隔由 GetInterval 方法返回的值决定 + Logger.LogWarning($"定时抄读后台任务执行中...{DateTime.Now.ToString()}"); + //await scheduledMeterReadingService.CreateToBeIssueTasks(); + } + } +} diff --git a/shared/JiShe.CollectBus.Common/WorkService/SystemBackGroundWorkService.cs b/shared/JiShe.CollectBus.Common/WorkService/SystemBackGroundWorkService.cs index 4b2ada0..e1054cd 100644 --- a/shared/JiShe.CollectBus.Common/WorkService/SystemBackGroundWorkService.cs +++ b/shared/JiShe.CollectBus.Common/WorkService/SystemBackGroundWorkService.cs @@ -40,11 +40,10 @@ namespace JiShe.CollectBus.Common.WorkService /// protected abstract TimeSpan GetInterval(); - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.CompletedTask;// 等待其他任务执行完成,避免阻塞应用程序启动 - Logger.LogInformation($"任务每隔{interval.TotalSeconds}秒执行一次"); + // Logger.LogInformation($"任务每隔{interval.TotalSeconds}秒执行一次"); await InitAsync(cancellationTokenSource.Token); } @@ -69,7 +68,7 @@ namespace JiShe.CollectBus.Common.WorkService { Logger.LogInformation("后台服务停止……"); cancellationTokenSource.Cancel(); - return base.StopAsync(cancellationToken); + return Task.CompletedTask; } ///