async Task ProcessMessages(IMessage[] messages, EndpointExecutorFsm fsm) { SendMessage command = Commands.SendMessage(messages); await fsm.RunAsync(command); await command.Completion; }
public async Task UpdatePriorities(IList <uint> priorities, Option <Endpoint> newEndpoint) { Preconditions.CheckArgument(priorities.Count > 0); Events.UpdatePriorities(this, priorities); if (this.closed) { throw new InvalidOperationException($"Endpoint executor for endpoint {this.Endpoint} is closed."); } // Update priorities by merging the new ones with the existing. // We don't ever remove stale priorities, otherwise stored messages // pending for a removed priority will never get sent. ImmutableDictionary <uint, EndpointExecutorFsm> snapshot = this.prioritiesToFsms; Dictionary <uint, EndpointExecutorFsm> updatedSnapshot = new Dictionary <uint, EndpointExecutorFsm>(snapshot); foreach (uint priority in priorities) { if (!updatedSnapshot.ContainsKey(priority)) { string id = GetMessageQueueId(this.Endpoint.Id, priority); // Create a message queue in the store for every priority we have await this.messageStore.AddEndpoint(id); // Create a checkpointer and a FSM for every message queue ICheckpointer checkpointer = await this.checkpointerFactory.CreateAsync(id, this.Endpoint.Id, priority); EndpointExecutorFsm fsm = new EndpointExecutorFsm(this.Endpoint, checkpointer, this.config); // Add it to our dictionary updatedSnapshot.Add(priority, fsm); } else { // Update the existing FSM with the new endpoint EndpointExecutorFsm fsm = updatedSnapshot[priority]; await newEndpoint.ForEachAsync(e => fsm.RunAsync(Commands.UpdateEndpoint(e))); } } if (this.prioritiesToFsms.CompareAndSet(snapshot, updatedSnapshot.ToImmutableDictionary())) { Events.UpdatePrioritiesSuccess(this, updatedSnapshot.Keys.ToList()); // Update the lastUsedFsm to be the initial one, we always // have at least one priority->FSM pair at this point. this.lastUsedFsm = updatedSnapshot[priorities[0]]; } else { Events.UpdatePrioritiesFailure(this, updatedSnapshot.Keys.ToList()); } }