Skip to content

Commit fa2da3a

Browse files
committed
Implement retry logic for device addition and update in DeviceManager
1 parent 62e3c68 commit fa2da3a

1 file changed

Lines changed: 100 additions & 81 deletions

File tree

WsdScanService.Host/Services/DeviceManager.cs

Lines changed: 100 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public async Task RemoveDevice(string deviceId, uint instanceId, uint metadataVe
5454
if (_devicesToRemove.TryGetValue(deviceId, out var tokenSource))
5555
{
5656
await tokenSource.CancelAsync();
57+
tokenSource.Dispose();
5758
}
5859

5960
var removalTokenSource = new CancellationTokenSource();
@@ -108,16 +109,47 @@ public async Task AddDevice(string deviceId, string mexAddress, string type, uin
108109
metadataVersion
109110
);
110111

111-
if (deviceRepository.HasById(deviceId))
112-
{
113-
await UpdateDevice(deviceId, mexAddress, instanceId, metadataVersion);
114-
}
115-
else
112+
const int maxRetries = 3;
113+
const int delayMs = 3000;
114+
115+
for (var retryAttempt = 0; retryAttempt < maxRetries; retryAttempt++)
116116
{
117-
await AddNewDevice(deviceId, mexAddress, type, instanceId, metadataVersion);
117+
try
118+
{
119+
if (deviceRepository.HasById(deviceId))
120+
{
121+
await UpdateDevice(deviceId, mexAddress, instanceId, metadataVersion);
122+
break;
123+
}
124+
else
125+
{
126+
await AddNewDevice(deviceId, mexAddress, type, instanceId, metadataVersion);
127+
break;
128+
}
129+
}
130+
catch (Exception ex)
131+
{
132+
if (retryAttempt < maxRetries - 1)
133+
{
134+
logger.LogWarning(
135+
ex,
136+
"Error: {ErrorMessage}. Retrying in {Delay}ms... (Attempt {Attempt})",
137+
ex.Message,
138+
delayMs,
139+
retryAttempt + 1
140+
);
141+
142+
await Task.Delay(delayMs);
143+
}
144+
else
145+
{
146+
throw;
147+
}
148+
}
118149
}
119150
}
120151

152+
121153
private async Task UpdateDevice(string deviceId, string mexAddress, uint instanceId, uint metadataVersion)
122154
{
123155
var existingDevice = deviceRepository.GetById(deviceId);
@@ -131,6 +163,7 @@ private async Task UpdateDevice(string deviceId, string mexAddress, uint instanc
131163
deviceId
132164
);
133165
await tokenSource.CancelAsync();
166+
tokenSource.Dispose();
134167
_devicesToRemove.Remove(deviceId, out _);
135168
}
136169

@@ -143,46 +176,39 @@ private async Task UpdateDevice(string deviceId, string mexAddress, uint instanc
143176
metadataVersion
144177
);
145178

