forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.service.ts
More file actions
372 lines (317 loc) · 10.2 KB
/
Copy pathbatch.service.ts
File metadata and controls
372 lines (317 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import {
Injectable,
Logger,
HttpException,
HttpStatus,
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository, In } from "typeorm";
import { InjectQueue } from "@nestjs/bull";
import { Queue, Job } from "bull";
import { ConfigService } from "@nestjs/config";
import { BatchJob, BatchJobType, BatchJobStatus } from "./entities/batch-job.entity";
import { BatchOperation, BatchOperationStatus } from "./entities/batch-operation.entity";
import {
CreateBatchSplitsDto,
CreateBatchPaymentsDto,
BatchOptionsDto,
} from "./dto/create-batch.dto";
import { BatchStatusDto, BatchListDto } from "./dto/batch-status.dto";
import { BatchProgressService } from "./batch-progress.service";
export interface BatchJobData {
batchId: string;
chunkSize: number;
concurrency: number;
}
@Injectable()
export class BatchService {
private readonly logger = new Logger(BatchService.name);
private readonly defaultChunkSize: number;
private readonly defaultConcurrency: number;
constructor(
@InjectRepository(BatchJob)
private batchJobRepository: Repository<BatchJob>,
@InjectRepository(BatchOperation)
private batchOperationRepository: Repository<BatchOperation>,
@InjectQueue("batch_splits") private splitQueue: Queue,
@InjectQueue("batch_payments") private paymentQueue: Queue,
@InjectQueue("batch_scheduled") private scheduledQueue: Queue,
private batchProgressService: BatchProgressService,
private configService: ConfigService,
) {
this.defaultChunkSize = this.configService.get<number>("BATCH_CHUNK_SIZE", 100);
this.defaultConcurrency = this.configService.get<number>("BATCH_CONCURRENCY", 5);
}
/**
* Create a batch of splits
*/
async createBatchSplits(dto: CreateBatchSplitsDto): Promise<BatchStatusDto> {
const { splits, options } = dto;
if (!splits || splits.length === 0) {
throw new HttpException("No splits provided", HttpStatus.BAD_REQUEST);
}
// Create batch job
const batch = this.batchJobRepository.create({
type: BatchJobType.SPLIT_CREATION,
status: BatchJobStatus.PENDING,
total_operations: splits.length,
options: options || {},
});
const savedBatch = await this.batchJobRepository.save(batch);
// Create operations
const operations = splits.map((split, index) =>
this.batchOperationRepository.create({
batch_id: savedBatch.id,
operation_index: index,
status: BatchOperationStatus.PENDING,
payload: split,
}),
);
await this.batchOperationRepository.save(operations);
// Queue the batch job
const chunkSize = options?.chunkSize || this.defaultChunkSize;
const concurrency = options?.concurrency || this.defaultConcurrency;
await this.splitQueue.add(
"process",
{
batchId: savedBatch.id,
chunkSize,
concurrency,
} as BatchJobData,
{
priority: options?.priority,
delay: options?.delay,
jobId: savedBatch.id,
},
);
this.logger.log(`Created batch split job ${savedBatch.id} with ${splits.length} operations`);
return this.getBatchStatus(savedBatch.id);
}
/**
* Create a batch of payments
*/
async createBatchPayments(dto: CreateBatchPaymentsDto): Promise<BatchStatusDto> {
const { payments, options } = dto;
if (!payments || payments.length === 0) {
throw new HttpException("No payments provided", HttpStatus.BAD_REQUEST);
}
// Create batch job
const batch = this.batchJobRepository.create({
type: BatchJobType.PAYMENT_PROCESSING,
status: BatchJobStatus.PENDING,
total_operations: payments.length,
options: options || {},
});
const savedBatch = await this.batchJobRepository.save(batch);
// Create operations
const operations = payments.map((payment, index) =>
this.batchOperationRepository.create({
batch_id: savedBatch.id,
operation_index: index,
status: BatchOperationStatus.PENDING,
payload: payment,
}),
);
await this.batchOperationRepository.save(operations);
// Queue the batch job
const chunkSize = options?.chunkSize || this.defaultChunkSize;
const concurrency = options?.concurrency || this.defaultConcurrency;
await this.paymentQueue.add(
"process",
{
batchId: savedBatch.id,
chunkSize,
concurrency,
} as BatchJobData,
{
priority: options?.priority,
delay: options?.delay,
jobId: savedBatch.id,
},
);
this.logger.log(`Created batch payment job ${savedBatch.id} with ${payments.length} operations`);
return this.getBatchStatus(savedBatch.id);
}
/**
* Get batch status with operations
*/
async getBatchStatus(batchId: string): Promise<BatchStatusDto> {
const batch = await this.batchJobRepository.findOne({
where: { id: batchId },
relations: ["operations"],
});
if (!batch) {
throw new HttpException("Batch not found", HttpStatus.NOT_FOUND);
}
return this.mapToStatusDto(batch);
}
/**
* List batches with pagination
*/
async listBatches(
page: number = 1,
limit: number = 50,
status?: BatchJobStatus,
): Promise<BatchListDto> {
const where: any = {};
if (status) {
where.status = status;
}
const [batches, total] = await this.batchJobRepository.findAndCount({
where,
order: { created_at: "DESC" },
skip: (page - 1) * limit,
take: limit,
});
return {
batches: batches.map((batch) => this.mapToStatusDto(batch)),
total,
page,
limit,
};
}
/**
* Retry failed operations
*/
async retryFailedOperations(
batchId: string,
operationIds?: string[],
): Promise<BatchStatusDto> {
const batch = await this.batchJobRepository.findOne({
where: { id: batchId },
});
if (!batch) {
throw new HttpException("Batch not found", HttpStatus.NOT_FOUND);
}
// Get failed operations to retry
const where: any = {
batch_id: batchId,
status: BatchOperationStatus.FAILED,
};
if (operationIds && operationIds.length > 0) {
where.id = In(operationIds);
}
const failedOperations = await this.batchOperationRepository.find({
where,
});
if (failedOperations.length === 0) {
throw new HttpException("No failed operations to retry", HttpStatus.BAD_REQUEST);
}
// Reset operations to pending
for (const operation of failedOperations) {
operation.status = BatchOperationStatus.PENDING;
operation.error_message = undefined;
operation.error_code = undefined;
await this.batchOperationRepository.save(operation);
}
// Update batch status
batch.status = BatchJobStatus.PENDING;
batch.error_message = undefined;
await this.batchJobRepository.save(batch);
// Re-queue the job
const queue = batch.type === BatchJobType.SPLIT_CREATION ? this.splitQueue : this.paymentQueue;
const chunkSize = batch.options?.chunkSize || this.defaultChunkSize;
const concurrency = batch.options?.concurrency || this.defaultConcurrency;
await queue.add(
"process",
{
batchId: batch.id,
chunkSize,
concurrency,
} as BatchJobData,
{
jobId: batch.id,
},
);
this.logger.log(`Retrying ${failedOperations.length} failed operations for batch ${batchId}`);
return this.getBatchStatus(batchId);
}
/**
* Cancel a batch job
*/
async cancelBatch(batchId: string): Promise<BatchStatusDto> {
const batch = await this.batchJobRepository.findOne({
where: { id: batchId },
});
if (!batch) {
throw new HttpException("Batch not found", HttpStatus.NOT_FOUND);
}
if (batch.status === BatchJobStatus.COMPLETED || batch.status === BatchJobStatus.FAILED) {
throw new HttpException("Cannot cancel a completed or failed batch", HttpStatus.BAD_REQUEST);
}
// Remove job from queue if still pending
const queue = batch.type === BatchJobType.SPLIT_CREATION ? this.splitQueue : this.paymentQueue;
const job = await queue.getJob(batchId);
if (job) {
await job.remove();
}
// Cancel pending operations
await this.batchOperationRepository.update(
{
batch_id: batchId,
status: In([BatchOperationStatus.PENDING, BatchOperationStatus.PROCESSING]),
},
{
status: BatchOperationStatus.CANCELLED,
completed_at: new Date(),
},
);
// Update batch status
batch.status = BatchJobStatus.CANCELLED;
batch.completed_at = new Date();
await this.batchJobRepository.save(batch);
this.logger.log(`Cancelled batch ${batchId}`);
return this.getBatchStatus(batchId);
}
/**
* Get operations for a batch
*/
async getBatchOperations(
batchId: string,
status?: BatchOperationStatus,
): Promise<BatchOperation[]> {
const where: any = { batch_id: batchId };
if (status) {
where.status = status;
}
return this.batchOperationRepository.find({
where,
order: { operation_index: "ASC" },
});
}
/**
* Map BatchJob entity to BatchStatusDto
*/
private mapToStatusDto(batch: BatchJob): BatchStatusDto {
const dto: BatchStatusDto = {
id: batch.id,
type: batch.type,
status: batch.status,
totalOperations: batch.total_operations,
completedOperations: batch.completed_operations,
failedOperations: batch.failed_operations,
progress: batch.progress,
options: batch.options,
errorMessage: batch.error_message,
startedAt: batch.started_at,
completedAt: batch.completed_at,
createdAt: batch.created_at,
updatedAt: batch.updated_at,
estimatedTimeRemaining: this.batchProgressService.calculateETA(batch),
processingRate: this.batchProgressService.calculateProcessingRate(batch),
};
if (batch.operations) {
dto.operations = batch.operations.map((op) => ({
id: op.id,
operationIndex: op.operation_index,
status: op.status,
errorMessage: op.error_message,
errorCode: op.error_code,
retryCount: op.retry_count,
startedAt: op.started_at,
completedAt: op.completed_at,
}));
}
return dto;
}
}