public override TaskStatus Run() { InfoFormat("Watching the folder {0} ...", FolderToWatch); try { if (!Directory.Exists(FolderToWatch)) { ErrorFormat("The folder {0} does not exist.", FolderToWatch); return(new TaskStatus(Status.Error)); } Info("Checking existing files..."); var files = GetFiles(); foreach (var file in files) { InfoFormat("FileSystemWatcher.OnFound started for {0}", file); try { if (ContinueDoTasksOnlyOnNotOpenFiles && FileChange.IsFileLocked(file)) { continue; } ClearFiles(); Files.Add(new FileInf(file, Id)); var tasks = GetTasks(OnFileFound); foreach (var task in tasks) { task.Logs.Clear(); task.Run(); CurrentLogs.AddRange(task.Logs); } Files.RemoveAll(f => f.Path == file); } catch (IOException ex) when((ex.HResult & 0x0000FFFF) == 32) { Logger.InfoFormat("There is a sharing violation for the file {0}.", file); } catch (Exception ex) { ErrorFormat("An error while triggering FileSystemWatcher.OnFound on the file {0}. Message: {1}", file, ex.Message); } finally { Info("FileSystemWatcher.OnFound finished."); } try { var entry = Workflow.Database.GetEntry(Workflow.Id, Workflow.InstanceId); entry.Logs = string.Join("\r\n", CurrentLogs); Workflow.Database.UpdateEntry(entry.GetDbId(), entry); } catch (Exception ex) { ErrorFormat("An error while updating FileSystemWatcher.OnCreated database entry.", ex); } } Info("Checking existing files finished."); Info("Initializing PollingFileSystemWatcher..."); Watcher = new PollingFileSystemWatcher(FolderToWatch, Filter, new EnumerationOptions { RecurseSubdirectories = IncludeSubFolders }); // Add event handlers. Watcher.ChangedDetailed += OnChanged; // Begin watching. Watcher.Start(); InfoFormat("PollingFileSystemWatcher.Path={0}", Watcher.Path); InfoFormat("PollingFileSystemWatcher.Filter={0}", Watcher.Filter); Info("PollingFileSystemWatcher Initialized."); Info("Begin watching ..."); CurrentLogs.AddRange(Logs); while (!IsStopped) { Thread.Sleep(1); } Watcher.Dispose(); } catch (ThreadAbortException) { if (Watcher != null) { Watcher.Dispose(); } throw; } catch (Exception e) { if (Watcher != null) { Watcher.Dispose(); } ErrorFormat("An error occured while watching the folder {0}. Error: {1}", FolderToWatch, e.Message); return(new TaskStatus(Status.Error, false)); } Info("Task finished"); return(new TaskStatus(Status.Success)); }
private void OnChanged(object source, PollingFileSystemEventArgs e) { foreach (var change in e.Changes) { var path = Path.Combine(change.Directory, change.Name); switch (change.ChangeType) { case WatcherChangeTypes.Created: Info("PollingFileSystemWatcher.OnCreated started."); try { if (SafeMode && FileChange.IsFileLocked(path)) { Info($"File lock detected on file {path}"); while (FileChange.IsFileLocked(path)) { Thread.Sleep(1000); } } ClearFiles(); Files.Add(new FileInf(path, Id)); var tasks = GetTasks(OnFileCreated); foreach (var task in tasks) { task.Logs.Clear(); task.Run(); CurrentLogs.AddRange(task.Logs); } Files.RemoveAll(f => f.Path == path); } catch (IOException ex) when((ex.HResult & 0x0000FFFF) == 32) { Logger.InfoFormat("There is a sharing violation for the file {0}.", path); } catch (Exception ex) { ErrorFormat("An error while triggering PollingFileSystemWatcher.OnCreated on the file {0}. Message: {1}", path, ex.Message); } Info("PollingFileSystemWatcher.OnCreated finished."); try { var entry = Workflow.Database.GetEntry(Workflow.Id, Workflow.InstanceId); entry.Logs = string.Join("\r\n", CurrentLogs); Workflow.Database.UpdateEntry(entry.GetDbId(), entry); } catch (Exception ex) { ErrorFormat("An error while updating PollingFileSystemWatcher.OnCreated database entry.", ex); } break; case WatcherChangeTypes.Changed: Info("PollingFileSystemWatcher.OnChanged started."); try { if (SafeMode && FileChange.IsFileLocked(path)) { Info($"File lock detected on file {path}"); while (FileChange.IsFileLocked(path)) { Thread.Sleep(1000); } } ClearFiles(); Files.Add(new FileInf(path, Id)); var tasks = GetTasks(OnFileChanged); foreach (var task in tasks) { task.Logs.Clear(); task.Run(); CurrentLogs.AddRange(task.Logs); } Files.RemoveAll(f => f.Path == path); } catch (IOException ex) when((ex.HResult & 0x0000FFFF) == 32) { Logger.InfoFormat("There is a sharing violation for the file {0}.", path); } catch (Exception ex) { ErrorFormat("An error while triggering PollingFileSystemWatcher.OnChanged on the file {0}. Message: {1}", path, ex.Message); } Info("PollingFileSystemWatcher.OnChanged finished."); try { var entry = Workflow.Database.GetEntry(Workflow.Id, Workflow.InstanceId); entry.Logs = string.Join("\r\n", CurrentLogs); Workflow.Database.UpdateEntry(entry.GetDbId(), entry); } catch (Exception ex) { ErrorFormat("An error while updating PollingFileSystemWatcher.OnChanged database entry.", ex); } break; case WatcherChangeTypes.Deleted: Info("PollingFileSystemWatcher.OnDeleted started."); try { ClearFiles(); Files.Add(new FileInf(path, Id)); var tasks = GetTasks(OnFileDeleted); foreach (var task in tasks) { task.Logs.Clear(); task.Run(); CurrentLogs.AddRange(task.Logs); } Files.RemoveAll(f => f.Path == path); } catch (IOException ex) when((ex.HResult & 0x0000FFFF) == 32) { Logger.InfoFormat("There is a sharing violation for the file {0}.", path); } catch (Exception ex) { ErrorFormat("An error while triggering PollingFileSystemWatcher.OnDeleted on the file {0}. Message: {1}", path, ex.Message); } Info("PollingFileSystemWatcher.OnDeleted finished."); try { var entry = Workflow.Database.GetEntry(Workflow.Id, Workflow.InstanceId); entry.Logs = string.Join("\r\n", CurrentLogs); Workflow.Database.UpdateEntry(entry.GetDbId(), entry); } catch (Exception ex) { ErrorFormat("An error while updating PollingFileSystemWatcher.OnDeleted database entry.", ex); } break; } } }