private async Task TrySendNextBatch(IMessageHandlerContext context) { var customersInProgress = Data.Invoices.Count(s => s.Status == InvoiceStatus.InProgress); var customersWaiting = Data.Invoices.Count(s => s.Status == InvoiceStatus.Waiting); if (customersInProgress > 0) { return; } if (customersWaiting == 0) { Console.WriteLine($"[{Data.BillingRunId}] {customersWaiting} customers remaining, completing saga."); await context.Publish <InvoiceProposalsCreated>(s => { s.BillingRunId = Data.BillingRunId; }); MarkAsComplete(); return; } Console.WriteLine($"[{Data.BillingRunId}] {customersWaiting} customers remaining, sending next batch of {BatchSize}."); var nextInvoices = Data.Invoices.Where(s => s.Status == InvoiceStatus.Waiting).Take(BatchSize); foreach (var invoice in nextInvoices) { var message = new CreateInvoiceProposalRequest(); message.CustomerId = invoice.CustomerId; message.BillingRunId = Data.BillingRunId; invoice.Status = InvoiceStatus.InProgress; await context.SendLocal(message); } }
public async Task Handle(CreateInvoiceProposalRequest message, IMessageHandlerContext context) { Console.WriteLine($"\t[{message.BillingRunId}] CreateInvoiceProposalRequest registered."); Data.CustomerId = message.CustomerId; Data.BillingRunId = message.BillingRunId; await context.Send(new CreateInvoice() { CustomerId = message.CustomerId }); }