Skip to content

Commit e7a00b7

Browse files
committed
feat: add AcquireRedisLockException
1 parent c70992c commit e7a00b7

File tree

7 files changed

+47
-24
lines changed

7 files changed

+47
-24
lines changed

src/ByteSync.Client/Interfaces/Services/Sessions/ISessionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public interface ISessionService
5252

5353
Task SetLocalSession(LocalSession localSession, RunLocalSessionProfileInfo? runLocalSessionProfileInfo, SessionSettings sessionSettings);
5454

55-
Task SetSessionSettings(SessionSettings sessionSettings);
55+
Task SetSessionSettings(SessionSettings sessionSettings, bool callApi);
5656

5757
Task SetSessionStatus(SessionStatus sessionStatus);
5858

src/ByteSync.Client/Services/Inventories/DataInventoryStarter.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Reactive;
22
using System.Reactive.Linq;
3-
using System.Threading.Tasks;
43
using ByteSync.Business.PathItems;
54
using ByteSync.Business.Profiles;
65
using ByteSync.Business.SessionMembers;
@@ -11,15 +10,13 @@
1110
using ByteSync.Common.Business.Sessions;
1211
using ByteSync.Common.Business.Sessions.Cloud;
1312
using ByteSync.Common.Business.Sessions.Local;
14-
using ByteSync.Common.Helpers;
1513
using ByteSync.Interfaces.Controls.Communications;
1614
using ByteSync.Interfaces.Controls.Communications.Http;
1715
using ByteSync.Interfaces.Controls.Encryptions;
1816
using ByteSync.Interfaces.Controls.Inventories;
1917
using ByteSync.Interfaces.Repositories;
2018
using ByteSync.Interfaces.Services.Sessions;
2119
using DynamicData;
22-
using Serilog;
2320

2421
namespace ByteSync.Services.Inventories;
2522

@@ -32,10 +29,11 @@ public class DataInventoryStarter : IDataInventoryStarter
3229
private readonly IInventoryApiClient _inventoryApiClient;
3330
private readonly IPathItemRepository _pathItemRepository;
3431
private readonly ISessionMemberRepository _sessionMemberRepository;
32+
private readonly ILogger<DataInventoryStarter> _logger;
3533

