添加定时后台服务
This commit is contained in:
parent
ea91622217
commit
0ec4c803e5
@ -7,6 +7,7 @@ using JiShe.CollectBus.IotSystems.MeterReadingRecords;
|
|||||||
using JiShe.CollectBus.Kafka;
|
using JiShe.CollectBus.Kafka;
|
||||||
using JiShe.CollectBus.Protocol;
|
using JiShe.CollectBus.Protocol;
|
||||||
using JiShe.CollectBus.ScheduledMeterReading;
|
using JiShe.CollectBus.ScheduledMeterReading;
|
||||||
|
using JiShe.CollectBus.Workers;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -44,6 +45,8 @@ public class CollectBusApplicationModule : AbpModule
|
|||||||
context.Services.AddAutoMapperObjectMapper<CollectBusApplicationModule>();
|
context.Services.AddAutoMapperObjectMapper<CollectBusApplicationModule>();
|
||||||
Configure<AbpAutoMapperOptions>(options => { options.AddMaps<CollectBusApplicationModule>(true); });
|
Configure<AbpAutoMapperOptions>(options => { options.AddMaps<CollectBusApplicationModule>(true); });
|
||||||
|
|
||||||
|
context.Services.AddHostedService<ScheduledMeterReadingBackGroundWorkService>();
|
||||||
|
|
||||||
//context.Services.AddSingleton(new MappingConfiguration()
|
//context.Services.AddSingleton(new MappingConfiguration()
|
||||||
// .Define(new CollectBusMapping()));
|
// .Define(new CollectBusMapping()));
|
||||||
|
|
||||||
@ -59,13 +62,7 @@ public class CollectBusApplicationModule : AbpModule
|
|||||||
public override async Task OnApplicationInitializationAsync(
|
public override async Task OnApplicationInitializationAsync(
|
||||||
ApplicationInitializationContext context)
|
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<ValueTuple<string, List<MeterReadingTelemetryPacketInfo>>>();
|
DataChannelManage.TaskDataChannel = Channel.CreateUnbounded<ValueTuple<string, List<MeterReadingTelemetryPacketInfo>>>();
|
||||||
|
|||||||
@ -32,7 +32,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Mappers\" />
|
<Folder Include="Mappers\" />
|
||||||
<Folder Include="Workers\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 定时抄表
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scheduledMeterReadingService">抄读服务</param>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
public class ScheduledMeterReadingBackGroundWorkService(IScheduledMeterReadingService scheduledMeterReadingService, ILogger<ScheduledMeterReadingBackGroundWorkService> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -40,11 +40,10 @@ namespace JiShe.CollectBus.Common.WorkService
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected abstract TimeSpan GetInterval();
|
protected abstract TimeSpan GetInterval();
|
||||||
|
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
await Task.CompletedTask;// 等待其他任务执行完成,避免阻塞应用程序启动
|
await Task.CompletedTask;// 等待其他任务执行完成,避免阻塞应用程序启动
|
||||||
Logger.LogInformation($"任务每隔{interval.TotalSeconds}秒执行一次");
|
// Logger.LogInformation($"任务每隔{interval.TotalSeconds}秒执行一次");
|
||||||
await InitAsync(cancellationTokenSource.Token);
|
await InitAsync(cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +68,7 @@ namespace JiShe.CollectBus.Common.WorkService
|
|||||||
{
|
{
|
||||||
Logger.LogInformation("后台服务停止……");
|
Logger.LogInformation("后台服务停止……");
|
||||||
cancellationTokenSource.Cancel();
|
cancellationTokenSource.Cancel();
|
||||||
return base.StopAsync(cancellationToken);
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user