public static async Task Run( [ServiceBusTrigger(QueueNames.Status, Connection = "ServiceBusConnectionString")] Message msg, [ServiceBus(QueueNames.StatusMessage, Connection = "ServiceBusConnectionString")] IAsyncCollector <Message> statusMessages, [Table(TableNames.Status)] CloudTable table, string correlationId, ILogger log) { await table.CreateIfNotExistsAsync(); if (string.IsNullOrWhiteSpace(correlationId)) { correlationId = Guid.NewGuid().ToString(); log.LogWarning("CorrelationID is empty, generating a new one ({correlationId})", correlationId); } var statusUpdateCommand = msg.Convert <ImportStatusCommand>(); var mergedStatus = new ImportStatusEntity { PartitionKey = PartitionKeys.Status, RowKey = correlationId, CreatedOn = DateTimeOffset.UtcNow }; if (!string.IsNullOrWhiteSpace(statusUpdateCommand.ErrorMessage)) { mergedStatus.ErrorMessage = statusUpdateCommand.ErrorMessage; } if (statusUpdateCommand.TotalEntries.HasValue) { mergedStatus.TotalEntries = statusUpdateCommand.TotalEntries.Value; } mergedStatus.LastModificationOn = DateTimeOffset.UtcNow; var operation = TableOperation.Insert(mergedStatus); await table.ExecuteAsync(operation); try { var importStatus = new ImportStatusMessageDto { TotalEntries = mergedStatus.TotalEntries, ErrorMessage = mergedStatus.ErrorMessage, CompletedOn = mergedStatus.CompletedOn, CorrelationId = correlationId, Failed = mergedStatus.TotalFailed, StartedOn = mergedStatus.CreatedOn, Succeeded = mergedStatus.TotalSucceeded }; await statusMessages.AddAsync(importStatus.Convert(correlationId)); } catch (Exception ex) { log.LogError(ex, "Failed to send message to status update queueu"); } await statusMessages.FlushAsync(); log.LogInformation($"C# ServiceBus queue trigger function processed message: {msg}"); }
public static async Task Run( [ServiceBusTrigger(QueueNames.StatusLoop, Connection = "ServiceBusConnectionString")] Message msg, [ServiceBus(QueueNames.StatusLoop, Connection = "ServiceBusConnectionString")] IAsyncCollector <Message> statusLoop, [ServiceBus(QueueNames.StatusMessage, Connection = "ServiceBusConnectionString")] IAsyncCollector <Message> statusMessages, [Table(TableNames.Status, PartitionKeys.Status, "{CorrelationId}")] ImportStatusEntity status, [Table(TableNames.Status)] CloudTable table, [Table(TableNames.StatusProcess)] CloudTable statusProcTable, string correlationId, ILogger log) { await statusProcTable.CreateIfNotExistsAsync(); await table.CreateIfNotExistsAsync(); if (string.IsNullOrWhiteSpace(correlationId)) { correlationId = Guid.NewGuid().ToString(); log.LogWarning("CorrelationID is empty, generating a new one ({correlationId})", correlationId); return; } if (status == null) { log.LogWarning("Oops, the status entry of import ({correlationId}) could not be found", correlationId); return; } var statusUpdateCommand = msg.Convert <ImportStatusReportCommand>(); var updateCounts = await GetProcessingCountsAsync(correlationId, statusProcTable); status.TotalSucceeded += updateCounts.Item1; status.TotalFailed += updateCounts.Item2; status.LastModificationOn = DateTimeOffset.UtcNow; if (!status.CompletedOn.HasValue && status.TotalEntries == status.TotalFailed + status.TotalSucceeded) { status.CompletedOn = DateTimeOffset.UtcNow; } var operation = TableOperation.Replace(status); await table.ExecuteAsync(operation); try { var importStatus = new ImportStatusMessageDto { TotalEntries = status.TotalEntries, ErrorMessage = status.ErrorMessage, CompletedOn = status.CompletedOn, CorrelationId = correlationId, Failed = status.TotalFailed, StartedOn = status.CreatedOn, Succeeded = status.TotalSucceeded }; await statusMessages.AddAsync(importStatus.Convert(correlationId)); if (updateCounts.Item1 > 0 || updateCounts.Item2 > 0) { statusUpdateCommand.StopReportingAt = DateTimeOffset.UtcNow.AddMinutes(30); } if (!status.CompletedOn.HasValue && statusUpdateCommand.StopReportingAt.CompareTo(DateTimeOffset.UtcNow) > 0) { await statusLoop.AddAsync(statusUpdateCommand.Convert(correlationId, 1)); } } catch (Exception ex) { log.LogError(ex, "Failed to send message to status update queueu"); } await statusMessages.FlushAsync(); await statusLoop.FlushAsync(); log.LogInformation($"C# ServiceBus queue trigger function processed message: {msg}"); }