This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCleanupOrphanRecordsService.cs
More file actions
101 lines (87 loc) · 4.1 KB
/
CleanupOrphanRecordsService.cs
File metadata and controls
101 lines (87 loc) · 4.1 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
101
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using NuGet.Insights.ReferenceTracking;
namespace NuGet.Insights.Worker.ReferenceTracking
{
public class CleanupOrphanRecordsService<T> : ICleanupOrphanRecordsService<T> where T : ICleanupOrphanCsvRecord
{
private readonly ContainerInitializationState _initializationState;
private readonly ContainerInitializationState _referenceTrackerInitializationState;
private readonly AutoRenewingStorageLeaseService _leaseService;
private readonly ICleanupOrphanRecordsAdapter<T> _adapter;
private readonly IMessageEnqueuer _messageEnqueuer;
private readonly TaskStateStorageService _taskStateStorageService;
private readonly ReferenceTracker _referenceTracker;
private readonly SchemaSerializer _schemaSerializer;
public CleanupOrphanRecordsService(
AutoRenewingStorageLeaseService leaseService,
ICleanupOrphanRecordsAdapter<T> adapter,
IMessageEnqueuer messageEnqueuer,
TaskStateStorageService taskStateStorageService,
ReferenceTracker referenceTracker,
SchemaSerializer schemaSerializer)
{
_initializationState = ContainerInitializationState.New(InitializeInternalAsync);
_referenceTrackerInitializationState = ContainerInitializationState.New(() => referenceTracker.InitializeAsync(
adapter.OwnerToSubjectTableName,
adapter.SubjectToOwnerTableName));
_leaseService = leaseService;
_adapter = adapter;
_messageEnqueuer = messageEnqueuer;
_taskStateStorageService = taskStateStorageService;
_referenceTracker = referenceTracker;
_schemaSerializer = schemaSerializer;
}
public async Task InitializeAsync()
{
await _initializationState.InitializeAsync();
}
private async Task InitializeInternalAsync()
{
await Task.WhenAll(
_leaseService.InitializeAsync(),
_messageEnqueuer.InitializeAsync(),
_taskStateStorageService.InitializeAsync(TaskStateStorageService.SingletonStorageSuffix));
}
private string OperationName => $"CleanupOrphans-{_adapter.OwnerType}-{_adapter.SubjectType}";
public async Task<bool> StartAsync()
{
await using (var lease = await _leaseService.TryAcquireWithRetryAsync(OperationName))
{
if (!lease.Acquired)
{
return false;
}
if (await IsRunningAsync())
{
return false;
}
await _referenceTrackerInitializationState.InitializeAsync();
var cleanupId = StorageUtility.GenerateDescendingId();
var cleanupIdStr = cleanupId.ToString();
var storageSuffix = cleanupId.Unique;
var parameters = _schemaSerializer.Serialize(new CleanupOrphanRecordsParameters()).AsString();
var taskState = new TaskState(TaskStateStorageService.SingletonStorageSuffix, OperationName, cleanupIdStr)
{
Parameters = parameters,
};
await _messageEnqueuer.EnqueueAsync(new[]
{
new CleanupOrphanRecordsMessage<T>
{
TaskStateKey = taskState.GetKey(),
CleanupId = cleanupIdStr,
StorageSuffix = storageSuffix,
}
});
await _taskStateStorageService.GetOrAddAsync(taskState);
return true;
}
}
public async Task<bool> IsRunningAsync()
{
var countLowerBound = await _taskStateStorageService.GetCountLowerBoundAsync(TaskStateStorageService.SingletonStorageSuffix, OperationName);
return countLowerBound > 0;
}
}
}