export interface BatchOperation {
id: string;
operations: any[];
concurrency: number;
status: 'pending' | 'running' | 'completed' | 'failed';
results: any[];
errors: any[];
}
export class BatchManager {
private batches: Map<string, BatchOperation> = new Map();
createBatch(operations: any[], concurrency: number = 5): string {
const id = Date.now().toString();
this.batches.set(id, {
id,
operations,
concurrency,
status: 'pending',
results: [],
errors: []
});
return id;
}
async executeBatch(batchId: string): Promise<any> {
const batch = this.batches.get(batchId);
if (!batch) {
throw new Error(`Batch ${batchId} not found`);
}
batch.status = 'running';
try {
// Execute operations in batches with concurrency limit
const results: any[] = [];
const errors: any[] = [];
for (let i = 0; i < batch.operations.length; i += batch.concurrency) {
const chunk = batch.operations.slice(i, i + batch.concurrency);
const chunkResults = await Promise.allSettled(
chunk.map(op => this.executeOperation(op))
);
chunkResults.forEach((result, idx) => {
if (result.status === 'fulfilled') {
results.push(result.value);
} else {
errors.push({
operation: chunk[idx],
error: result.reason
});
}
});
}
batch.results = results;
batch.errors = errors;
batch.status = errors.length > 0 ? 'failed' : 'completed';
return {
totalOperations: batch.operations.length,
successful: results.length,
failed: errors.length,
results,
errors
};
} catch (error) {
batch.status = 'failed';
throw error;
}
}
private async executeOperation(operation: any): Promise<any> {
// Placeholder - actual implementation would execute the operation
return new Promise(resolve => {
setTimeout(() => resolve({ success: true }), Math.random() * 100);
});
}
getBatchStatus(batchId: string): BatchOperation | undefined {
return this.batches.get(batchId);
}
}