-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStartInventoryCommandHandler.cs
More file actions
186 lines (157 loc) · 7.94 KB
/
StartInventoryCommandHandler.cs
File metadata and controls
186 lines (157 loc) · 7.94 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using ByteSync.Common.Business.Inventories;
using ByteSync.Common.Business.Sessions;
using ByteSync.ServerCommon.Business.Repositories;
using ByteSync.ServerCommon.Business.Sessions;
using ByteSync.ServerCommon.Entities;
using ByteSync.ServerCommon.Interfaces.Repositories;
using ByteSync.ServerCommon.Interfaces.Services;
using ByteSync.ServerCommon.Interfaces.Services.Clients;
using MediatR;
using Microsoft.Extensions.Logging;
using RedLockNet;
using StackExchange.Redis;
namespace ByteSync.ServerCommon.Commands.Inventories;
public class StartInventoryCommandHandler : IRequestHandler<StartInventoryRequest, StartInventoryResult>
{
private readonly IInventoryRepository _inventoryRepository;
private readonly ICloudSessionsRepository _cloudSessionsRepository;
private readonly ISharedFilesService _sharedFilesService;
private readonly IInvokeClientsService _invokeClientsService;
private readonly IRedisInfrastructureService _redisInfrastructureService;
private readonly ILogger<StartInventoryCommandHandler> _logger;
public StartInventoryCommandHandler(
IInventoryRepository inventoryRepository,
ICloudSessionsRepository cloudSessionsRepository,
ISharedFilesService sharedFilesService,
IInvokeClientsService invokeClientsService,
IRedisInfrastructureService redisInfrastructureService,
ILogger<StartInventoryCommandHandler> logger)
{
_inventoryRepository = inventoryRepository;
_cloudSessionsRepository = cloudSessionsRepository;
_sharedFilesService = sharedFilesService;
_invokeClientsService = invokeClientsService;
_redisInfrastructureService = redisInfrastructureService;
_logger = logger;
}
public async Task<StartInventoryResult> Handle(StartInventoryRequest request, CancellationToken cancellationToken)
{
var sessionId = request.SessionId;
var client = request.Client;
await using var sessionRedisLock = await _redisInfrastructureService.AcquireLockAsync(EntityType.Session, sessionId);
await using var inventoryRedisLock = await _redisInfrastructureService.AcquireLockAsync(EntityType.Inventory, sessionId);
var transaction = _redisInfrastructureService.OpenTransaction();
UpdateEntityResult<InventoryData>? inventoryUpdateResult = null;
var sessionUpdateResult = await ActivateSession(sessionId, transaction, sessionRedisLock);
var startInventoryResult = CheckSession(sessionId, sessionUpdateResult);
if (startInventoryResult != null)
{
return startInventoryResult;
}
if (sessionUpdateResult.IsWaitingForTransaction)
{
(inventoryUpdateResult, startInventoryResult) =
await UpdateInventory(sessionUpdateResult, transaction, inventoryRedisLock);
}
if (sessionUpdateResult.IsWaitingForTransaction
&& inventoryUpdateResult != null && inventoryUpdateResult.IsWaitingForTransaction)
{
await transaction.ExecuteAsync();
await _sharedFilesService.ClearSession(sessionId);
var dto = new InventoryStartedDTO(sessionId, client.ClientInstanceId, sessionUpdateResult.Element!.SessionSettings);
await _invokeClientsService.SessionGroupExcept(sessionId, client).InventoryStarted(dto);
_logger.LogInformation("StartInventory: session {SessionId} - OK", sessionId);
}
return startInventoryResult!;
}
private StartInventoryResult? CheckSession(string sessionId, UpdateEntityResult<CloudSessionData> sessionUpdateResult)
{
StartInventoryResult? startInventoryResult = null;
if (sessionUpdateResult.IsNoOperation)
{
startInventoryResult = LogAndBuildStartInventoryResult(sessionId, StartInventoryStatuses.InventoryStartedSucessfully, "already activated");
}
else if (sessionUpdateResult.IsNotFound)
{
startInventoryResult = LogAndBuildStartInventoryResult(sessionId, StartInventoryStatuses.SessionNotFound);
}
else
{
var cloudSessionData = sessionUpdateResult.Element!;
if (cloudSessionData.SessionMembers.Count < 2)
{
startInventoryResult = LogAndBuildStartInventoryResult(cloudSessionData, StartInventoryStatuses.LessThan2Members);
}
else if (cloudSessionData.SessionMembers.Count > 5)
{
startInventoryResult = LogAndBuildStartInventoryResult(cloudSessionData, StartInventoryStatuses.MoreThan5Members);
}
}
return startInventoryResult;
}
private async Task<(UpdateEntityResult<InventoryData> inventoryUpdateResult, StartInventoryResult? startInventoryResult)> UpdateInventory(
UpdateEntityResult<CloudSessionData> sessionUpdateResult, ITransaction transaction, IRedLock inventoryRedisLock)
{
StartInventoryResult? startInventoryResult = null;
var cloudSessionData = sessionUpdateResult.Element!;
UpdateEntityResult<InventoryData> inventoryUpdateResult;
inventoryUpdateResult = await _inventoryRepository.UpdateIfExists(cloudSessionData.SessionId, inventoryData =>
{
if (inventoryData.InventoryMembers.Count > cloudSessionData.SessionMembers.Count)
{
startInventoryResult = LogAndBuildStartInventoryResult(cloudSessionData, StartInventoryStatuses.UnknownError);
}
else if (inventoryData.InventoryMembers.Count < cloudSessionData.SessionMembers.Count
|| inventoryData.InventoryMembers.Any(imd => imd.SharedPathItems.Count == 0))
{
startInventoryResult = LogAndBuildStartInventoryResult(cloudSessionData, StartInventoryStatuses.AtLeastOneMemberWithNoDataToSynchronize);
}
if (startInventoryResult == null)
{
inventoryData.IsInventoryStarted = true;
startInventoryResult = StartInventoryResult.BuildOK();
}
return startInventoryResult.IsOK;
}, transaction, inventoryRedisLock);
if (inventoryUpdateResult.IsNotFound)
{
startInventoryResult = LogAndBuildStartInventoryResult(cloudSessionData,
StartInventoryStatuses.AtLeastOneMemberWithNoDataToSynchronize);
}
return (inventoryUpdateResult, startInventoryResult);
}
private async Task<UpdateEntityResult<CloudSessionData>> ActivateSession(string sessionId, ITransaction transaction, IRedLock sessionRedisLock)
{
var sessionUpdateResult = await _cloudSessionsRepository.UpdateIfExists(sessionId, session =>
{
if (!session.IsSessionActivated)
{
session.IsSessionActivated = true;
return true;
}
else
{
_logger.LogWarning("Session {sessionId} is already activated", sessionId);
return false;
}
}, transaction, sessionRedisLock);
return sessionUpdateResult;
}
private StartInventoryResult LogAndBuildStartInventoryResult(CloudSessionData cloudSessionData, StartInventoryStatuses status,
string? details = null)
{
return LogAndBuildStartInventoryResult(cloudSessionData.SessionId, status, details);
}
private StartInventoryResult LogAndBuildStartInventoryResult(string sessionId, StartInventoryStatuses status, string? details = null)
{
if (details != null)
{
_logger.LogInformation("StartInventory: session {SessionId} - {Status} - {Details}", sessionId, status, details);
}
else
{
_logger.LogInformation("StartInventory: session {SessionId} - {Status}", sessionId, status);
}
return StartInventoryResult.BuildFrom(status);
}
}