Ejemplo n.º 1
0
 private void HandleEvent(FileEvent fileEvent)
 {
     Console.WriteLine($"Handling {fileEvent}.");
     using (var ctx = new FilesContext())
     {
         if (fileEvent.Type == EventType.Deleted)
         {
             Console.WriteLine($"Performing delete on {fileEvent.FullPath}");
             _changedCache.Remove(fileEvent.FullPath, CacheEntryRemovedReason.Removed);
             ctx.DeleteFile(fileEvent.FullPath, watcher);
             fsWatcher.SendChangeOccurred();
         }
         else if (fileEvent.Type == EventType.Renamed)
         {
             HandleRenamed(fileEvent, ctx);
             fsWatcher.SendChangeOccurred();
         }
         else if (fileEvent.Type == EventType.Created)
         {
             HandleCreated(fileEvent, ctx);
             fsWatcher.SendChangeOccurred();
         }
         else if (fileEvent.Type == EventType.Changed)
         {
             HandleChanged(fileEvent, ctx);
         }
         else if (fileEvent.Type == EventType.Error)
         {
             throw fileEvent.Error.GetException();
         }
     }
 }
Ejemplo n.º 2
0
        public void RestartById(int id)
        {
            FSWatcherTaskPack pack;

            if (watchers.TryGetValue(id, out pack))
            {
                lock (pack)
                {
                    pack.FSWatcher.Stop();
                    try
                    {
                        pack.Task.Wait();
                    }
                    catch (Exception)
                    {
                        // Pass... TODO: filter only useful expcetions
                    }
                }
            }
            watchers.Remove(id);

            Watcher found = null;

            using (var ctx = new FilesContext())
            {
                found = ctx.Watchers.Find(id);
            }
            if (found == null)
            {
                return;
            }
            AddWatcher(found);
        }
Ejemplo n.º 3
0
 private void HandleChanged(FileEvent fileEvent, FilesContext ctx)
 {
     Console.WriteLine($"File: {fileEvent.FullPath} changed.");
     if (!Directory.Exists(fileEvent.FullPath) && watcher.WhitelistedFile(fileEvent.FullPath))
     {
         _changedCache.AddOrGetExisting(fileEvent.FullPath, fileEvent, ChangeCachePolicy());
         //ctx.UpdateFile(fileEvent.FullPath, watcher);
     }
 }
Ejemplo n.º 4
0
        private void TriggerChange(CacheEntryRemovedArguments args)
        {
            var fileEvent = (FileEvent)args.CacheItem.Value;

            using (var ctx = new FilesContext())
            {
                Console.WriteLine("Triggered cached file.");
                ctx.UpdateFile(fileEvent.FullPath, watcher);
            }
        }
Ejemplo n.º 5
0
 private void HandleCreated(FileEvent fileEvent, FilesContext ctx)
 {
     Console.WriteLine($"File: {fileEvent.FullPath} created event receieved.");
     if (!Directory.Exists(fileEvent.FullPath) && watcher.WhitelistedFile(fileEvent.FullPath))
     {
         ctx.AddFile(fileEvent.FullPath, watcher);
     }
     else if (Directory.Exists(fileEvent.FullPath))
     {
         ctx.SyncCreatedDirectory(watcher, fileEvent.FullPath);
     }
 }
Ejemplo n.º 6
0
 private void HandleRenamed(FileEvent fileEvent, FilesContext ctx)
 {
     Console.WriteLine($"File: {fileEvent.OldFullPath} renamed to {fileEvent.FullPath}");
     if (!Directory.Exists(fileEvent.FullPath) && watcher.WhitelistedFile(fileEvent.FullPath))
     {
         _changedCache.Remove(fileEvent.FullPath, CacheEntryRemovedReason.Removed);
         ctx.RenameFile(fileEvent.OldFullPath, fileEvent.FullPath, watcher);
     }
     else if (Directory.Exists(fileEvent.FullPath))
     {
         ctx.RenameFilesInDirectory(fileEvent.OldFullPath, fileEvent.FullPath, watcher);
     }
 }
Ejemplo n.º 7
0
        public static WatcherMonitor InitMonitorFromDB()
        {
            var monitor = new WatcherMonitor();

            using (var ctx = new FilesContext())
            {
                foreach (var watcher in ctx.Watchers.ToList())
                {
                    monitor.AddWatcher(watcher);
                }
            }
            return(monitor);
        }
Ejemplo n.º 8
0
 public void Sync(bool updateThumbnails = true)
 {
     // Add timeout?
     syncMutex.WaitOne();
     try
     {
         using (var ctx = new FilesContext())
         {
             var changed = ctx.Sync(Watcher, updateThumbnails);
             OnSyncOccurredEvent(new SyncOccurredEventArgs {
                 Changed = changed, WatcherId = Watcher.Id
             });
             syncTimer.Reset();
         }
     } catch (Exception e)
     {
         ForceStop(e);
     }
     finally
     {
         syncMutex.ReleaseMutex();
     }
 }