private void BackgroundProcessing(CancellationToken stoppingToken) { _logger.Log(LogLevel.Information, "Disk Space Reclaimer Hosted Service is running."); while (!stoppingToken.IsCancellationRequested) { _logger.Log(LogLevel.Debug, "Waiting for instance..."); var filePath = _taskQueue.Dequeue(stoppingToken); if (filePath == null) { continue; // likely canceled } Policy.Handle <Exception>() .WaitAndRetry( 3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, retryCount, context) => { _logger.Log(LogLevel.Error, exception, $"Error occurred deleting file {filePath} on {retryCount} retry."); }) .Execute(() => { _logger.Log(LogLevel.Debug, "Deleting file {0}", filePath); if (_fileSystem.File.Exists(filePath)) { _fileSystem.File.Delete(filePath); _logger.Log(LogLevel.Debug, "File deleted {0}", filePath); } }); } _logger.Log(LogLevel.Information, "Cancellation requested."); }
private void BackgroundProcessing(CancellationToken stoppingToken) { _logger.Log(LogLevel.Information, "Disk Space Reclaimer Hosted Service is running."); while (!stoppingToken.IsCancellationRequested) { _logger.Log(LogLevel.Debug, "Waiting for instance..."); var workItem = _taskQueue.Dequeue(stoppingToken); if (workItem == null) { continue; // likely cancelled } try { _logger.Log(LogLevel.Debug, "Deleting file {0}", workItem.InstanceStorageFullPath); if (_fileSystem.File.Exists(workItem.InstanceStorageFullPath)) { _fileSystem.File.Delete(workItem.InstanceStorageFullPath); _logger.Log(LogLevel.Debug, "File deleted {0}", workItem.InstanceStorageFullPath); } } catch (Exception ex) { _logger.Log(LogLevel.Error, ex, $"Error occurred deleting file {workItem.InstanceStorageFullPath}."); } } _logger.Log(LogLevel.Information, "Cancellation requested."); }