Exemplo n.º 1
0
        public void StartWatchFolder(FileInfo configFileInfo, params string[] topicMessages)
        {
            if (configFileInfo == null)
            {
                throw new ArgumentNullException(nameof(configFileInfo));
            }

            if (!IsFolder(configFileInfo.FullName))
            {
                throw new ArgumentException("The configuration folder is not a real folder");
            }

            FileSystemWatcher watcher = new FileSystemWatcher
            {
                Path         = configFileInfo.FullName,
                NotifyFilter =
                    NotifyFilters.LastAccess |
                    NotifyFilters.LastWrite |
                    NotifyFilters.FileName |
                    NotifyFilters.DirectoryName,
                Filter = configFileInfo.Extension,
                EnableRaisingEvents = true,
            };

            observableFileSystemWatcher.SetFile(watcher);

            configWatcherDisposable.Disposable = observableFileSystemWatcher.Changed.SubscribeOn(
                rxSchedulerService.TaskPool).Throttle(TimeSpan.FromMilliseconds(500)).Subscribe(
                async x =>
            {
                await this.fileProcessingHandler.ProcessingMessageAsync(configFileInfo, topicMessages);
            },
                ex =>
            {
                logger.Info($"Error encountered attempting to read new config data from config file {configFileInfo.Name}");
            },
                () =>
            {
                logger.Info($"Finish processing file {configFileInfo.Name}");
            });
        }
Exemplo n.º 2
0
        private void CreateConfigWatcher(FileInfo configFileInfo)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path         = configFileInfo.DirectoryName;
            watcher.NotifyFilter =
                NotifyFilters.LastAccess |
                NotifyFilters.LastWrite |
                NotifyFilters.FileName |
                NotifyFilters.DirectoryName;
            watcher.Filter = configFileInfo.Name;
            _observableFileSystemWatcher.SetFile(watcher);
            //FSW is notorious for firing twice see here :
            //http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice
            //so lets use Rx to Throttle it a bit
            _configWatcherDisposable.Disposable = _observableFileSystemWatcher.Changed.SubscribeOn(
                _rxSchedulerService.TaskPool).Throttle(TimeSpan.FromMilliseconds(500)).Subscribe(
                async x =>
            {
                //at this point the config has changed, start a critical section
                using (var releaser = await _lock.LockAsync())
                {
                    //tell current scheduled job that we need to read new config, and wait for it
                    //to signal us that we may continue
                    _log.Log($"Config file {configFileInfo.Name} has changed, attempting to read new config data");
                    _schedulingAssistanceService.RequiresNewSchedulerSetup = true;
                    _schedulingAssistanceService.SchedulerRestartGate.WaitAsync().GetAwaiter().GetResult();
                    //recreate the AzureBlobConfiguration, and recreate the scheduler using new settings
                    ConfigurationManager.RefreshSection("schedulingConfiguration");
                    var newSchedulingConfiguration = SimpleConfig.Configuration.Load <SchedulingConfiguration>();
                    _log.Log($"SchedulingConfiguration section is now : {newSchedulingConfiguration}");
                    ContainerOperations.ReInitialiseSchedulingConfiguration(newSchedulingConfiguration);
                    ReScheduleJob();
                }
            },
                ex =>
            {
                _log.Log($"Error encountered attempting to read new config data from config file {configFileInfo.Name}");
            });
        }