You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
using Build_God_Api.Services;
|
|
|
|
namespace Build_God_Api.Services
|
|
{
|
|
public class DailyMissionHostedService(
|
|
IServiceProvider serviceProvider,
|
|
ILogger<DailyMissionHostedService> logger
|
|
) : BackgroundService
|
|
{
|
|
private readonly IServiceProvider _serviceProvider = serviceProvider;
|
|
private readonly ILogger<DailyMissionHostedService> _logger = logger;
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("每日任务后台服务已启动");
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
using var scope = _serviceProvider.CreateScope();
|
|
var dailyMissionService = scope.ServiceProvider.GetRequiredService<IDailyMissionService>();
|
|
|
|
await dailyMissionService.CheckAndCompleteExpiredMissions();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "检查每日任务时发生错误");
|
|
}
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
|
|
}
|
|
|
|
_logger.LogInformation("每日任务后台服务已停止");
|
|
}
|
|
}
|
|
}
|
|
|