LASAPlatform/OpenAuth.WebApi/SystemTask/DelayedExecutionService.cs

47 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using OpenAuth.App.ServiceApp.DroneDocking;
namespace OpenAuth.WebApi.SystemTask;
public class DelayedExecutionService : BackgroundService
{
private readonly ILogger<DelayedExecutionService> _logger;
private readonly DroneDockApp _droneDockApp;
public DelayedExecutionService(ILogger<DelayedExecutionService> logger, DroneDockApp droneDockApp)
{
_logger = logger;
_droneDockApp = droneDockApp;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("定时任务已启动将每5秒执行一次");
while (!stoppingToken.IsCancellationRequested)
{
try
{
await PerformDelayedTask();
// 等待5秒
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
catch (OperationCanceledException)
{
_logger.LogInformation("无人机状态上报任务被取消");
break;
}
catch (Exception ex)
{
_logger.LogError(ex, "无人机状态上报任务发生错误");
// 即使出错也继续下一次执行
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
}
}
}
private async Task PerformDelayedTask()
{
await _droneDockApp.ReportDroneStatus();
await Task.CompletedTask;
}
}