public void InvokeEvent(object @event) { var type = @event.GetType(); List <Wire> info; if (!Dict.TryGetValue(type, out info)) { return; } try { var sw = new Stopwatch(); foreach (var wire in info) { sw.Restart(); wire.Call(@event); sw.Stop(); var seconds = sw.Elapsed.TotalSeconds; if (seconds > 2 && wire.@object != null) { SystemObserver.Notify("[Warn]: {0}\ttook {1} seconds to process event {2}", [email protected]().Name, seconds, @event.ToString()); } } } catch (TargetInvocationException ex) { if (null != InternalPreserveStackTraceMethod) { InternalPreserveStackTraceMethod.Invoke(ex.InnerException, new object[0]); } throw ex.InnerException; } }
PublishResult PublishEventsIfAnyNew(long initialPosition, int count) { var records = _store.EnumerateAllItems(initialPosition, count); var currentPosition = initialPosition; var publishedCount = 0; foreach (var e in records) { if (e.StoreVersion < currentPosition) { throw new InvalidOperationException(string.Format("Retrieved record with position less than current. Store versions {0} <= current position {1}", e.StoreVersion, currentPosition)); } if (_recordShouldBePublished(e)) { for (int i = 0; i < e.Items.Length; i++) { // predetermined id to kick in event deduplication // if server crashes somehow var envelopeId = "esp-" + e.StoreVersion + "-" + i; var item = e.Items[i]; publishedCount += 1; _sender.Send(item, envelopeId); } } currentPosition = e.StoreVersion; } var result = new PublishResult(initialPosition, currentPosition, count); if (result.Changed) { SystemObserver.Notify("{0}\t[sys] Message store pointer moved to {1} ({2} published)", _name, result.FinalPosition, publishedCount); } return(result); }
public static IDisposable When <T>(Action <T> when, bool includeTracing = true) where T : class { var eventsObserver = new ImmediateEventsObserver(); Action <ISystemEvent> onEvent = @event => { var type = @event as T; if (type != null) { when(type); } }; eventsObserver.Event += onEvent; var list = new List <IObserver <ISystemEvent> > { eventsObserver }; if (includeTracing) { list.Add(new ImmediateTracingObserver()); } var old = SystemObserver.Swap(list.ToArray()); return(new Disposable(() => { SystemObserver.Swap(old); eventsObserver.Event -= onEvent; })); }
public void Send(object message, string envelopeId, params MessageAttribute[] attributes) { var envelope = new ImmutableEnvelope(envelopeId, DateTime.UtcNow, message, attributes); var data = _streamer.SaveEnvelopeData(envelope); _queue.PutMessage(data); SystemObserver.Notify(new EnvelopeSent( _queue.Name, envelope.EnvelopeId, envelope.Message.GetType().Name, envelope.Attributes)); }
public static IDisposable When <T>(Action <T> when, bool includeTracing = true) where T : class { var observer = new EventsObserver(@event => { var type1 = @event as T; if (type1 != null) { when(type1); } if (includeTracing) { Trace.WriteLine(@event); } }); var old = SystemObserver.Swap(new IObserver <ISystemEvent>[] { observer }); return(new Disposable(() => SystemObserver.Swap(old))); }
void InnerSendBatch(Action <EnvelopeBuilder> configure, object[] messageItems, IdGeneration id = IdGeneration.Default) { string envelopeId; switch (id) { case IdGeneration.Default: envelopeId = _idGenerator(); break; case IdGeneration.HashContent: envelopeId = HashContents(configure, messageItems); break; default: throw new ArgumentOutOfRangeException("id"); } var builder = new EnvelopeBuilder(envelopeId); foreach (var item in messageItems) { builder.AddItem(item); } configure(builder); var envelope = builder.Build(); var data = _streamer.SaveEnvelopeData(envelope); var queue = GetOutboundQueue(); queue.PutMessage(data); SystemObserver.Notify(new EnvelopeSent(queue.Name, envelope.EnvelopeId, envelope.Items.Select(x => x.MappedType.Name).ToArray(), envelope.GetAllAttributes())); }
public void VerifyEventStreamSanity() { var result = _storage.GetSingletonOrNew <PublishCounter>(); if (result.Position != 0) { SystemObserver.Notify(_name + "\tContinuing work with existing event store"); return; } var store = _store.EnumerateAllItems(0, 100).ToArray(); if (store.Length == 0) { SystemObserver.Notify(_name + "\tOpening new event stream"); //_sender.SendHashed(new EventStreamStarted()); return; } if (store.Length == 100) { throw new InvalidOperationException( "It looks like event stream really went ahead (or storage pointer was reset). Do you REALLY mean to resend all events?"); } }