private void ConsumeEventByListeners(AggregateEvent @event, IEnumerable <IAsyncListener> listeners) { using (var context = new ThreadContextContainer()) { this.eventContext.LoadFromEvent(@event); foreach (var listener in listeners) { try { listener.Consume(@event); } catch { var message = S.Invariant( $@"Error processing the listener {listener.GetType().ToString()} for aggregate {@event.AggregateUid}, version {@event.AggregateVersion}, event type {@event.GetType().ToString()}"); this.log.LogError(message); throw; } } this.work.MarkEventAsAsyncProcessed(@event); work.Commit(); } }
/// <summary> /// Gets the sceneario. /// </summary> /// <param name="id">The identifier.</param> /// <returns></returns> public ScenarioAggregate GetSceneario(int id) { using (var context = new ThreadContextContainer()) { InitIfNeeded(); var aggregate = this.work.GetAggregate <ScenarioAggregate>(scenarioNames[id]); this.work.Commit(); return(aggregate); } }
public ScenarioAggregate Reset(int id) { using (var context = new ThreadContextContainer()) { var aggregate = this.work.GetAggregate <ScenarioAggregate>(scenarioNames[id]); this.work.Handle(new ScenarioReset() { AggregateUid = scenarioNames[id] }); this.work.Commit(); return(aggregate); } }
public ScenarioAggregate Move(int id, DirectionEnum direction) { using (var context = new ThreadContextContainer()) { var aggregate = this.work.GetAggregate <ScenarioAggregate>(scenarioNames[id]); this.work.Handle(new PlayerMoved() { AggregateUid = scenarioNames[id], Direction = direction }); this.work.Commit(); return(aggregate); } }
public void Process(string message, BasicDeliverEventArgs ack, IQueue queue, IQueue errorQueue) { Check.NotNull(() => queue); Check.NotNull(() => errorQueue); object data = this.serializer.Deserialize <object>(message); if (data == null) { return; } var dataType = data.GetType(); if (!this.methodsByType.ContainsKey(dataType)) { this.log.LogError(S.Invariant($"The datatType {dataType.ToString()} dosen't have any handler")); return; } bool sendToError = false; LockerInfo currentLock = null; try { using (var context = new ThreadContextContainer()) { var method = this.methodsByType[dataType]; var lockName = method.GetLockName(data); if (!string.IsNullOrWhiteSpace(lockName)) { currentLock = this.locker.TryLockKey(lockName, 300); if (currentLock == null) { queue.SendNoAck(ack); this.log.LogDebug(S.Invariant($"{lockName} is locked so the delayed calls hub cannot procces it right now")); CheckLockerBarrier(lockName); return; } } method.CompleteCall(data); this.work.Commit(); } } catch (ConcurrencyException) { this.log.LogWarning(S.Invariant($"There is a concurrency excpetion in the DelayedCall with message {message}")); queue.SendNoAck(ack); return; } catch (Exception ex) { this.log.LogError( S.Invariant($"There is an error executing the DelayedCall with message {message}, the message will be sent to the error queue") , ex); sendToError = true; } if (currentLock != null) { this.locker.ReleaseKey(currentLock.ItemName); } if (sendToError) { SendToError(data, errorQueue); } queue.SendAck(ack); }
private void ProcessIfNeeded(IRecurrentAlarm job) { try { log.LogDebug(S.Invariant($"Processing job {job.JobName}")); var jobRow = new WakeUpRow { SchedulerName = job.JobName, DateRangeStart = this.DateTimeProvider.UtcNowTicks }; var jobRows = this.provider.GetValuesByKeyPattern <WakeUpRow>(100, jobRow.StartKey, jobRow.DocumentType).Items.Where(x => !x.ReverseRow); foreach (var row in jobRows) { if (row.AggregateUid != new Guid()) { if (this.locker.TryLockKey(row.AggregateUid.ToString(), 300) == null) { this.log.LogDebug(S.Invariant($"Aggregate {row.AggregateUid} is locked so the scheduler cannot procces it right now")); continue; } } else { if (this.TryLockItem(row, 300) == null) { this.log.LogDebug(S.Invariant($"Row {row.DocumentKey} is blocked so the asynchub cannot procces it right now")); continue; } } try { using (var context = new ThreadContextContainer()) { log.LogDebug(S.Invariant($"Processing row {row.DocumentKey} for job {job.JobName}")); job.Process(row); this.unitOfWork.Commit(); } } catch (Exception ex) { this.log.LogError( ex, S.Invariant($"Error processing row {row.DocumentKey}")); } try { this.ReleaseItem(row); this.locker.ReleaseKey(row.AggregateUid.ToString()); } catch (Exception ex) { this.log.LogError( ex, S.Invariant($"Error releasing row {row.DocumentKey}")); } } } catch (Exception ex) { this.log.LogError( ex, S.Invariant($"Uncontrolled Exception")); } }