public void AddWatch(string path) { ValidateArg.NotNullOrEmpty(path, "path"); if (!String.IsNullOrEmpty(path)) { var directoryName = Path.GetDirectoryName(path); var fileName = Path.GetFileName(path); FileWatcherInfo watcherInfo; if (!fileWatchers.TryGetValue(path, out watcherInfo)) { watcherInfo = new FileWatcherInfo(new FileSystemWatcher(directoryName, fileName)); if (fileWatchers.TryAdd(path, watcherInfo)) { watcherInfo.Watcher.Changed += OnChanged; // We are monitoring for this file to be renamed. // This is needed to catch file modifications in VS2013+. In these version VS won't update an existing file. // It will create a new file, and then delete old one and swap in the new one transactionally watcherInfo.Watcher.Renamed += OnRenamed; watcherInfo.Watcher.EnableRaisingEvents = true; } } } }
public void AddWatch(string path) { ValidateArg.NotNullOrEmpty(path, "path"); if (!String.IsNullOrEmpty(path)) { var directoryName = Path.GetDirectoryName(path); if (!Directory.Exists(directoryName)) return; var fileName = Path.GetFileName(path); FileWatcherInfo watcherInfo; if (!fileWatchers.TryGetValue(path, out watcherInfo)) { var watcher = new FileSystemWatcher(directoryName, fileName); watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcherInfo = new FileWatcherInfo(watcher); fileWatchers.Add(path, watcherInfo); watcherInfo.Watcher.Changed += OnChanged; watcherInfo.Watcher.Created += OnChanged; watcherInfo.Watcher.Deleted += OnChanged; watcherInfo.Watcher.Renamed += OnChanged; watcherInfo.Watcher.EnableRaisingEvents = true; } } }
private void OnFileCreated(object sender, FileWatcherInfo e) { if (e == null) { return; } Task.Run(() => TryDelete(e)); Task.Run(() => TryWriteJobMessage(e)); }
private void TryWriteJobMessage(FileWatcherInfo info) { void WriteJobMessage(dynamic profile, string appId) { string jobId = Path.GetFileNameWithoutExtension(info.Name); string data = Convert.ToBase64String(info.FileBytes); string directory = profile.System.Directory; string scopes = profile.System.Scopes; string jobMessage = "{\"auth\":{\"app_id\":\"" + appId + "\"},\"meta\":{\"id\":\"" + jobId + "\",\"scopes\":\"" + scopes + "\"},\"data\":\"" + data + "\"}"; string messageDir = profile.System.Directory; string path = Path.Combine(directory, jobId + ".json"); while (File.Exists(path)) { Logger.LogWarning("File already exists, wait 1 second and try again."); SpinWait.SpinUntil(() => false, 1000); } File.WriteAllText(path, jobMessage, new UTF8Encoding(false)); } try { string[] folders = info.DirectoryName.Split('\\'); if (folders.Count() < 3) { throw new ArgumentException(info.DirectoryName); } string[] reverseFolders = folders.Reverse().ToArray(); string scopes = reverseFolders[0].ToLower(); string appId = reverseFolders[1].ToLower(); Guard.AgainstNullAndEmpty(nameof(scopes), scopes); Guard.AgainstNullAndEmpty(nameof(appId), appId); var profiles = Profile.LoadProfile(appId, Const.PLUGIN_NAME, scopes); Parallel.ForEach(profiles, profile => { WriteJobMessage(profile, appId); }); } catch (AggregateException e) { foreach (var ie in e.InnerExceptions) { Logger.LogError(ie, ie.ToString()); } } catch (Exception e) { Logger.LogError(e, e.ToString()); } }
private void TryBackup(FileWatcherInfo info, string jobId) { try { string backupDir = Path.Combine(Const.DIRECTORY, DateTime.Now.ToString(Const.BACKUP_PATTERN)); Directory.CreateDirectory(backupDir); string backupPath = Path.Combine(backupDir, jobId + ".json"); File.Move(info.FullName, backupPath); } catch (Exception e) { Logger.LogError(e, e.ToString()); } }
public void AddWatch(string path) { ValidateArg.NotNullOrEmpty(path, "path"); if (!fileWatchers.ContainsKey(path)) { string directoryName = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); FileWatcherInfo watcherInfo = new FileWatcherInfo(new FileSystemWatcher(directoryName, fileName)); fileWatchers.Add(path, watcherInfo); watcherInfo.Watcher.Changed += OnChanged; watcherInfo.Watcher.Renamed += OnRenamed; watcherInfo.Watcher.EnableRaisingEvents = true; } }
private void OnFileCreated(object sender, FileWatcherInfo e) { if (OnMessageReceived == null) { return; } if (e == null) { return; } string jobId = Generator.RandomLongId; Task.Run(() => TryBackup(e, jobId)); Task.Run(() => TryPushMessage(e, jobId)); }
public static void StopFileWatch(string filter, string parentDirectory, FileSystemEventHandler callback) { FileWatcherInfo watcherInfo = _fileWatcherInfos.FirstOrDefault( info => info.FileWatcher.Filter == filter && info.FileWatcher.Path == parentDirectory); if (watcherInfo == null) { return; } watcherInfo.TryRemoveSubscriber(callback); if (!watcherInfo.HasSubscribers()) { _fileWatcherInfos.Remove(watcherInfo); } }
public static void StartFileWatch(string filter, string parentDirectory, FileSystemEventHandler callback) { FileWatcherInfo watcherInfo = null; foreach (FileWatcherInfo info in _fileWatcherInfos) { FileSystemWatcher watcher = info.FileWatcher; if (watcher.Filter != filter || watcher.Path != parentDirectory) { continue; } watcherInfo = info; // TODO REFACTOR (weakman) This assigned value is never used.... return; // ...because of this return, } if (watcherInfo == null) { // REFACTOR (weakman) , Which means this is currently always true... IOHelper.EnsureDirectoryExists(parentDirectory); watcherInfo = new FileWatcherInfo(new FileSystemWatcher(parentDirectory, filter), callback); InitializeWatcher(watcherInfo.FileWatcher); _fileWatcherInfos.Add(watcherInfo); } else { // REFACTOR (weakman) ...so this is never executed foreach (FileSystemEventHandler sub in watcherInfo.Subscribers) { if (sub == callback) { // already subscribed to this watcher return; } } watcherInfo.TryAddSubscriber(callback); } }
public void AddWatch(string path) { ValidateArg.NotNullOrEmpty(path, "path"); if (!String.IsNullOrEmpty(path)) { var directoryName = Path.GetDirectoryName(path); var fileName = Path.GetFileName(path); FileWatcherInfo watcherInfo; if (!fileWatchers.TryGetValue(path, out watcherInfo)) { watcherInfo = new FileWatcherInfo(new FileSystemWatcher(directoryName, fileName)); fileWatchers.Add(path, watcherInfo); watcherInfo.Watcher.Changed += OnChanged; watcherInfo.Watcher.EnableRaisingEvents = true; } } }
public static void StartFileWatch(string filter, string parentDirectory, FileSystemEventHandler callback) { FileWatcherInfo watcherInfo = null; foreach (var info in _fileWatcherInfos) { var watcher = info.FileWatcher; if (watcher.Filter == filter && watcher.Path == parentDirectory) { watcherInfo = info; return; } } if (watcherInfo == null) { IOHelper.EnsureDirectoryExists(parentDirectory); watcherInfo = new FileWatcherInfo(new FileSystemWatcher(parentDirectory, filter), callback); InitializeWatcher(watcherInfo.FileWatcher); _fileWatcherInfos.Add(watcherInfo); } else { foreach (var sub in watcherInfo.Subscribers) { if (sub == callback) { // already subscribed to this watcher return; } } watcherInfo.TryAddSubscriber(callback); } }
private void TryPushMessage(FileWatcherInfo info, string jobId) { try { var context = new Dictionary <string, string> { { WidgetConst.PMSG_ID, Path.GetFileNameWithoutExtension(info.Name) }, { WidgetConst.PMSG_JOBID, jobId }, { WidgetConst.PMSG_SOURCE, WidgetName }, }; Parallel.ForEach(OnMessageReceived.GetInvocationList(), t => { ((PipelineMessageEventHandler)t).BeginInvoke(info.FileBytes, context, null, null); }); } catch (AggregateException e) { foreach (var ie in e.InnerExceptions) { Logger.LogError(ie, ie.ToString()); } } catch (Exception e) { Logger.LogError(e, e.ToString()); } }
/// <summary> /// Registers a file to be watched</summary> /// <param name="filePath">Path of file to watch. It must exist.</param> public void Register(string filePath) { if (!m_watchers.ContainsKey(filePath)) { string directory = Path.GetDirectoryName(filePath); string filter = Path.GetFileName(filePath); DateTime lastWriteTime = File.GetLastWriteTimeUtc(filePath); var watcher = new FileSystemWatcher(directory, filter); watcher.SynchronizingObject = m_syncObject; watcher.Changed += watcher_Changed; watcher.Renamed += OnRenamed; var watcherInfo = new FileWatcherInfo { Watcher = watcher, LastWriteTime = lastWriteTime }; m_watchers.Add(filePath, watcherInfo); watcher.EnableRaisingEvents = true; } }