/// <summary> /// Constructs a new instance. /// </summary> /// <param name="context">The <see cref="SimpleMessageBusFileProcessorFactoryContext"/> to use.</param> public SimpleMessageBusFileProcessor(SimpleMessageBusFileProcessorFactoryContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } _options = context.Options; _attribute = context.Attribute; _executor = context.Executor; _logger = context.Logger; //RWM: Use reflection because _attribute.GetRootPath() is internal. var dynMethod = _attribute.GetType().GetMethod("GetRootPath", BindingFlags.NonPublic | BindingFlags.Instance); var attributePath = dynMethod.Invoke(_attribute, null); _filePath = Path.Combine(_options.RootFolder, attributePath.ToString()); var settings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, }; _serializer = JsonSerializer.Create(settings); }
public async Task StartAsync(CancellationToken cancellationToken) { ThrowIfDisposed(); if (_watcher != null && _watcher.EnableRaisingEvents) { throw new InvalidOperationException("The listener has already been started."); } CreateFileWatcher(); var context = new SimpleMessageBusFileProcessorFactoryContext(_options.Value, _attribute, _triggerExecutor, _logger); _processor = _fileProcessorFactory.CreateFileProcessor(context); var options = new ExecutionDataflowBlockOptions { BoundedCapacity = _processor.MaxQueueSize, MaxDegreeOfParallelism = _processor.MaxDegreeOfParallelism, }; _workQueue = new ActionBlock <FileSystemEventArgs>(async(e) => await ProcessWorkItem(e), options); // on startup, process any preexisting files that haven't been processed yet ProcessFiles(); // Create a timer to cleanup processed files. // The timer doesn't auto-reset. It resets itself as files // are completed. // We start the timer on startup so we have at least one // cleanup pass _cleanupTimer = new System.Timers.Timer() { AutoReset = false, Interval = _rand.Next(5 * 1000, 8 * 1000) }; _cleanupTimer.Elapsed += OnCleanupTimer; _cleanupTimer.Start(); await Task.FromResult <bool>(true); }