public async Task GivenADeletedDicomInstance_WhenIncrementingRetryCount_NewRetryCountShouldBeReturned() { string studyInstanceUid = TestUidGenerator.Generate(); string seriesInstanceUid = TestUidGenerator.Generate(); string sopInstanceUid = TestUidGenerator.Generate(); Instance instance1 = await CreateIndexAndVerifyInstance(studyInstanceUid, seriesInstanceUid, sopInstanceUid); await _indexDataStore.DeleteInstanceIndexAsync(studyInstanceUid, seriesInstanceUid, sopInstanceUid, Clock.UtcNow); DeletedInstance deletedEntry = (await _testHelper.GetDeletedInstanceEntriesAsync(studyInstanceUid, seriesInstanceUid, sopInstanceUid)).First(); var versionedDicomInstanceIdentifier = new VersionedInstanceIdentifier(studyInstanceUid, seriesInstanceUid, sopInstanceUid, deletedEntry.Watermark); var retryCount = await _indexDataStore.IncrementDeletedInstanceRetryAsync(versionedDicomInstanceIdentifier, Clock.UtcNow); Assert.Equal(1, retryCount); }
/// <inheritdoc /> public async Task <int> IncrementDeletedInstanceRetryAsync(VersionedInstanceIdentifier versionedInstanceIdentifier, DateTimeOffset cleanupAfter, CancellationToken cancellationToken) { LogIncrementDeletedInstanceRetryAsyncDelegate(_logger, versionedInstanceIdentifier, cleanupAfter, null); try { int returnValue = await _indexDataStore.IncrementDeletedInstanceRetryAsync(versionedInstanceIdentifier, cleanupAfter, cancellationToken); LogOperationSucceededDelegate(_logger, null); return(returnValue); } catch (Exception ex) { LogOperationFailedDelegate(_logger, ex); throw; } }
public async Task <(bool success, int retrievedInstanceCount)> CleanupDeletedInstancesAsync(CancellationToken cancellationToken) { bool success = true; int retrievedInstanceCount = 0; using (ITransactionScope transactionScope = _transactionHandler.BeginTransaction()) { try { var deletedInstanceIdentifiers = (await _indexDataStore .RetrieveDeletedInstancesAsync( _deletedInstanceCleanupConfiguration.BatchSize, _deletedInstanceCleanupConfiguration.MaxRetries, cancellationToken)) .ToList(); retrievedInstanceCount = deletedInstanceIdentifiers.Count; foreach (VersionedInstanceIdentifier deletedInstanceIdentifier in deletedInstanceIdentifiers) { try { Task[] tasks = new[] { _fileStore.DeleteFileIfExistsAsync(deletedInstanceIdentifier, cancellationToken), _metadataStore.DeleteInstanceMetadataIfExistsAsync(deletedInstanceIdentifier, cancellationToken), }; await Task.WhenAll(tasks); await _indexDataStore.DeleteDeletedInstanceAsync(deletedInstanceIdentifier, cancellationToken); } catch (Exception cleanupException) { try { int newRetryCount = await _indexDataStore.IncrementDeletedInstanceRetryAsync(deletedInstanceIdentifier, GenerateCleanupAfter(_deletedInstanceCleanupConfiguration.RetryBackOff), cancellationToken); if (newRetryCount > _deletedInstanceCleanupConfiguration.MaxRetries) { _logger.LogCritical(cleanupException, "Failed to cleanup instance {deletedInstanceIdentifier}. Retry count is now {newRetryCount} and retry will not be re-attempted.", deletedInstanceIdentifier, newRetryCount); } else { _logger.LogError(cleanupException, "Failed to cleanup instance {deletedInstanceIdentifier}. Retry count is now {newRetryCount}.", deletedInstanceIdentifier, newRetryCount); } } catch (Exception incrementException) { _logger.LogCritical(incrementException, "Failed to increment cleanup retry for instance {deletedInstanceIdentifier}.", deletedInstanceIdentifier); success = false; } } } transactionScope.Complete(); } catch (Exception retrieveException) { _logger.LogCritical(retrieveException, "Failed to retrieve instances to cleanup."); success = false; } } return(success, retrievedInstanceCount); }