Example #1
0
        public IObservable <RecordedRawEvent> NewEvents(string stream_name, Subject <RecordedRawEvent> new_subject = null)
        {
            new_subject = new_subject ?? new Subject <RecordedRawEvent>();

            connection.SubscribeToStreamAsync(stream_name, true,
                                              (e, s) =>
            {
                var recorded_event = new RecordedRawEvent(new RawEvent(s.Event.EventId,
                                                                       s.Event.Data,
                                                                       s.Event.Metadata,
                                                                       s.Event.EventType),
                                                          stream_name,
                                                          s.Event.EventNumber);

                new_subject.OnNext(recorded_event);
            },
                                              (ess, dr, ex) =>
            {
                Console.WriteLine($"Subscription to {stream_name} dropped .. reconnecting");

                NewEvents(stream_name, new_subject);
            });

            return(new_subject.AsObservable());
        }
Example #2
0
        public void CommitAndPublish(RecordedRawEvent origin_event, IEnumerable <RawEvent> raw_events)
        {
            var log_payload = origin_event.ToPointer();


            //events to publish
            var events = raw_events.Select(payload => new EventData(payload.id, payload.type, true, payload.data, payload.metadata));


            processed_request_ids.AddOrUpdate(origin_event.raw_event.id, default(byte), (g, b) => b);
            //cleanup
            byte remove; received_request_ids.TryRemove(origin_event.raw_event.id, out remove);

            events =
                events.Union(new EventData(Guid.NewGuid(),
                                           $"{LoggedEventPointer}-{log_payload.stream_name}",  //Todo: wrap this in a constant
                                           true,
                                           log_payload.ToBytes(),
                                           new { processed_request_ids }.ToBytes())
                             .ToEnumerable());


            //publish

            connection
            .AppendToStreamAsync(current_domain_stream,
                                 ExpectedVersion.Any,
                                 events)
            .Wait();
        }
Example #3
0
        private void handle(RecordedRawEvent recorded_event)
        {
            Console.WriteLine($"Received event with id: {recorded_event.raw_event.id}");

            if (!router.is_route_handler_defined(recorded_event.raw_event.type))
            {
                Console.WriteLine($"No handler defined for event with id: {recorded_event.raw_event.id}");

                return;
            }

            var request = new RawRequest <W>(recorded_event.raw_event.id,
                                             recorded_event.raw_event.data.ToJsonString(),
                                             recorded_event.raw_event.type,
                                             router.get_handler(recorded_event.raw_event.type));

            var responder_added = responders.TryAdd(recorded_event.raw_event.id, response => {
                response
                .core_response
                .match(
                    is_success: events =>
                {
                    var payload =
                        events.Select(ev => ev.ToRawEvent(response.raw_request.id));

                    //Potential data inconsistencies between removing above, and publishing below
                    event_store.CommitAndPublish(recorded_event, payload);
                },
                    is_error: errors =>
                {
                    var payload =
                        errors.Select(ev => ev.ToRawEvent(response.raw_request.id));

                    //Potential data inconsistencies between removing above, and publishing below
                    event_store.PublishErrors(recorded_event, payload);
                }
                    );


                Console.WriteLine($"Finished event with id: {response.raw_request.id}");
            });

            if (!responder_added)
            {
                throw new Exception("Responder could not be added");
            }

            requests.Add(request);
        }
Example #4
0
        public void PublishErrors(RecordedRawEvent origin_event, IEnumerable <RawEvent> raw_events)
        {
            //Add to in-memory: will be made durable when the next request is persisted
            processed_request_ids.AddOrUpdate(origin_event.raw_event.id, default(byte), (g, b) => b);
            byte remove; received_request_ids.TryRemove(origin_event.raw_event.id, out remove);

            var events = raw_events.Select(payload => new EventData(payload.id, payload.type, true, payload.data, payload.metadata));


            connection
            .AppendToStreamAsync($"{origin_event.origin_stream}-Errors",
                                 ExpectedVersion.Any,
                                 events)
            .Wait();
        }
Example #5
0
        public IObservable <RecordedRawEvent> AllUnprocessedEvents(string stream_name, Subject <RecordedRawEvent> allUnprocessedSubject = null)
        {
            allUnprocessedSubject = allUnprocessedSubject ?? new Subject <RecordedRawEvent>();

            var settings = new CatchUpSubscriptionSettings(50, 10, true, true);

            connection.SubscribeToStreamFrom(stream_name, StreamCheckpoint.StreamStart, settings,
                                             (e, s) =>
            {
                if (!received_request_ids.ContainsKey(s.Event.EventId)
                    &&
                    !processed_request_ids.ContainsKey(s.Event.EventId)
                    &&
                    !s.Event.EventType.Contains(LoggedEventPointer))
                {
                    received_request_ids.AddOrUpdate(s.Event.EventId, default(byte), (g, b) => b);

                    var recorded_event =
                        new RecordedRawEvent(new RawEvent(s.Event.EventId,
                                                          s.Event.Data,
                                                          s.Event.Metadata,
                                                          s.Event.EventType),
                                             stream_name,
                                             s.Event.EventNumber);


                    allUnprocessedSubject.OnNext(recorded_event);
                }
            }, null,
                                             (ess, dr, ex) =>
            {
                Console.WriteLine($"Subscription to {stream_name} dropped .. reconnecting");

                AllUnprocessedEvents(stream_name, allUnprocessedSubject);
            });

            return(allUnprocessedSubject.AsObservable());
        }