Beispiel #1
0
        private void HandleEvent(EventStoreCatchUpSubscription arg1, ResolvedEvent arg2)
        {
            var @event = SerializationUtils.DeserializeEvent(arg2.OriginalEvent);

            if (@event == null)
            {
                return;
            }
            var eventType = @event.GetType();

            if (@event is IMessage)
            {
                if (@event is IEvent)
                {
                    _domainEntry.Publish(@event as IEvent);
                }
                if (@event is ICommand)
                {
                    _domainEntry.Send(@event as ICommand);
                }
            }
            _latestPosition = arg2.OriginalPosition;

            if (_latestPosition.HasValue && _domainEntry.CanHandle(eventType))
            {
                _checkpointRepository.Save(_latestPosition.Value);
            }
        }
Beispiel #2
0
        public async Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent @event)
        {
            CurrentCheckpoint = @event.OriginalEventNumber;
            var ev = TryDeserializeEvent(@event.Event.Metadata, @event.Event.Data);

            await EventAppearedCallback(ev, @event.OriginalEventNumber + 1);
        }
Beispiel #3
0
        private Task GotEvent(EventStoreCatchUpSubscription sub, ResolvedEvent evt)
        {
            try {
                lock (_updateLock) {
                    if (evt.Event.Data.Length <= 0 || !evt.Event.IsJson || evt.Event.EventType.StartsWith("$") || evt.IsResolved)
                    {
                        return(Task.CompletedTask);
                    }
                    var e = _deserializer(evt);
                    if (e is IEvent message)
                    {
                        Apply(message);
                    }
                    // ReSharper disable once PossibleInvalidOperationException
                    // this always has a value here
                    _checkPoint = evt.OriginalPosition.Value;
                }
            }
            catch (Exception e) {
                //Console.WriteLine(e);
            }
            if (_isLive)
            {
                UpdateSubscribers();
            }

            return(Task.CompletedTask);
        }
        private void OnSubscriptionDropped(EventStoreCatchUpSubscription catchUpSubscription, SubscriptionDropReason dropReason, Exception exception)
        {
            string innerExceptionMessage = string.Empty;

            if (exception != null && exception.InnerException != null)
            {
                innerExceptionMessage = string.Format(" ({0})", exception.InnerException.Message);
            }

            // logProvider.Log("Subscription dropped (reason: " + SubscriptionDropReasonText(dropReason) + innerExceptionMessage + ")", LogMessageLevel.Info);

            if (dropReason == SubscriptionDropReason.ProcessingQueueOverflow)
            {
                // This happens when the server detects that _liveQueue.Count >= MaxPushQueueSize which defaults to 10,000
                // In the forum James Nugent suggests "Wait and reconnect probably with back off" https://gist.github.com/jen20/6092666

                // For now we will just re-subscribe
                InitialiseCatchUpSubscription();
            }

            if (SubscriptionDropMayBeRecoverable(dropReason))
            {
                InitialiseCatchUpSubscription();
            }
        }
