|
| 1 | +/** |
| 2 | + * Migration Script: |
| 3 | + * - Finds uploads stuck in 'PROCESSING' status |
| 4 | + * - Republishes them to the END_IMPORT queue to restart processing |
| 5 | + */ |
| 6 | + |
| 7 | +import '../../config'; |
| 8 | +import { AppModule } from '../../app.module'; |
| 9 | +import { NestFactory } from '@nestjs/core'; |
| 10 | +import { UploadStatusEnum, QueuesEnum } from '@impler/shared'; |
| 11 | +import { UploadRepository, TemplateRepository } from '@impler/dal'; |
| 12 | +import { QueueService } from '@shared/services/queue.service'; |
| 13 | + |
| 14 | +async function run() { |
| 15 | + console.log('Starting migration: reprocessing uploads in PROCESSING state'); |
| 16 | + |
| 17 | + const app = await NestFactory.create(AppModule, { logger: false }); |
| 18 | + |
| 19 | + try { |
| 20 | + const uploadRepository = app.get(UploadRepository); |
| 21 | + const templateRepository = app.get(TemplateRepository); |
| 22 | + const queueService = app.get(QueueService); |
| 23 | + |
| 24 | + const processingUploads = await uploadRepository.find({ |
| 25 | + status: UploadStatusEnum.PROCESSING, |
| 26 | + }); |
| 27 | + |
| 28 | + if (!processingUploads.length) { |
| 29 | + console.log('No uploads found in PROCESSING state.'); |
| 30 | + |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + console.log(`Found ${processingUploads.length} uploads. Republishing to queue.`); |
| 35 | + |
| 36 | + for (const upload of processingUploads) { |
| 37 | + const template = await templateRepository.findOne({ _id: upload._templateId }); |
| 38 | + |
| 39 | + if (template?.destination) { |
| 40 | + queueService.publishToQueue(QueuesEnum.END_IMPORT, { |
| 41 | + uploadId: upload._id, |
| 42 | + uploadedFileId: upload._uploadedFileId, |
| 43 | + destination: template.destination, |
| 44 | + }); |
| 45 | + |
| 46 | + console.log(`Send upload ${upload._id} to destination ${template.destination}`); |
| 47 | + } else { |
| 48 | + console.warn(`No destination found for upload ${upload._id}`); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + console.log('Migration completed.'); |
| 53 | + } catch (error) { |
| 54 | + console.error('Migration failed:', error); |
| 55 | + } finally { |
| 56 | + await app.close(); |
| 57 | + process.exit(0); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +run(); |
0 commit comments