25 lines
829 B
C#
25 lines
829 B
C#
|
|
using YD_TrainingCompetition_Api.Competition;
|
||
|
|
|
||
|
|
namespace YD_TrainingCompetition_Api.Services;
|
||
|
|
|
||
|
|
// 单独的倒计时后台服务,每秒推动一次比赛会话状态。
|
||
|
|
// 这样即使设备端没有持续上报时间,前端也能稳定看到倒计时变化。
|
||
|
|
public sealed class CompetitionCountdownWorker : BackgroundService
|
||
|
|
{
|
||
|
|
private readonly ICompetitionSessionService _competition;
|
||
|
|
|
||
|
|
public CompetitionCountdownWorker(ICompetitionSessionService competition)
|
||
|
|
{
|
||
|
|
_competition = competition;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||
|
|
{
|
||
|
|
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
|
||
|
|
while (await timer.WaitForNextTickAsync(stoppingToken))
|
||
|
|
{
|
||
|
|
await _competition.TickAsync(stoppingToken);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|