Beispiel #5
0
        private async void EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent)
        {
            this.logger.Info("Processed event: " + resolvedEvent.OriginalPosition + "/" + resolvedEvent.OriginalEventNumber.ToString());

            await EventStoreEventLogClient.SemaphoreSlim.WaitAsync();

            try
            {
                if (
                    resolvedEvent.OriginalStreamId.StartsWith(StreamIdPrefix) &&
                    await this.ProcessEvent(
                        Encoding.UTF8.GetString(resolvedEvent.OriginalEvent.Metadata),
                        Encoding.UTF8.GetString(resolvedEvent.OriginalEvent.Data)
                        )
                    )
                {
                    this.lastPositionString = resolvedEvent.OriginalPosition?.ToString();

                    if (this.isLive)
                    {
                        this.ProcessLive(this.lastPositionString);
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Error(ex, "Error occured while processing event.");
            }
            finally
            {
                EventStoreEventLogClient.SemaphoreSlim.Release();
            }
        }
        private async Task EventAppeared(EventStoreCatchUpSubscription sub, ResolvedEvent evt)
        {
            _logger.LogTrace("EventAppeared {SubscriptionName} {Stream} Projector {ProjectorId}",
                             sub.SubscriptionName,
                             sub.StreamId,
                             ProjectorId);

            var de   = evt.Event.ToDomainEvent();
            var desc = new ProjectionEventData
            {
                EventData   = de,
                EventNumber = evt.OriginalEventNumber
            };

            // if (_useQueue)
            // {
            //     _logger.LogTrace("Enqueue Event {@0}", desc);
            //     await Queue.EnqueueAsync(desc).ConfigureAwait(false);
            // }
            // else
            // {
            await HandleEventAsync(desc).ConfigureAwait(false);

            // }
        }
        private Task EventAppeared(EventStoreCatchUpSubscription arg1, ResolvedEvent arg2)
        {
            if (arg2.Event == null || arg2.Event.EventStreamId.StartsWith("$") || arg2.Event.EventType.StartsWith("$"))
            {
                return(Task.CompletedTask);
            }

            dynamic data     = JObject.Parse(Encoding.UTF8.GetString(arg2.Event.Data));
            dynamic metaData = JObject.Parse(Encoding.UTF8.GetString(arg2.Event.Metadata));

            if (arg2.Event.EventType.Equals("TaskAddedV1"))
            {
                HandleTaskAdded(data, metaData);
            }
            if (arg2.Event.EventType.Equals("TaskRemovedV1"))
            {
                HandleTaskDeleted(data, metaData);
            }
            if (arg2.Event.EventType.Equals("WrongRemoveTaskRequestedV1"))
            {
                HandleWrongRemoveTaskRequested(data, metaData);
            }

            return(Task.CompletedTask);
        }
 private void SubscriptionDropped(EventStoreCatchUpSubscription subscription, SubscriptionDropReason reason, Exception ex)
 {
     // Known bug in ES, must manually stop subscription or sub will continue delivering events.
     // https://groups.google.com/forum/#!searchin/event-store/subscription/event-store/AdKzv8TxabM/6RzudeuAAgAJ
     _sub.Stop();
     Subscribe();
 }
Beispiel #9
0
 private void HandleDrop(
     EventStoreCatchUpSubscription subscription,
     SubscriptionDropReason reason,
     Exception ex)
 {
     RecoverSubscription();
 }
Beispiel #10
0
        private void HandleEvent(EventStoreCatchUpSubscription subscription, ResolvedEvent resolved)
        {
            if (!CanHandleSubscriptionEvent(subscription))
            {
                return;
            }

            try
            {
                connectionLock.EnterReadLock();

                if (CanHandleSubscriptionEvent(subscription))
                {
                    var storedEvent = Formatter.Read(resolved);

                    PublishAsync(storedEvent).Wait();

                    position = resolved.OriginalEventNumber;
                }
            }
            finally
            {
                connectionLock.ExitReadLock();
            }
        }
Beispiel #11
0
 private void Dropped(EventStoreCatchUpSubscription sub, SubscriptionDropReason reason, Exception ex)
 {
     //Reconnect if we drop
     //TODO: check the reason and handle it appropriately
     _view.ErrorMsg = "Subscription Dropped, press Enter to reconnect";
     Subscribe();
 }
Beispiel #12
0
        private async Task EventAppeared(EventStoreCatchUpSubscription sub, ResolvedEvent e, CancellationToken token,
                                         Func <string, long, IFullEvent, Task> callback)
        {
            // Don't care about metadata streams
            if (e.Event == null || e.Event.EventStreamId[0] == '$')
            {
                return;
            }

            if (token.IsCancellationRequested)
            {
                Logger.WarnEvent("Cancelation", "Token cancel requested");
                ThreadPool.QueueUserWorkItem((_) => sub.Stop(TimeSpan.FromSeconds(10)));
                token.ThrowIfCancellationRequested();
            }
            try
            {
                await EventAppeared(e, token, callback).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.ErrorEvent("AppearedException", ex, "Stream: [{Stream:l}] Position: {StreamPosition} {ExceptionType} - {ExceptionMessage}", e.Event.EventStreamId, e.Event.EventNumber, ex.GetType().Name, ex.Message);
                //throw;
            }
        }
Beispiel #13
0
        private void GotEvent(EventStoreCatchUpSubscription sub, ResolvedEvent evt)
        {
            try
            {
                //create local copies of state variables
                var total      = _checkpoint.Value;
                var checkpoint = evt.Event.EventNumber;

                var amount = (string)JObject.Parse(Encoding.UTF8.GetString(evt.Event.Data))["amount"];
                switch (evt.Event.EventType.ToUpperInvariant())
                {
                case "CREDIT":
                    total += int.Parse(amount);
                    break;

                case "DEBIT":
                    total -= int.Parse(amount);
                    break;

                default:
                    throw new Exception("Unknown Event Type");
                }
                _checkpoint.Save(checkpoint, total);
            }
            catch (Exception ex)
            {
                _view.ErrorMsg = "Event Exception: " + ex.Message;
            }
            //repaint screen
            _view.Total = _checkpoint.Value;
        }
        private async Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent)
        {
            if (_subscriptionEpochStopping ||
                !IsValidEvent(resolvedEvent))
            {
                return;
            }

            // ReSharper disable once PossibleInvalidOperationException
            var commitPosition = resolvedEvent.OriginalPosition.Value.CommitPosition;

            try
            {
                await EventAppeared(resolvedEvent, commitPosition);
            }
            catch (Exception e)
            {
                if (Retry())
                {
                    Log.Warning(e, "Exception received for subscription epoch {Name}. Retrying...", SubscriptionName);
                    throw;
                }

                if (_settings.StopOnException)
                {
                    TerminateSubscriptionDueToError(resolvedEvent, e);
                    return;
                }

                ExceptionReceivedAfterMaxRetries(resolvedEvent, e);
            }

            await OnUpdateLastCommitPosition(commitPosition);
        }
Beispiel #15
0
        private Task EventAppeared(EventStoreCatchUpSubscription _, ResolvedEvent resolvedEvent)
        {
            //filter out technical and test events from the $all stream
            if (resolvedEvent.Event.EventType.StartsWith("$") ||
                resolvedEvent.Event.EventType.StartsWith("eventType") ||
                resolvedEvent.Event.EventType.StartsWith("PersistentConfig1") ||
                resolvedEvent.Event.EventType.StartsWith("Subscription"))
            {
                return(Task.CompletedTask);
            }
            //Deserialize event to C# object
            var @event = resolvedEvent.Deserialize();

            //tell user which events are being projected
            Console.WriteLine("Projecting event {0}", @event.GetType().Name);

            //check which event appeared in the projection and update read model
            switch (@event)
            {
            case Events.UserRegistered e:
                _items.Add(new UserReadModel()
                {
                    UserId = e.UserId,
                    Name   = e.Name
                });
                break;

            case Events.UserNameUpdated e:
                UpdateItem(e.UserId, user => user.Name = e.Name);
                break;
            }

            return(Task.CompletedTask);
        }
        private void GotEvent(EventStoreCatchUpSubscription sub, ResolvedEvent evt)
        {
            try
            {
                //create local copies of state variables
                var total      = _total;
                var checkpoint = evt.Event.EventNumber;

                var amount = (string)JObject.Parse(Encoding.UTF8.GetString(evt.Event.Data))["amount"];
                switch (evt.Event.EventType.ToUpperInvariant())
                {
                case "CREDIT":
                    total += int.Parse(amount);
                    break;

                case "DEBIT":
                    total -= int.Parse(amount);
                    break;

                default:
                    throw new Exception("Unknown Event Type");
                }
                File.WriteAllText(_localFile, checkpoint + "," + total);
                //Update the common state after commit to disk
                _total     = total;
                Checkpoint = checkpoint;
            }
            catch (Exception ex)
            {
                _view.ErrorMsg = "Event Exception: " + ex.Message;
            }
            //repaint screen
            _view.Total = _total;
        }
Beispiel #17
0
        void EventAppeared(EventStoreCatchUpSubscription sub, ResolvedEvent evnt)
        {
            if (evnt.OriginalStreamId.StartsWith("$"))
            {
                return;
            }

            dynamic ev = _adapter.TryGetDomainEvent(evnt);

            if (ev == null)
            {
                return;
            }

            try
            {
                lock (this)
                {
                    Dispatch(ev);
                    _succeded++;
                    _checkPoint      = evnt.OriginalPosition.GetValueOrDefault();
                    _lastEventNumber = evnt.OriginalEventNumber;
                    if (ev.Timestamp > LastUpdate)
                    {
                        LastUpdate = ev.Timestamp;
                    }
                }
            }
            catch (Exception)
            {
                Debugger.Break();
            }
        }
        async Task EventAppeared(
            EventStoreCatchUpSubscription _,
            ResolvedEvent resolvedEvent)
        {
            if (resolvedEvent.Event.EventType.StartsWith("$"))
            {
                return;
            }

            var @event = resolvedEvent.Deserialze();

            Log.Debug("Projecting event {event}", @event.ToString());

            try
            {
                await Task.WhenAll(_projections.Select(x => x.Project(@event)));

                await _checkpointStore.StoreCheckpoint(
                    resolvedEvent.OriginalPosition.Value
                    );
            }
            catch (Exception e)
            {
                Log.Error(
                    e,
                    "Error occured when projecting the event {event}",
                    @event
                    );
                throw;
            }
        }
Beispiel #19
0
        private static void EventAppeared(EventStoreCatchUpSubscription s, ResolvedEvent e)
        {
            if (e.Event.EventType != "withdrew")
            {
                return;
            }

            var o           = e.Deserialze();
            var withdrew    = o as V1.Withdrew;
            var withdrewFix = new V1.WithdrewFix
            {
                Id          = withdrew.Id,
                Amount      = new Account().CalculateFee(withdrew.Amount),
                Description = "my fix",
                ChangedAt   = DateTime.Now,
                WithdrewId  = e.Event.EventId
            };
            var data = new EventData(
                Guid.NewGuid(),
                EventTypeMapper.GetTypeName(withdrewFix.GetType()),
                true,
                Encoding.Default.GetBytes(JsonConvert.SerializeObject(withdrewFix)), null);

            eventStoreConnection.AppendToStreamAsync(e.OriginalStreamId, ExpectedVersion.Any, data);

            Console.WriteLine($"Last position of fix=> {e.OriginalPosition.Value.CommitPosition}");
        }
        private Task EventAppeared(EventStoreCatchUpSubscription sub, ResolvedEvent @event)
        {
            var processedEvent = EventSerializer.DeserializeResolvedEvent(@event);

            if (processedEvent != null)
            {
                try
                {
                    _handleEvent(processedEvent);
                }
                catch (Exception ex)
                {
                    //TODO: handle
                }
            }

            if ([email protected])
            {
                throw new ArgumentException("ResolvedEvent didn't come off a subscription to all (has no position).");
            }

            _lastProcessedPosition = new Position(@event.OriginalPosition.Value.CommitPosition, @event.OriginalPosition.Value.PreparePosition);

            try
            {
                _lastProcessedUpdatedAction?.Invoke(@event.OriginalPosition.Value.CommitPosition, @event.OriginalPosition.Value.PreparePosition);
            }
            catch (Exception ex)
            {
                //TODO: handle
            }

            return(Task.CompletedTask);
        }
        /// <summary>
        /// Catches up subscription event appeared.
        /// </summary>
        /// <param name="subscription">The subscription.</param>
        /// <param name="resolvedEvent">The resolved event.</param>
        /// <param name="endpointId">The endpoint identifier.</param>
        /// <returns></returns>
        private async Task CatchUpSubscriptionEventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent, Guid catchUpSubscriptionId, Guid endpointId)
        {
            try
            {
                // Check the event data has been received
                if (this.EventAppeared != null)
                {
                    // Serialise the event data
                    String serialisedData = JsonConvert.SerializeObject(resolvedEvent);

                    SubscriptionDataTransferObject subscriptionInformation = new SubscriptionDataTransferObject()
                    {
                        SerialisedData      = serialisedData,
                        EventId             = (Guid)((RecordedEvent)resolvedEvent.Event).EventId,
                        SubscriptionGroupId = catchUpSubscriptionId.ToString(),
                    };

                    var handledSuccessfully = await this.EventAppeared(subscriptionInformation);

                    if (!handledSuccessfully)
                    {
                        throw new Exception($"Failed to Process Event {resolvedEvent.Event.EventId} on catchup subscription group {catchUpSubscriptionId}");
                    }
                }
                else
                {
                    Logger.LogInformation("Unable to process event as EventAppeared Event handler is null");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }
Beispiel #22
0
        public async Task SubscribeAsync(Func <StoredEvent, Task> onNext, Func <Exception, Task> onError = null)
        {
            Guard.NotNull(onNext, nameof(onNext));

            if (publishNext != null)
            {
                throw new InvalidOperationException("An handler has already been registered.");
            }

            publishNext  = onNext;
            publishError = onError;

            await CreateProjectionAsync();

            try
            {
                connectionLock.EnterWriteLock();

                internalSubscription = SubscribeToEventStore();
            }
            finally
            {
                connectionLock.ExitWriteLock();
            }
        }
Beispiel #23
0
 private void LiveProcessingStarted(EventStoreCatchUpSubscription sub)
 {
     if (!_onCompletedFired)
     {
         _onCompletedFired = true;
         _subject.OnCompleted();
     }
 }
        private void ProcessEvent(EventStoreCatchUpSubscription subscribtion, ResolvedEvent resolvedEvent)
        {
            var alarm = resolvedEvent.ParseJson <IrresponsibleGamblerDetected>();

            Publish(alarm);

            StoreCheckpoint(resolvedEvent);
        }
Beispiel #25
0
        public IObservable <OccuredEvent> Start(long?position)
        {
            eventSub?.Dispose();
            eventSub  = new Subject <OccuredEvent>();
            activeSub = connection.SubscribeToStreamFrom(StreamName, position, settings, Handle);

            return(eventSub);
        }
        private async Task RecoverSubscription()
        {
            var checkpointToken = await _checkpoints.Get();

            _subscription = _streamId == null
                ? SubscribeToAllFrom(checkpointToken.ParsePosition())
                : SubscribeToStreamFrom(checkpointToken == null ? default(int?) : int.Parse(checkpointToken));
        }
 private void SubscriptionDropped(EventStoreCatchUpSubscription _, SubscriptionDropReason reason, Exception error)
 {
     Unsubscribe();
     Debug("Subscription dropped. Reason: " + reason);
     Error(error);
     Thread.Sleep(5000);
     Subscribe();
 }
 private void SubscriptionDropped(
     IActorRef self,
     EventStoreCatchUpSubscription subscription,
     SubscriptionDropReason reason,
     Exception exception)
 {
     self.Tell(Kill.Instance);
 }
        static Task OnEvent(EventStoreCatchUpSubscription sub, ResolvedEvent ev)
        {
            return(Task.Run(() =>
                            Console.WriteLine($"EventType: {ev.Event.EventType}, EventNumber: {ev.OriginalEventNumber}")));
//            Console.WriteLine("saw event");
//            return new Task(() =>
//                );
        }
Beispiel #30
0
        private Task EventAppeared(EventStoreCatchUpSubscription subscription, ResolvedEvent resolvedEvent)
        {
            if (resolvedEvent.Event.EventType.StartsWith("$"))
            {
                return(Task.CompletedTask);
            }

            var @event = resolvedEvent.Deserialize();

            Log.Debug("Projecting event {type}", @event.GetType().Name);

            switch (@event)
            {
            case Events.CalendarCreated e:
                _calendarDetails.Add(new ReadModels.CalendarDetails
                {
                    CalendarId  = e.CalendarId,
                    Description = e.CalendarDescription,
                    MaximumBookingTimeInMinutes = e.MaximumBookingTimeInMinutes,
                    NumberOfBookings            = 0
                });
                _calendarOverviews.Add(new ReadModels.CalendarOverview {
                    CalendarId  = e.CalendarId,
                    Description = e.CalendarDescription
                });
                break;

            case Events.CalendarDescriptionChanged e:
                UpdateCalendarDetails(e.CalendarId, c => c.Description  = e.NewCalendarDescription);
                UpdateCalendarOverview(e.CalendarId, c => c.Description = e.NewCalendarDescription);
                break;

            case Events.CalendarMaxBookingTimeChanged e:
                UpdateCalendarDetails(e.CalendarId, c => c.MaximumBookingTimeInMinutes = e.NewMaximumBookingTimeInMinutes);
                break;

            case Events.BookingAdded e:
                UpdateCalendarDetails(e.CalendarId, c => c.NumberOfBookings++);
                _bookingDetails.Add(new ReadModels.BookingDetails
                {
                    CalendarId = e.CalendarId,
                    BookingId  = e.BookingId,
                    BookedBy   = e.BookedBy,
                    StartTime  = e.Start,
                    EndTime    = e.End
                });
                break;

            case Events.BookingRemoved e:
                UpdateCalendarDetails(e.CalendarId, c => c.NumberOfBookings--);
                _bookingDetails.Remove(
                    _bookingDetails.FirstOrDefault(b => b.BookingId == e.BookingId));
                break;
            }

            return(Task.CompletedTask);
        }