/// <copydocfrom cref="IFileProcessResultHandler.Handle" /> public void Handle(FileProcessResult fileEvent, ProcessingFile file) { if (!File.Exists(file.CurrentFilePath)) { // TODO: Log return; } switch (fileEvent) { case FileProcessResult.Cancelled: this.cancelledStrategy.Handle(file); break; case FileProcessResult.Error: this.failureStrategy.Handle(file); break; case FileProcessResult.Processed: this.successStrategy.Handle(file); break; default: throw new NotSupportedException("Unknown event: " + fileEvent); } }
/// <summary> /// Apply the strategies to the file. /// </summary> /// <param name="processingFile">File to use</param> public void Handle(ProcessingFile processingFile) { foreach (var strategy in this.strategies) { strategy.Handle(processingFile); } }
/// <summary> /// Delete a file that has been processed successfully. /// </summary> /// <param name="processingFile">File to process</param> public void Handle(ProcessingFile processingFile) { var file = new FileInfo(processingFile.CurrentFilePath); if (file.Exists) { file.Delete(); } const int MaxTries = 10; var tries = 0; Exception lastException; do { tries++; try { File.Delete(processingFile.CurrentFilePath); Logger.InfoFormat("Deleted: {0}", processingFile.CurrentFilePath); return; } catch (IOException ex) { lastException = ex; Logger.InfoFormat( "Failed deleting {0} - attempt {1}/{2}", processingFile.CurrentFilePath, tries, MaxTries); Thread.Sleep(1000); } }while (tries < MaxTries); throw new FileDeleteException(processingFile.CurrentFilePath, lastException); }
public void Notify(ProcessingFile processingFile) { this.ThrowIfDisposed(); var fileProcessorFullPath = this.fileProcessor as IFileHandlerFullPath; Task task; task = fileProcessorFullPath != null ? this.EnqueueWork(() => { if (!fileProcessorFullPath.HandleWithFullPath(new FileInfo(processingFile.CurrentFilePath), processingFile.FullPathOfOriginalFile)) { throw new InvalidOperationException("File was not handled HandleWithFullPath returned False."); } }) : this.EnqueueWork(() => { if (this.fileProcessor.Handle(new FileInfo(processingFile.CurrentFilePath), processingFile.OriginalFilePath)) { throw new InvalidOperationException("File was not handled Handle returned False."); } }); task.ContinueWith(x => this.FileProcessed(processingFile), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously); task.ContinueWith(x => this.FileCancelled(processingFile), TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously); task.ContinueWith(x => this.FileErrored(processingFile, x.Exception), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously); }
public void Notify(ProcessingFile processingFile) { this.ThrowIfDisposed(); var fileProcessorFullPath = this.fileProcessor as IFileHandlerFullPath; Task task; task = fileProcessorFullPath != null? this.EnqueueWork(() => { if (!fileProcessorFullPath.HandleWithFullPath(new FileInfo(processingFile.CurrentFilePath), processingFile.FullPathOfOriginalFile)) { throw new InvalidOperationException("File was not handled HandleWithFullPath returned False."); } }) : this.EnqueueWork(() => { if (this.fileProcessor.Handle(new FileInfo(processingFile.CurrentFilePath), processingFile.OriginalFilePath)) { throw new InvalidOperationException("File was not handled Handle returned False."); } }); task.ContinueWith(x => this.FileProcessed(processingFile), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously); task.ContinueWith(x => this.FileCancelled(processingFile), TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously); task.ContinueWith(x => this.FileErrored(processingFile, x.Exception), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously); }
/// <summary> /// Delete a file that has been processed successfully. /// </summary> /// <param name="processingFile">File to process</param> public void Handle(ProcessingFile processingFile) { var file = new FileInfo(processingFile.CurrentFilePath); if (file.Exists) { file.Delete(); } const int MaxTries = 10; var tries = 0; Exception lastException; do { tries++; try { File.Delete(processingFile.CurrentFilePath); Logger.InfoFormat("Deleted: {0}", processingFile.CurrentFilePath); return; } catch (IOException ex) { lastException = ex; Logger.InfoFormat( "Failed deleting {0} - attempt {1}/{2}", processingFile.CurrentFilePath, tries, MaxTries); Thread.Sleep(1000); } } while (tries < MaxTries); throw new FileDeleteException(processingFile.CurrentFilePath, lastException); }
public void HandleFileDoesNotExistDoNothing() { var handler = new DeleteSuccessfulFileHandlingStrategy(); var processingFile = new ProcessingFile(Path.Combine(testDirectory, "somenonexistant.file.inprogress"), "somenonexistant.file", "nonexistantpath\\nonexistant.file"); handler.Handle(processingFile); Assert.IsTrue(true); }
public void Notify(ProcessingFile processingFile) { this.ThrowIfDisposed(); this.timer.Stop(); this.fileHandler.Notify(processingFile); if (!this.stopAlerting) { this.timer.Start(); } }
public void HandleFileExistsDeleteFile() { var file = new FileInfo(Path.Combine(testDirectory, "test.file")); var processingFile = new ProcessingFile(file.FullName, "original.name", "originalpath\\original.name"); using (var sr = file.Create()) { } var handler = new DeleteSuccessfulFileHandlingStrategy(); handler.Handle(processingFile); Assert.IsFalse(file.Exists); }
/// <summary> /// Move a file into a directory. /// </summary> /// <param name="processingFile">File to move</param> public void Handle(ProcessingFile processingFile) { var destinationDir = targetDirectory; if (targetDirectory.Contains("%filepath%")) { destinationDir = targetDirectory.Replace("%filepath%", Path.GetDirectoryName(processingFile.FullPathOfOriginalFile)); } Logger.DebugFormat("Moving {0} to {1}", processingFile.OriginalFilePath, destinationDir); string newFilePath = Path.Combine(destinationDir, processingFile.OriginalFilePath); var tries = 0; Exception lastException; do { tries++; try { File.Move(processingFile.CurrentFilePath, newFilePath); Logger.InfoFormat("{0} moved to {1}", processingFile.OriginalFilePath, destinationDir); return; } catch (IOException ex) { lastException = ex; if (ex.Message.ToLowerInvariant().Contains("already exists")) { Logger.Warn(string.Format("Unable to move file to {0}", newFilePath), ex); return; } Logger.DebugFormat( "Failed moving {0} to {1} - attempt {2}/{3}", processingFile.OriginalFilePath, destinationDir, tries, MaxTries); Thread.Sleep(500); } }while (tries < MaxTries); Logger.Warn(string.Format("Failed moving {0} to {1}", processingFile.OriginalFilePath, destinationDir), lastException); }
/// <summary> /// Move a file into a directory. /// </summary> /// <param name="processingFile">File to move</param> public void Handle(ProcessingFile processingFile) { var destinationDir = targetDirectory; if (targetDirectory.Contains("%filepath%")) { destinationDir = targetDirectory.Replace("%filepath%", Path.GetDirectoryName(processingFile.FullPathOfOriginalFile)); } Logger.DebugFormat("Moving {0} to {1}", processingFile.OriginalFilePath, destinationDir); string newFilePath = Path.Combine(destinationDir, processingFile.OriginalFilePath); var tries = 0; Exception lastException; do { tries++; try { File.Move(processingFile.CurrentFilePath, newFilePath); Logger.InfoFormat("{0} moved to {1}", processingFile.OriginalFilePath, destinationDir); return; } catch (IOException ex) { lastException = ex; if (ex.Message.ToLowerInvariant().Contains("already exists")) { Logger.Warn(string.Format("Unable to move file to {0}", newFilePath), ex); return; } Logger.DebugFormat( "Failed moving {0} to {1} - attempt {2}/{3}", processingFile.OriginalFilePath, destinationDir, tries, MaxTries); Thread.Sleep(500); } } while (tries < MaxTries); Logger.Warn(string.Format("Failed moving {0} to {1}", processingFile.OriginalFilePath, destinationDir), lastException); }
private void FileErrored(ProcessingFile processingFile, AggregateException exception) { Logger.Error(string.Format("File processing failed: {0}", processingFile.OriginalFilePath), exception.Flatten()); this.processResultHandler.Handle(FileProcessResult.Error, processingFile); }
private void FileCancelled(ProcessingFile processingFile) { Logger.Debug(string.Format("File processing cancelled: {0}", processingFile.OriginalFilePath)); this.processResultHandler.Handle(FileProcessResult.Cancelled, processingFile); }
private void FileProcessed(ProcessingFile processingFile) { Logger.Debug(string.Format("File processed successfully: {0}", processingFile.OriginalFilePath)); this.processResultHandler.Handle(FileProcessResult.Processed, processingFile); }
public void FileProcessed(ProcessingFile processingFile) { this.Handle(FileProcessResult.Processed, processingFile); }
public void FileErrored(ProcessingFile processingFile) { this.Handle(FileProcessResult.Error, processingFile); }
public void FileCancelled(ProcessingFile processingFile) { this.Handle(FileProcessResult.Cancelled, processingFile); }