private IEnumerable <IDomainEvent> PerformIntegrityLoad(EventStoreSubscriptionHandler eventStoreSubscriptionCallback) { Register(eventStoreSubscriptionCallback); return(_maybeEventStoreSubscriptionHandler .Unwrap( PerformIntegrityLoadInternal, () => throw new InvalidOperationException($"Integrity load can't be performed if '{nameof(EventStoreSubscriptionHandler)} is not registered.'"))); }
private IEnumerable <IDomainEvent> PerformIntegrityLoadInternal(EventStoreSubscriptionHandler callback) { _isIntegrityLoadPerformed = true; foreach (var e in _mongoDbReader.LoadAll()) { yield return(e); } _domainEventAggregator.StopAggregation(callback); }
private Nothing WhenThereIsNoWorkerThreadStartIt(EventStoreSubscriptionHandler eventStoreSubscriptionHandler) { var t = new Thread(() => { while (true) { try { eventStoreSubscriptionHandler(_aggregatedEvents.Take(_cancellationTokenSource.Token)); } catch (OperationCanceledException) { break; } } }); t.Start(); _maybeWorkerThread = t; return(NotAtAll); }
public Nothing Register <T>(EventStoreSubscriptionHandler callback) where T : IAggregateEventSubscription, new() { var aggregateEventSubscription = new T(); _channel.ExchangeDeclare(aggregateEventSubscription.AggregateTopicName, "topic", true); var queueName = _channel.QueueDeclare().QueueName; _channel.QueueBind(queueName, aggregateEventSubscription.AggregateTopicName, ""); var consumer = new EventingBasicConsumer(_channel); consumer.Received += (model, ea) => { var messageAsString = Encoding.ASCII.GetString(ea.Body); messageAsString.Deserialize() .OnSuccess(message => callback(message as IDomainEvent)) .OnFailure(error => Console.WriteLine($"Failed to deserialize received message: {error}")); }; _channel.BasicConsume(queueName, true, consumer); return(NotAtAll); }
public Nothing StopAggregation(EventStoreSubscriptionHandler eventStoreSubscriptionHandler) => _maybeWorkerThread .Unwrap( WhenThereIsWorkerThreadJustIgnore, () => WhenThereIsNoWorkerThreadStartIt(eventStoreSubscriptionHandler));
private Nothing Register(EventStoreSubscriptionHandler callback) { _maybeEventStoreSubscriptionHandler = callback; _rabbitMqSubscriber.Register <T>(callback); return(NotAtAll); }
public IEnumerable <IDomainEvent> IntegrityLoadEvents(EventStoreSubscriptionHandler eventStoreSubscriptionCallback) => _isIntegrityLoadPerformed .OnBoth( ThrowInvalidOperationException, () => PerformIntegrityLoad(eventStoreSubscriptionCallback));