public void Execute(Fileset fileset)
        {
            _activityLogService.Log(fileset.Id, "Executing manual copy");

            foreach (var item in fileset.Includes)
            {
                var fileName = Path.GetFileName(item.RelativePath);
                var filePath = FilesetItem.GetFullPath(fileset.SourcePath, item.RelativePath);

                var replicationItem = new ReplicationItem
                {
                    Fileset             = fileset,
                    FileName            = fileName,
                    FullSourcePath      = filePath,
                    FullDestinationPath = Path.Combine(fileset.DestinationPath, fileName)
                };
                replicationItem.Data.LockingProcesses = new string[0];
                _replicationQueue.TryAdd(replicationItem);
            }

            if (fileset.Excludes.Any())
            {
                var relativePath  = fileset.Excludes.FirstOrDefault().RelativePath;
                var fullPath      = FilesetItem.GetFullPath(fileset.SourcePath, relativePath);
                var directoryName = Path.GetDirectoryName(fullPath);

                if (!string.IsNullOrEmpty(directoryName))
                {
                    IEnumerable <string> files = Directory.GetFiles(directoryName);
                    files = files.Except(fileset.Excludes.Select(f =>
                    {
                        return(FilesetItem.GetFullPath(fileset.SourcePath, f.RelativePath));
                    }));

                    foreach (var file in files)
                    {
                        var fileName        = Path.GetFileName(file);
                        var replicationItem = new ReplicationItem
                        {
                            Fileset             = fileset,
                            FileName            = fileName,
                            FullSourcePath      = file,
                            FullDestinationPath = Path.Combine(fileset.DestinationPath, fileName)
                        };
                        replicationItem.Data.LockingProcesses = new string[0];
                        _replicationQueue.TryAdd(replicationItem);
                    }
                }
            }
        }
        private void ActivateFileset(Fileset fileset)
        {
            List <IDisposable> watchers;

            if (!_watchers.TryGetValue(fileset, out watchers))
            {
                watchers = new List <IDisposable>();
                _watchers.TryAdd(fileset, watchers);
            }

            DeactivateFileset(fileset);

            var distinctIncludeFolders = fileset.Includes.Select(f =>
            {
                var fullPath = FilesetItem.GetFullPath(fileset.SourcePath, f.RelativePath);
                return(new FileInfo(fullPath).DirectoryName);
            }).Distinct();

            var distinctExcludeFolders = fileset.Excludes.Select(f =>
            {
                var fullPath = FilesetItem.GetFullPath(fileset.SourcePath, f.RelativePath);
                return(new FileInfo(fullPath).DirectoryName);
            }).Distinct();

            var foldersToWatch = distinctIncludeFolders.Union(distinctExcludeFolders);

            foreach (var folderToWatch in foldersToWatch)
            {
                if (Directory.Exists(folderToWatch))
                {
                    IDisposable observable = GetObservable(folderToWatch)
                                             .Buffer(TimeSpan.FromSeconds(2))
                                             .Subscribe(t =>
                    {
                        if (t.Any())
                        {
                            var fileNames = t.Distinct();

                            foreach (var filePath in fileNames)
                            {
                                var relativePath = FilesetItem.GetRelativePath(fileset.SourcePath, filePath);

                                if (fileset.Includes.Any(f => f.RelativePath == relativePath))
                                {
                                    var fileName        = Path.GetFileName(filePath);
                                    var replicationItem = new ReplicationItem
                                    {
                                        Fileset             = fileset,
                                        FileName            = fileName,
                                        FullSourcePath      = filePath,
                                        FullDestinationPath = Path.Combine(fileset.DestinationPath, fileName)
                                    };
                                    replicationItem.Data.LockingProcesses = new string[0];
                                    _replicationQueue.TryAdd(replicationItem);
                                }

                                if (fileset.Excludes.Any())
                                {
                                    string directoryName = Path.GetDirectoryName(filePath);

                                    if (!string.IsNullOrEmpty(directoryName))
                                    {
                                        IEnumerable <string> files = Directory.GetFiles(directoryName);
                                        files = files.Except(fileset.Excludes.Select(f =>
                                        {
                                            return(FilesetItem.GetFullPath(fileset.SourcePath, f.RelativePath));
                                        }));

                                        foreach (var file in files)
                                        {
                                            var fileName        = Path.GetFileName(file);
                                            var replicationItem = new ReplicationItem
                                            {
                                                Fileset             = fileset,
                                                FileName            = fileName,
                                                FullSourcePath      = filePath,
                                                FullDestinationPath = Path.Combine(fileset.DestinationPath, fileName)
                                            };
                                            replicationItem.Data.LockingProcesses = new string[0];

                                            _replicationQueue.TryAdd(replicationItem);
                                        }
                                    }
                                }
                            }
                        }
                    });

                    watchers.Add(observable);
                }
            }
        }