|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { MinioService } from 'omniboxd/minio/minio.service'; |
| 3 | +import { ConfigService } from '@nestjs/config'; |
| 4 | + |
| 5 | +@Injectable() |
| 6 | +export class ChunkManagerService { |
| 7 | + private readonly logger = new Logger(ChunkManagerService.name); |
| 8 | + private readonly cleanupDelay: number; |
| 9 | + |
| 10 | + constructor( |
| 11 | + private readonly minioService: MinioService, |
| 12 | + private readonly configService: ConfigService, |
| 13 | + ) { |
| 14 | + this.cleanupDelay = |
| 15 | + parseInt(this.configService.get('OBB_CHUNK_CLEANUP_DELAY', '60'), 10) * |
| 16 | + 1000; // Convert to milliseconds |
| 17 | + } |
| 18 | + |
| 19 | + async storeChunk( |
| 20 | + taskId: string, |
| 21 | + chunkIndex: number, |
| 22 | + totalChunks: number, |
| 23 | + data: string, |
| 24 | + ): Promise<void> { |
| 25 | + const chunkPath = this.getChunkPath(taskId, chunkIndex); |
| 26 | + const buffer = Buffer.from(data, 'base64'); |
| 27 | + |
| 28 | + try { |
| 29 | + await this.minioService.putChunkObject(chunkPath, buffer, buffer.length); |
| 30 | + this.logger.debug( |
| 31 | + `Stored chunk ${chunkIndex + 1}/${totalChunks} for task ${taskId}`, |
| 32 | + ); |
| 33 | + } catch (error) { |
| 34 | + this.logger.error( |
| 35 | + `Failed to store chunk ${chunkIndex} for task ${taskId}:`, |
| 36 | + error, |
| 37 | + ); |
| 38 | + throw error; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async assembleChunks(taskId: string, totalChunks: number): Promise<string> { |
| 43 | + const assembledPath = this.getAssembledPath(taskId); |
| 44 | + const chunkPaths = Array.from({ length: totalChunks }, (_, i) => |
| 45 | + this.getChunkPath(taskId, i), |
| 46 | + ); |
| 47 | + |
| 48 | + try { |
| 49 | + // Use MinIO's composeObject to merge all chunks |
| 50 | + await this.minioService.composeObject(assembledPath, chunkPaths); |
| 51 | + |
| 52 | + // Retrieve the assembled data |
| 53 | + const stream = await this.minioService.getObject(assembledPath); |
| 54 | + const chunks: Buffer[] = []; |
| 55 | + |
| 56 | + return new Promise((resolve, reject) => { |
| 57 | + stream.on('data', (chunk) => chunks.push(chunk)); |
| 58 | + stream.on('end', () => { |
| 59 | + const assembledBuffer = Buffer.concat(chunks); |
| 60 | + const assembledData = assembledBuffer.toString('utf-8'); |
| 61 | + resolve(assembledData); |
| 62 | + }); |
| 63 | + stream.on('error', reject); |
| 64 | + }); |
| 65 | + } catch (error) { |
| 66 | + this.logger.error(`Failed to assemble chunks for task ${taskId}:`, error); |
| 67 | + throw error; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + cleanupChunks(taskId: string, totalChunks: number): void { |
| 72 | + // Schedule cleanup after delay to allow for any final processing |
| 73 | + setTimeout(() => { |
| 74 | + this.performCleanup(taskId, totalChunks).catch((error) => { |
| 75 | + this.logger.error( |
| 76 | + `Failed to cleanup chunks for task ${taskId}:`, |
| 77 | + error, |
| 78 | + ); |
| 79 | + }); |
| 80 | + }, this.cleanupDelay); |
| 81 | + } |
| 82 | + |
| 83 | + private async performCleanup( |
| 84 | + taskId: string, |
| 85 | + totalChunks: number, |
| 86 | + ): Promise<void> { |
| 87 | + try { |
| 88 | + const objectsToRemove: string[] = []; |
| 89 | + |
| 90 | + // Add chunk paths |
| 91 | + for (let i = 0; i < totalChunks; i++) { |
| 92 | + objectsToRemove.push(this.getChunkPath(taskId, i)); |
| 93 | + } |
| 94 | + |
| 95 | + // Add assembled path |
| 96 | + objectsToRemove.push(this.getAssembledPath(taskId)); |
| 97 | + |
| 98 | + // Remove all objects |
| 99 | + await Promise.all( |
| 100 | + objectsToRemove.map((objectName) => |
| 101 | + this.minioService.removeObject(objectName).catch((error) => { |
| 102 | + this.logger.warn(`Failed to remove object ${objectName}:`, error); |
| 103 | + }), |
| 104 | + ), |
| 105 | + ); |
| 106 | + |
| 107 | + this.logger.debug(`Cleaned up chunks for task ${taskId}`); |
| 108 | + } catch (error) { |
| 109 | + this.logger.error(`Failed to cleanup chunks for task ${taskId}:`, error); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private getChunkPath(taskId: string, chunkIndex: number): string { |
| 114 | + return `wizard-chunks/${taskId}/chunk-${chunkIndex.toString().padStart(6, '0')}`; |
| 115 | + } |
| 116 | + |
| 117 | + private getAssembledPath(taskId: string): string { |
| 118 | + return `wizard-chunks/${taskId}/assembled`; |
| 119 | + } |
| 120 | +} |
0 commit comments