36-
public DataInventoryStarter(ISessionService sessionService, ICloudProxy connectionManager,
37-
IDataEncrypter dataEncrypter, IDataInventoryRunner dataInventoryRunner,
38-
IInventoryApiClient inventoryApiClient, IPathItemRepository pathItemRepository, ISessionMemberRepository sessionMemberRepository)
34+
public DataInventoryStarter(ISessionService sessionService, ICloudProxy connectionManager, IDataEncrypter dataEncrypter,
35+
IDataInventoryRunner dataInventoryRunner, IInventoryApiClient inventoryApiClient, IPathItemRepository pathItemRepository,
36+
ISessionMemberRepository sessionMemberRepository, ILogger<DataInventoryStarter> logger)
3937
{
4038
_sessionService = sessionService;
4139
_connectionManager = connectionManager;
@@ -44,6 +42,7 @@ public DataInventoryStarter(ISessionService sessionService, ICloudProxy connecti
4442
_inventoryApiClient = inventoryApiClient;
4543
_pathItemRepository = pathItemRepository;
4644
_sessionMemberRepository = sessionMemberRepository;
45+
_logger = logger;
4746

4847
_connectionManager.HubPushHandler2.InventoryStarted
4948
.Where(dto => _sessionService.CheckSession(dto.SessionId))
@@ -65,7 +64,7 @@ public DataInventoryStarter(ISessionService sessionService, ICloudProxy connecti
6564
}
6665
catch (Exception e)
6766
{
68-
Log.Error(e, "An unexpected error occurred while checking the inventory auto-start");
67+
_logger.LogError(e, "An unexpected error occurred while checking the inventory auto-start");
6968
}
7069
return Unit.Default;
7170
})
@@ -130,11 +129,11 @@ public async Task<StartInventoryResult> StartDataInventory(bool isLaunchedByUser
130129

131130
if (isLaunchedByUser)
132131
{
133-
Log.Information("The current user has requested to start the Data Inventory");
132+
_logger.LogInformation("The current user has requested to start the Data Inventory");
134133
}
135134
else
136135
{
137-
Log.Information("The Data Inventory has been automatically started");
136+
_logger.LogInformation("The Data Inventory has been automatically started");
138137
}
139138

140139
var sessionSettings = _sessionService.CurrentSessionSettings;
@@ -150,14 +149,12 @@ public async Task<StartInventoryResult> StartDataInventory(bool isLaunchedByUser
150149
{
151150
return result;
152151
}
153-
154-
// todo : remonter également les autres paramètres pour contrôle par les aux parties de l'égalité des paramètres
155152

156153
result = await SendSessionSettings(session, sessionSettings);
157154

158155
if (result.IsOK)
159156
{
160-
await _sessionService.SetSessionSettings(sessionSettings);
157+
await _sessionService.SetSessionSettings(sessionSettings, false);
161158
await _dataInventoryRunner.RunDataInventory();
162159

163160
return result;
@@ -172,17 +169,17 @@ public async void OnDataInventoryStarted(InventoryStartedDTO inventoryStartedDto
172169
{
173170
try
174171
{
175-
Log.Information("The Data Inventory has been started by another client (ClientInstanceId:{ClientInstanceId})",
172+
_logger.LogInformation("The Data Inventory has been started by another client (ClientInstanceId:{ClientInstanceId})",
176173
inventoryStartedDto.ClientInstanceId);
177174

178175
var sessionSettings = _dataEncrypter.DecryptSessionSettings(inventoryStartedDto.EncryptedSessionSettings);
179176

180-
await _sessionService.SetSessionSettings(sessionSettings);
177+
await _sessionService.SetSessionSettings(sessionSettings, false);
181178
await _dataInventoryRunner.RunDataInventory();
182179
}
183180
catch (Exception ex)
184181
{
185-
Log.Error(ex, "OnStartInventory");
182+
_logger.LogError(ex, "OnStartInventory");
186183
}
187184
}
188185

@@ -269,7 +266,7 @@ private void FinalizeSessionSettings(SessionSettings sessionSettings)
269266

270267
private StartInventoryResult LogAndBuildStartInventoryResult(AbstractSession localSession, StartInventoryStatuses status)
271268
{
272-
Log.Information("StartInventory: session {@localSession} - {Status}", localSession.SessionId, status);
269+
_logger.LogInformation("StartInventory: session {@localSession} - {Status}", localSession.SessionId, status);
273270
return StartInventoryResult.BuildFrom(status);
274271
}
275272
}

src/ByteSync.Client/Services/Sessions/SessionService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,18 @@ public Task SetLocalSession(LocalSession localSession, RunLocalSessionProfileInf
164164
});
165165
}
166166

167-
public async Task SetSessionSettings(SessionSettings sessionSettings)
167+
public async Task SetSessionSettings(SessionSettings sessionSettings, bool callApi)
168168
{
169169
var currentSession = CurrentSession;
170170

171171
if (currentSession is CloudSession)
172172
{
173173
var encryptedSessionSettings = _dataEncrypter.EncryptSessionSettings(sessionSettings);
174-
175-
await _cloudSessionApiClient.UpdateSettings(currentSession.SessionId, encryptedSessionSettings);
174+
175+
if (callApi)
176+
{
177+
await _cloudSessionApiClient.UpdateSettings(currentSession.SessionId, encryptedSessionSettings);
178+
}
176179
}
177180

178181
_sessionSettings.OnNext(sessionSettings);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using RedLockNet;
2+
3+
namespace ByteSync.ServerCommon.Exceptions;
4+
5+
public class AcquireRedisLockException : Exception
6+
{
7+
public AcquireRedisLockException(string message) : base(message)
8+
{
9+
}
10+
11+
public AcquireRedisLockException(string message, Exception innerException) : base(message, innerException)
12+
{
13+
}
14+
15+
public AcquireRedisLockException(string key, IRedLock redisLock) :
16+
base("Could not acquire redis lock, key: " + key + ", status: " + redisLock.Status)
17+
{
18+
19+
}
20+
}

src/ByteSync.ServerCommon/Repositories/BaseRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ByteSync.Common.Controls.Json;
33
using ByteSync.Common.Helpers;
44
using ByteSync.ServerCommon.Business.Repositories;
5+
using ByteSync.ServerCommon.Exceptions;
56
using ByteSync.ServerCommon.Interfaces.Repositories;
67
using ByteSync.ServerCommon.Interfaces.Services;
78
using RedLockNet;
@@ -134,7 +135,7 @@ private async Task<UpdateEntityResult<T>> DoUpdate(string key, Func<T, bool> upd
134135
}
135136
else
136137
{
137-
throw new Exception("Could not acquire redis lock");
138+
throw new AcquireRedisLockException(key, redisLock);
138139
}
139140
}
140141
finally

src/ByteSync.ServerCommon/Repositories/SharedFilesRepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ByteSync.Common.Business.SharedFiles;
22
using ByteSync.ServerCommon.Business.Sessions;
3+
using ByteSync.ServerCommon.Exceptions;
34
using ByteSync.ServerCommon.Interfaces.Repositories;
45
using ByteSync.ServerCommon.Interfaces.Services;
56

@@ -97,15 +98,15 @@ public async Task<List<SharedFileData>> Clear(string sessionId)
9798
}
9899
else
99100
{
100-
throw new Exception("Could not acquire redis lock");
101+
throw new AcquireRedisLockException(sharedFileCacheKey, sharedFileLock);
101102
}
102103
}
103104

104105
await database.KeyDeleteAsync(sessionSharedFilesKey);
105106
}
106107
else
107108
{
108-
throw new Exception("Could not acquire redis lock");
109+
throw new AcquireRedisLockException(sessionSharedFilesKey, sessionSharedFilesLock);
109110
}
110111

111112
return result;

src/ByteSync.ServerCommon/Services/CacheService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ByteSync.ServerCommon.Business.Settings;
2+
using ByteSync.ServerCommon.Exceptions;
23
using ByteSync.ServerCommon.Interfaces.Services;
34
using Microsoft.Extensions.Logging;
45
using Microsoft.Extensions.Options;
@@ -67,7 +68,7 @@ public async Task<IRedLock> AcquireLockAsync(string key)
6768
}
6869
else
6970
{
70-
throw new Exception("Could not acquire redis lock for key: " + key);
71+
throw new AcquireRedisLockException(key, redisLock);
7172
}
7273
}
7374
}

0 commit comments

Comments
 (0)