146-
try
147-
{
148-
var updatedMetadata = await scanner.GetScanDeviceMetadataAsync(deviceId, mexAddress);
179+
var updatedMetadata = await scanner.GetScanDeviceMetadataAsync(deviceId, mexAddress);
149180

150-
existingDevice.ModelName = updatedMetadata.ModelName;
151-
existingDevice.SerialNumber = updatedMetadata.SerialNumber;
152-
existingDevice.ScanServiceAddress = updatedMetadata.ScanServiceAddress;
181+
existingDevice.ModelName = updatedMetadata.ModelName;
182+
existingDevice.SerialNumber = updatedMetadata.SerialNumber;
183+
existingDevice.ScanServiceAddress = updatedMetadata.ScanServiceAddress;
153184

154-
foreach (var subscriptionEntry in existingDevice.Subscriptions)
185+
foreach (var subscriptionEntry in existingDevice.Subscriptions)
186+
{
187+
try
155188
{
156-
try
157-
{
158-
var subscription = await scanner.SubscribeAsync(
159-
existingDevice.ScanServiceAddress,
160-
subscriptionEntry.Key,
161-
_scanDestinations
162-
);
163-
164-
existingDevice.Subscriptions[subscriptionEntry.Key] = subscription;
165-
166-
logger.LogDebug(
167-
"Resubscribed {SubscriptionId} for device {DeviceId}",
168-
subscription.Identifier,
169-
deviceId
170-
);
171-
}
172-
catch (Exception ex)
173-
{
174-
logger.LogError(
175-
ex,
176-
"Failed to resubscribe {SubscriptionId} for device {DeviceId}.",
177-
subscriptionEntry.Value.Identifier,
178-
deviceId
179-
);
180-
}
189+
var subscription = await scanner.SubscribeAsync(
190+
existingDevice.ScanServiceAddress,
191+
subscriptionEntry.Key,
192+
_scanDestinations
193+
);
194+
195+
existingDevice.Subscriptions[subscriptionEntry.Key] = subscription;
196+
197+
logger.LogDebug(
198+
"Resubscribed {SubscriptionId} for device {DeviceId}",
199+
subscription.Identifier,
200+
deviceId
201+
);
202+
}
203+
catch (Exception ex)
204+
{
205+
logger.LogError(
206+
ex,
207+
"Failed to resubscribe {SubscriptionId} for device {DeviceId}.",
208+
subscriptionEntry.Value.Identifier,
209+
deviceId
210+
);
181211
}
182-
}
183-
catch (Exception ex)
184-
{
185-
logger.LogError(ex, "Failed to update device metadata for {DeviceId}", deviceId);
186212
}
187213

188214
existingDevice.InstanceId = instanceId;
@@ -199,52 +225,45 @@ private async Task AddNewDevice(
199225
uint metadataVersion
200226
)
201227
{
202-
try
203-
{
204-
var scanDeviceMetadata = await scanner.GetScanDeviceMetadataAsync(deviceId, mexAddress);
228+
var scanDeviceMetadata = await scanner.GetScanDeviceMetadataAsync(deviceId, mexAddress);
205229

206-
var subscriptions = new Dictionary<SubscriptionEventType, Subscription>
230+
var subscriptions = new Dictionary<SubscriptionEventType, Subscription>
231+
{
207232
{
208-
{
233+
SubscriptionEventType.ScanAvailableEvent,
234+
await scanner.SubscribeAsync(
235+
scanDeviceMetadata.ScanServiceAddress,
209236
SubscriptionEventType.ScanAvailableEvent,
210-
await scanner.SubscribeAsync(
211-
scanDeviceMetadata.ScanServiceAddress,
212-
SubscriptionEventType.ScanAvailableEvent,
213-
_scanDestinations
214-
)
215-
}
216-
};
237+
_scanDestinations
238+
)
239+
}
240+
};
217241

218242

219-
var newDevice = new Device
220-
{
221-
DeviceId = deviceId,
222-
Type = type,
223-
MexAddress = mexAddress,
224-
ScanServiceAddress = scanDeviceMetadata.ScanServiceAddress,
225-
ModelName = scanDeviceMetadata.ModelName,
226-
SerialNumber = scanDeviceMetadata.SerialNumber,
227-
ScanDestinations = _scanDestinations,
228-
Subscriptions = subscriptions,
229-
ScanTickets = configuration.Value.ScanProfiles.ToDictionary(
230-
e => e.Id,
231-
e => new ScanTicket
232-
{
233-
Resolution = e.Resolution
234-
}
235-
),
236-
InstanceId = instanceId,
237-
MetadataVersion = metadataVersion
238-
};
243+
var newDevice = new Device
244+
{
245+
DeviceId = deviceId,
246+
Type = type,
247+
MexAddress = mexAddress,
248+
ScanServiceAddress = scanDeviceMetadata.ScanServiceAddress,
249+
ModelName = scanDeviceMetadata.ModelName,
250+
SerialNumber = scanDeviceMetadata.SerialNumber,
251+
ScanDestinations = _scanDestinations,
252+
Subscriptions = subscriptions,
253+
ScanTickets = configuration.Value.ScanProfiles.ToDictionary(
254+
e => e.Id,
255+
e => new ScanTicket
256+
{
257+
Resolution = e.Resolution
258+
}
259+
),
260+
InstanceId = instanceId,
261+
MetadataVersion = metadataVersion
262+
};
239263

240-
deviceRepository.Add(newDevice);
264+
deviceRepository.Add(newDevice);
241265

242-
logger.LogInformation("Device added: {Device}", newDevice);
243-
}
244-
catch (Exception e)
245-
{
246-
logger.LogError(e, "Error while adding new device: {DeviceId} at {Address}", deviceId, mexAddress);
247-
}
266+
logger.LogInformation("Device added: {Device}", newDevice);
248267
}
249268

250269
public Task StartAsync(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)