Skip to content

Commit 56fc217

Browse files
committed
Use request's cancellationtoken in InitAsync
1 parent 1708ebe commit 56fc217

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

LiveControlGateway/LifetimeManager/HubLifetime.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum HubLifetimeState
4343

4444
private Dictionary<Guid, ShockerState> _shockerStates = new();
4545
private readonly byte _tps;
46-
private readonly CancellationTokenSource _cancellationToken;
46+
private readonly CancellationTokenSource _cancellationSource;
4747

4848
private readonly IDbContextFactory<OpenShockContext> _dbContextFactory;
4949
private readonly IRedisConnectionProvider _redisConnectionProvider;
@@ -68,7 +68,7 @@ public HubLifetime([Range(1, 10)] byte tps, IHubController hubController,
6868
{
6969
_tps = tps;
7070
HubController = hubController;
71-
_cancellationToken = new CancellationTokenSource();
71+
_cancellationSource = new CancellationTokenSource();
7272
_dbContextFactory = dbContextFactory;
7373
_redisConnectionProvider = redisConnectionProvider;
7474
_redisPubService = redisPubService;
@@ -81,15 +81,24 @@ public HubLifetime([Range(1, 10)] byte tps, IHubController hubController,
8181
/// <summary>
8282
/// Call on creation to setup shockers for the first time
8383
/// </summary>
84-
public async Task InitAsync()
84+
public async Task<bool> InitAsync(CancellationToken cancellationToken)
8585
{
86-
await using var db = await _dbContextFactory.CreateDbContextAsync(_cancellationToken.Token);
87-
await UpdateShockers(db);
86+
try
87+
{
88+
await using var db = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
89+
await UpdateShockers(db, cancellationToken);
8890
#pragma warning disable CS4014
89-
LucTask.Run(UpdateLoop);
91+
LucTask.Run(UpdateLoop);
9092
#pragma warning restore CS4014
93+
}
94+
catch (Exception e)
95+
{
96+
_logger.LogError(e, "Error initializing OpenShock Hub lifetime");
97+
return false;
98+
}
9199

92100
_state = HubLifetimeState.Idle; // We are fully setup, we can go back to idle state
101+
return true;
93102
}
94103

95104
/// <summary>
@@ -134,7 +143,7 @@ public bool TryMarkRemoving()
134143

135144
private async Task UpdateLoop()
136145
{
137-
while (!_cancellationToken.IsCancellationRequested)
146+
while (!_cancellationSource.IsCancellationRequested)
138147
{
139148
var startingTime = Stopwatch.GetTimestamp();
140149

@@ -156,7 +165,7 @@ private async Task UpdateLoop()
156165
continue;
157166
}
158167

159-
await Task.Delay(waitTime, _cancellationToken.Token);
168+
await Task.Delay(waitTime, _cancellationSource.Token);
160169
}
161170
}
162171

@@ -189,8 +198,8 @@ private async Task Update()
189198
/// </summary>
190199
public async Task UpdateDevice()
191200
{
192-
await using var db = await _dbContextFactory.CreateDbContextAsync(_cancellationToken.Token);
193-
await UpdateShockers(db);
201+
await using var db = await _dbContextFactory.CreateDbContextAsync(_cancellationSource.Token);
202+
await UpdateShockers(db, _cancellationSource.Token);
194203

195204
foreach (var websocketController in
196205
WebsocketManager.LiveControlUsers.GetConnections(HubController.Id))
@@ -200,7 +209,7 @@ public async Task UpdateDevice()
200209
/// <summary>
201210
/// Update all shockers config
202211
/// </summary>
203-
private async Task UpdateShockers(OpenShockContext db)
212+
private async Task UpdateShockers(OpenShockContext db, CancellationToken cancellationToken)
204213
{
205214
_logger.LogDebug("Updating shockers for device [{DeviceId}]", HubController.Id);
206215

@@ -209,7 +218,7 @@ private async Task UpdateShockers(OpenShockContext db)
209218
Id = x.Id,
210219
Model = x.Model,
211220
RfId = x.RfId
212-
}).ToDictionaryAsync(x => x.Id, x => x, cancellationToken: _cancellationToken.Token);
221+
}).ToDictionaryAsync(x => x.Id, x => x, cancellationToken);
213222
}
214223

215224
/// <summary>
@@ -353,7 +362,7 @@ public async ValueTask DisposeAsync()
353362
if(_disposed) return;
354363
_disposed = true;
355364

356-
await _cancellationToken.CancelAsync();
365+
await _cancellationSource.CancelAsync();
357366
}
358367

359368
}

LiveControlGateway/LifetimeManager/HubLifetimeManager.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public sealed class HubLifetimeManager
2424
private readonly IRedisPubService _redisPubService;
2525
private readonly ILoggerFactory _loggerFactory;
2626
private readonly ILogger<HubLifetimeManager> _logger;
27+
2728
private readonly Dictionary<Guid, HubLifetime> _lifetimes = new();
28-
2929
private readonly SemaphoreSlim _lifetimesLock = new(1);
3030

3131
/// <summary>
@@ -91,7 +91,12 @@ public async Task<bool> TryAddDeviceConnection(byte tps, IHubController hubContr
9191
}
9292
else
9393
{
94-
await hubLifetime.InitAsync();
94+
if (!await hubLifetime.InitAsync(cancellationToken))
95+
{
96+
// If we fail to initialize, the hub must be removed
97+
await RemoveDeviceConnection(hubController, false); // Here be dragons?
98+
return false;
99+
}
95100

96101
foreach (var websocketController in WebsocketManager.LiveControlUsers.GetConnections(hubLifetime
97102
.HubController.Id))
@@ -121,7 +126,8 @@ private HubLifetime CreateNewLifetime(byte tps, IHubController hubController)
121126
/// this is the actual end of life of the hub
122127
/// </summary>
123128
/// <param name="hubController"></param>
124-
public async Task RemoveDeviceConnection(IHubController hubController)
129+
/// <param name="notifyLiveControlClients"></param>
130+
public async Task RemoveDeviceConnection(IHubController hubController, bool notifyLiveControlClients = true)
125131
{
126132
HubLifetime? hubLifetime;
127133

@@ -142,9 +148,12 @@ public async Task RemoveDeviceConnection(IHubController hubController)
142148
return;
143149
}
144150
}
145-
146-
foreach (var websocketController in WebsocketManager.LiveControlUsers.GetConnections(hubController.Id))
147-
await websocketController.UpdateConnectedState(false);
151+
152+
if (notifyLiveControlClients)
153+
{
154+
foreach (var websocketController in WebsocketManager.LiveControlUsers.GetConnections(hubController.Id))
155+
await websocketController.UpdateConnectedState(false);
156+
}
148157

149158
await hubLifetime.DisposeAsync();
150159

0 commit comments

Comments
 (0)