private async Task <AsyncEventQueue> GetOrCreateQueueAsync(string queueName, long?lastSequenceNumberProcessed) { AsyncEventQueue queue = await GetQueueAsync(queueName); if (queue == null) { queue = new AsyncEventQueue(queueName, lastSequenceNumberProcessed); crudRepository.Add(queue); } return(queue); }
public async Task DequeueEventAsync(Guid asyncEventQueueRecordId) { QueuedAsyncEvent queuedEvent = await crudRepository.FindAsync <QueuedAsyncEvent>(asyncEventQueueRecordId); if (queuedEvent != null) { AsyncEventQueue queue = await GetOrCreateQueueAsync(queuedEvent.QueueId, null); if (queuedEvent.SequenceNumber != null) { queue.LastSequenceNumberProcessed = queuedEvent.SequenceNumber; } crudRepository.Remove(queuedEvent); } }
public async Task <IReadOnlyCollection <IAsyncEventQueueRecord> > GetQueueEventsAsync(string queueName) { AsyncEventQueue queue = await GetQueueAsync(queueName); if (queue == null) { return(new List <IAsyncEventQueueRecord>()); } var events = (await QueryQueuedEvents() .Where(x => x.QueueId == queue.Id) .ToListAsync(crudRepository)) .Select(SelectRecordFromQueuedEvent) .ToList(); return(events); }
public async Task <IAsyncEventQueueState> GetQueueStateAsync(string queueName) { AsyncEventQueue queue = await GetQueueAsync(queueName); return(queue); //can be null }
protected async Task <List <QueuedAsyncEvent> > EnqueueEventsToRepositoryAsync() { var eventStoreEvents = eventsToQueues.Where(x => (x.Key as IEventStoreEventMessage)?.Record is EventStoreRecordAdapter).ToArray(); var externalEvents = eventsToQueues.Except(eventStoreEvents).ToArray(); var queuedEvents = new List <QueuedAsyncEvent>(); //external if (externalEvents.Length > 0) { foreach (var externalEvent in externalEvents) { externalEventStore.PushEvent(externalEvent.Key); } var externalEventRecords = await externalEventStore.CommitAsync(); foreach (var externalEventRecord in externalEventRecords) { if (externalEventRecord.IsDispatchedToAsyncQueues) { continue; } externalEventRecord.MarkDispatchedToAsyncQueues(); var sequences = externalEvents.First(x => x.Key.Metadata.GetEventId() == externalEventRecord.Id).Value; foreach (var sequence in sequences) { AsyncEventQueue queue = await GetOrCreateQueueAsync(sequence.SequenceName, null); QueuedAsyncEvent queuedEvent = new QueuedAsyncEvent(queue.Id, externalEventRecord, sequence.EventSequenceNumber); crudRepository.Add(queuedEvent); queuedEvents.Add(queuedEvent); } } } //event store var allQueueNames = new HashSet <string>(); foreach (var eventToQueues in eventStoreEvents) { foreach (var sequence in eventToQueues.Value) { allQueueNames.Add(sequence.SequenceName); } } if (allQueueNames.Count > 10) { await LoadQueuesAsync(allQueueNames.ToArray()); } foreach (var eventToQueues in eventStoreEvents) { var storeMessage = (IEventStoreEventMessage)eventToQueues.Key; var eventStreamRow = ((EventStoreRecordAdapter)storeMessage.Record).EventStreamRow; if (eventStreamRow.IsDispatchedToAsyncQueues) { continue; } eventStreamRow.MarkDispatchedToAsyncQueues(); if (!crudRepository.IsAttached(eventStreamRow)) { crudRepository.Attach(eventStreamRow); crudRepository.SetEntityState(eventStreamRow, EntityState.Modified); } foreach (var sequence in eventToQueues.Value) { AsyncEventQueue queue = await GetOrCreateQueueAsync(sequence.SequenceName, null); QueuedAsyncEvent queuedEvent = new QueuedAsyncEvent(queue.Id, eventStreamRow, sequence.EventSequenceNumber); crudRepository.Add(queuedEvent); queuedEvents.Add(queuedEvent); } } eventsToQueues.Clear(); return(queuedEvents); }