Example 03: Batch Order Processing Queue
Overview
The dashboard implements intelligent batch processing for purchase order creation, optimizing SYSPRO interactions and providing robust error handling for large-scale order generation.
Implementation
Batch Queue Structure
public class OrderBatchProcessor
{
private readonly Queue<SummaryOrder> _orderQueue;
private readonly int _batchSize = 10;
public async Task ProcessBatchAsync(
IEnumerable<SummaryOrder> orders)
{
// Queue orders for processing
foreach (var order in orders)
{
_orderQueue.Enqueue(order);
}
// Process in batches
while (_orderQueue.Any())
{
var batch = DequeueBatch(_batchSize);
await ProcessSingleBatch(batch);
}
}
private async Task ProcessSingleBatch(
IEnumerable<SummaryOrder> batch)
{
var results = new List<OrderResult>();
// Process with retry logic
foreach (var order in batch)
{
var result = await ProcessWithRetry(order, 3);
results.Add(result);
}
// Handle results
await UpdateBatchStatus(results);
}
}
State Management
-- Custom tracking table
CREATE TABLE CG_OrderProcessingQueue (
QueueId INT IDENTITY PRIMARY KEY,
OrderData NVARCHAR(MAX),
Status VARCHAR(20),
RetryCount INT DEFAULT 0,
CreatedDate DATETIME,
ProcessedDate DATETIME,
ErrorMessage NVARCHAR(MAX)
)
Parallel Processing
public async Task ProcessOrdersInParallel(
IEnumerable<SummaryOrder> orders)
{
var semaphore = new SemaphoreSlim(5); // Max 5 concurrent
var tasks = new List<Task<OrderResult>>();
foreach (var order in orders)
{
await semaphore.WaitAsync();
var task = Task.Run(async () =>
{
try
{
return await ProcessOrder(order);
}
finally
{
semaphore.Release();
}
});
tasks.Add(task);
}
var results = await Task.WhenAll(tasks);
await ProcessResults(results);
}
Performance Optimizations
Batching Strategy
- Group by supplier for efficiency
- Limit batch size to prevent timeouts
- Process high-priority orders first
Error Recovery
- Automatic retry with exponential backoff
- Dead letter queue for failed orders
- Partial success handling
Progress Tracking
- Real-time progress updates
- Estimated completion time
- Cancel capability
Benefits
- Improved throughput
- Better error isolation
- Progress visibility
- Resumable processing
- Audit trail