-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTimedHostedService.cs
More file actions
100 lines (83 loc) · 3.53 KB
/
TimedHostedService.cs
File metadata and controls
100 lines (83 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using dsstats.db8services;
using dsstats.shared.Interfaces;
using pax.dsstats.web.Server.Services.Arcade;
namespace dsstats.api.Services;
public class TimedHostedService : BackgroundService
{
private int executionCount = 0;
private readonly IServiceScopeFactory scopeFactory;
private readonly ILogger<TimedHostedService> logger;
public TimedHostedService(IServiceScopeFactory scopeFactory, ILogger<TimedHostedService> logger)
{
this.scopeFactory = scopeFactory;
this.logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Timed Hosted Service running.");
// run every hour at the 30-minute mark
DateTime nowTime = DateTime.UtcNow;
DateTime startTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, nowTime.Hour, 30, 0);
if (nowTime.Minute >= 30)
startTime = startTime.AddHours(1);
TimeSpan waitTime = startTime - nowTime;
logger.LogInformation("Timed Hosted Service first run: {time}", startTime.ToString("yyyy-MM-dd HH:mm:ss"));
try
{
await Task.Delay(waitTime, stoppingToken);
await DoWork(stoppingToken);
using PeriodicTimer timer = new(TimeSpan.FromHours(1));
while (await timer.WaitForNextTickAsync(stoppingToken))
{
await DoWork(stoppingToken);
}
}
catch (OperationCanceledException)
{
logger.LogInformation("Timed Hosted Service is stopping.");
}
}
// Could also be a async method, that can be awaited in ExecuteAsync above
private async Task DoWork(CancellationToken token)
{
int count = Interlocked.Increment(ref executionCount);
try
{
using var scope = scopeFactory.CreateAsyncScope();
var ratingService = scope.ServiceProvider.GetRequiredService<IRatingService>();
var replayRepository = scope.ServiceProvider.GetRequiredService<IReplayRepository>();
DateTime nowTime = DateTime.UtcNow;
if (nowTime.Hour == 3)
{
await replayRepository.FixDsstatsPlayerNames();
try
{
var crawlerService = scope.ServiceProvider.GetRequiredService<CrawlerService>();
await crawlerService.GetLobbyHistory(DateTime.Today.AddDays(-6), token);
bool arcadeRecalc = nowTime.DayOfWeek == DayOfWeek.Friday;
await ratingService.ProduceRatings(shared.RatingCalcType.Arcade, arcadeRecalc);
await replayRepository.FixArcadePlayerNames();
}
catch (Exception ex)
{
logger.LogError("CrawlerService failed: {error}", ex.Message);
}
await ratingService.ProduceRatings(shared.RatingCalcType.Combo, true);
var ihService = scope.ServiceProvider.GetRequiredService<IIhService>();
await ihService.Cleanup();
}
else
{
await ratingService.ProduceRatings(shared.RatingCalcType.Dsstats);
await ratingService.ProduceRatings(shared.RatingCalcType.Combo, false);
}
await replayRepository.SetReplayViews();
await replayRepository.SetReplayDownloads();
}
catch (Exception ex)
{
logger.LogError("background job failed: {error}", ex.Message);
}
}
}