public void Start(IEventConsumer <FileSystemEventArgs> eventConsumer) { log.Info("Starting event provider..."); log.InfoFormat("Source: '{0}'", this.Configuration.sourceDir); log.InfoFormat("Target: '{0}'", this.Configuration.targetDir); log.InfoFormat("Command: {0} {1}", this.Configuration.appName, this.Configuration.appArguments); log.InfoFormat("Ignoring: {0}", this.Configuration.ignores != null ? "\n" + string.Join("\n", this.Configuration.ignores) : string.Empty); log.InfoFormat("After app kill delay: {0} miliseconds", this.Configuration.afterAppKillDelay); log.InfoFormat("Event throttling delay: {0} miliseconds", this.Configuration.eventThrottlingDelay); // wait till source dir exists (just for user conveniance) while (!Directory.Exists(this.Configuration.sourceDir)) { log.InfoFormat("Source dir '{0}' does not exist, waiting till it does...", this.Configuration.sourceDir); Thread.Sleep(TimeSpan.FromSeconds(3)); } log.InfoFormat("Source dir '{0}' detected, running...", this.Configuration.sourceDir); var fsw = new FileSystemWatcher(this.Configuration.sourceDir); fsw.Changed += (sender, args) => this.DoEvent(args); fsw.Created += (sender, args) => this.DoEvent(args); fsw.Deleted += (sender, args) => this.DoEvent(args); fsw.Renamed += (sender, args) => this.DoEvent(args); this.Rs .Where(e => !this.ShouldBeIgnored(e, this.Configuration.ignores, this.Configuration.sourceDir)) .Throttle(TimeSpan.FromMilliseconds(this.Configuration.eventThrottlingDelay)) .Subscribe(eventArgs => eventConsumer.Consume(eventArgs)); // do an initial start eventConsumer.Consume(null); log.InfoFormat("Watching source for changes..."); fsw.EnableRaisingEvents = true; // no need for that, the host or it's controller should ensure that we are not ending the process ////while (true) ////{ //// Thread.Sleep(TimeSpan.FromSeconds(1)); ////} }
public void Subscribe <T>(IEventConsumer <T> consumer) where T : DomainEvent { var eventType = typeof(T); var publishEvent = _consumersMap.TryGetValue(eventType).IfNone(() => EmptyPublishEventAction); publishEvent += @event => { var typedEvent = (T)@event; consumer.Consume(typedEvent); }; _consumersMap[eventType] = publishEvent; }
public void RegisterConsumer <T>(IEventConsumer <T> consumer) where T : EventInfoBase { var queue = _bus.QueueDeclare(GetQueueNameForConsumer(consumer)); var routingKey = GetRoutingKeyForEvent <T>(); _bus.Bind(_mainExchange, queue, routingKey); _bus.Consume <T>(queue, (message, info) => Task.Factory.StartNew(() => { try { consumer.Consume(message.Body); } catch (Exception ex) { throw new Exception(ex.Message); } } )); }
public async Task PublishAsync(object message) { await _eventConsumer.Consume(message); await _messagePublisher.PublishAsync(message); }