-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathOrganizationEventCleanupRepository.cs
More file actions
66 lines (58 loc) · 2.58 KB
/
Copy pathOrganizationEventCleanupRepository.cs
File metadata and controls
66 lines (58 loc) · 2.58 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
using System.Data;
using Bit.Core.Dirt.Entities;
using Bit.Core.Dirt.Repositories;
using Bit.Core.Settings;
using Bit.Infrastructure.Dapper.Repositories;
using Dapper;
using Microsoft.Data.SqlClient;
namespace Bit.Infrastructure.Dapper.Dirt.Repositories;
public class OrganizationEventCleanupRepository : BaseRepository, IOrganizationEventCleanupRepository
{
private const int LeaseDurationMinutes = 10;
private const int MaxFailureCount = 5;
public OrganizationEventCleanupRepository(GlobalSettings globalSettings)
: base(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
{ }
public async Task CreateAsync(OrganizationEventCleanup cleanup)
{
cleanup.SetNewId();
using var connection = new SqlConnection(ConnectionString);
await connection.ExecuteAsync(
"[dbo].[OrganizationEventCleanup_Create]",
new { cleanup.Id, cleanup.OrganizationId, cleanup.CreationDate },
commandType: CommandType.StoredProcedure);
}
public async Task<OrganizationEventCleanup?> ClaimNextPendingAsync()
{
var now = DateTime.UtcNow;
using var connection = new SqlConnection(ConnectionString);
return await connection.QuerySingleOrDefaultAsync<OrganizationEventCleanup>(
"[dbo].[OrganizationEventCleanup_ClaimNextPending]",
new { Now = now, StaleLeaseThreshold = now.AddMinutes(-LeaseDurationMinutes), MaxFailureCount },
commandType: CommandType.StoredProcedure);
}
public async Task UpdateProgressAsync(Guid id, long delta)
{
using var connection = new SqlConnection(ConnectionString);
await connection.ExecuteAsync(
"[dbo].[OrganizationEventCleanup_UpdateProgress]",
new { Id = id, Delta = delta, Now = DateTime.UtcNow },
commandType: CommandType.StoredProcedure);
}
public async Task UpdateErrorAsync(Guid id, string message)
{
using var connection = new SqlConnection(ConnectionString);
await connection.ExecuteAsync(
"[dbo].[OrganizationEventCleanup_UpdateError]",
new { Id = id, Message = message, Now = DateTime.UtcNow },
commandType: CommandType.StoredProcedure);
}
public async Task UpdateCompletedAsync(Guid id)
{
using var connection = new SqlConnection(ConnectionString);
await connection.ExecuteAsync(
"[dbo].[OrganizationEventCleanup_UpdateCompleted]",
new { Id = id, Now = DateTime.UtcNow },
commandType: CommandType.StoredProcedure);
}
}