Skip to content

Commit 272f107

Browse files
committed
misc: 将每分钟更新的记录数提升到160。
1 parent c21c50e commit 272f107

1 file changed

Lines changed: 62 additions & 14 deletions

File tree

Bleatingsheep.NewHydrant.DataMaintenance/UpdateSnapshotsService.cs

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.EntityFrameworkCore;
44

55
namespace Bleatingsheep.NewHydrant.DataMaintenance;
6+
67
public class UpdateSnapshotsService : BackgroundService
78
{
89
private static readonly TimeSpan s_updateScheduleDefault = TimeSpan.FromHours(8);
@@ -15,7 +16,11 @@ public class UpdateSnapshotsService : BackgroundService
1516
private readonly DataMaintainer _dataMaintainer;
1617
private readonly ILogger<UpdateSnapshotsService> _logger;
1718

18-
public UpdateSnapshotsService(IDbContextFactory<NewbieContext> dbContextFactory, DataMaintainer dataMaintainer, ILogger<UpdateSnapshotsService> logger)
19+
public UpdateSnapshotsService(
20+
IDbContextFactory<NewbieContext> dbContextFactory,
21+
DataMaintainer dataMaintainer,
22+
ILogger<UpdateSnapshotsService> logger
23+
)
1924
{
2025
_dbContextFactory = dbContextFactory;
2126
_dataMaintainer = dataMaintainer;
@@ -25,24 +30,37 @@ public UpdateSnapshotsService(IDbContextFactory<NewbieContext> dbContextFactory,
2530
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2631
{
2732
var timer = new PeriodicTimer(TimeSpan.FromMinutes(1));
28-
while (await timer.WaitForNextTickAsync(stoppingToken) && !stoppingToken.IsCancellationRequested)
33+
while (
34+
await timer.WaitForNextTickAsync(stoppingToken)
35+
&& !stoppingToken.IsCancellationRequested
36+
)
2937
{
3038
try
3139
{
3240
await using var db = _dbContextFactory.CreateDbContext();
33-
int scheduledCount = await db.UpdateSchedules.CountAsync(s => s.NextUpdate <= DateTimeOffset.UtcNow, stoppingToken);
41+
int scheduledCount = await db.UpdateSchedules.CountAsync(
42+
s => s.NextUpdate <= DateTimeOffset.UtcNow,
43+
stoppingToken
44+
);
3445
if (scheduledCount == 0)
46+
{
3547
continue;
48+
}
49+
3650
if (scheduledCount >= 500)
3751
{
3852
_logger.LogWarning("Backlog is {count}", scheduledCount);
3953
}
40-
var toUpdate = await db.UpdateSchedules
41-
.Where(s => s.NextUpdate <= DateTimeOffset.UtcNow)
54+
var toUpdate = await db
55+
.UpdateSchedules.Where(s => s.NextUpdate <= DateTimeOffset.UtcNow)
4256
.OrderBy(s => s.NextUpdate)
43-
.Take(120)
57+
.Take(160)
4458
.ToListAsync(stoppingToken);
45-
_logger.LogDebug("Updating {toUpdate.Count} of {scheduledCount} snapshots.", toUpdate.Count, scheduledCount);
59+
_logger.LogDebug(
60+
"Updating {toUpdate.Count} of {scheduledCount} snapshots.",
61+
toUpdate.Count,
62+
scheduledCount
63+
);
4664
int successCount = 0;
4765
int normalCount = 0;
4866
int activeCount = 0;
@@ -53,7 +71,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5371
{
5472
try
5573
{
56-
var report = await _dataMaintainer.UpdateNowAsync(schedule.UserId, schedule.Mode);
74+
var report = await _dataMaintainer.UpdateNowAsync(
75+
schedule.UserId,
76+
schedule.Mode
77+
);
5778
TimeSpan nextDelay;
5879
if (report.UserNotExists || report.Inactive)
5980
{
@@ -87,26 +108,53 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
87108
{
88109
if (e.Message.Contains("429 Too Many Requests"))
89110
{
90-
_logger.LogInformation("Reached API rate limit on user id {schedule.UserId} mode {schedule.Mode}", schedule.UserId, schedule.Mode);
111+
_logger.LogInformation(
112+
"Reached API rate limit on user id {schedule.UserId} mode {schedule.Mode}",
113+
schedule.UserId,
114+
schedule.Mode
115+
);
91116
}
92117
else if (e.Message.Contains("502 Bad Gateway"))
93118
{
94-
_logger.LogInformation("Server error, breaking to wait for server to get normal. user id {schedule.UserId} mode {schedule.Mode}", schedule.UserId, schedule.Mode);
119+
_logger.LogInformation(
120+
"Server error, breaking to wait for server to get normal. user id {schedule.UserId} mode {schedule.Mode}",
121+
schedule.UserId,
122+
schedule.Mode
123+
);
95124
}
96125
else
97126
{
98-
_logger.LogError(e, "Update error on user id {schedule.UserId} mode {schedule.Mode}", schedule.UserId, schedule.Mode);
127+
_logger.LogError(
128+
e,
129+
"Update error on user id {schedule.UserId} mode {schedule.Mode}",
130+
schedule.UserId,
131+
schedule.Mode
132+
);
99133
}
100134
break;
101135
}
102136
}
103137
await db.SaveChangesAsync(stoppingToken).ConfigureAwait(false); // 将 schedule 更新持久化到数据库
104-
_logger.LogDebug("Update schedule completed. Success {successCount} of {toUpdate.Count}.", successCount, toUpdate.Count);
105-
_logger.LogDebug("Normal: {normalCount}, Active: {activeCount}, SemiActive: {semiActiveCount}, Inactive: {inactiveCount}, No Change: {noChangeCount}", normalCount, activeCount, semiActiveCount, inactiveCount, noChangeCount);
138+
_logger.LogDebug(
139+
"Update schedule completed. Success {successCount} of {toUpdate.Count}.",
140+
successCount,
141+
toUpdate.Count
142+
);
143+
_logger.LogDebug(
144+
"Normal: {normalCount}, Active: {activeCount}, SemiActive: {semiActiveCount}, Inactive: {inactiveCount}, No Change: {noChangeCount}",
145+
normalCount,
146+
activeCount,
147+
semiActiveCount,
148+
inactiveCount,
149+
noChangeCount
150+
);
106151
}
107152
catch (DbUpdateConcurrencyException e)
108153
{
109-
_logger.LogWarning("数据库更新并发冲突,可能是其他服务修改了更新 schedule:{message}。", e.Message);
154+
_logger.LogWarning(
155+
"数据库更新并发冲突,可能是其他服务修改了更新 schedule:{message}。",
156+
e.Message
157+
);
110158
}
111159
catch (Exception e)
112160
{

0 commit comments

Comments
 (0)