|
| 1 | +import { BaseEvent } from './base-event' |
| 2 | +import { getConfig } from '../../config' |
| 3 | +import { Job, SendOptions, WorkOptions } from 'pg-boss' |
| 4 | +import { logger, logSchema } from '@internal/monitoring' |
| 5 | +import { Storage } from '../index' |
| 6 | +import { BasePayload } from '@internal/queue' |
| 7 | + |
| 8 | +export interface ObjectDeleteBatchEvent extends BasePayload { |
| 9 | + prefixes: string[] |
| 10 | + bucketId: string |
| 11 | +} |
| 12 | + |
| 13 | +const { storageS3Bucket, adminDeleteQueueTeamSize, adminDeleteConcurrency } = getConfig() |
| 14 | + |
| 15 | +export class ObjectAdminDeleteBatch extends BaseEvent<ObjectDeleteBatchEvent> { |
| 16 | + static queueName = 'object:admin:delete-batch' |
| 17 | + |
| 18 | + static getWorkerOptions(): WorkOptions { |
| 19 | + return { |
| 20 | + teamSize: adminDeleteQueueTeamSize, |
| 21 | + teamConcurrency: adminDeleteConcurrency, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + static getQueueOptions(): SendOptions { |
| 26 | + return { |
| 27 | + priority: 10, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + static async handle(job: Job<ObjectDeleteBatchEvent>) { |
| 32 | + console.log('in ObjectAdminDeleteBatch.handle()') |
| 33 | + let storage: Storage | undefined = undefined |
| 34 | + |
| 35 | + const { prefixes, bucketId } = job.data |
| 36 | + if (prefixes.length < 1) { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + storage = await this.createStorage(job.data) |
| 42 | + |
| 43 | + logSchema.event(logger, `[Admin]: ObjectAdminDeleteBatch ${bucketId} ${prefixes.length}`, { |
| 44 | + jodId: job.id, |
| 45 | + type: 'event', |
| 46 | + event: 'ObjectAdminDeleteBatch', |
| 47 | + payload: JSON.stringify(job.data), |
| 48 | + objectPath: bucketId, |
| 49 | + resources: prefixes, |
| 50 | + tenantId: job.data.tenant.ref, |
| 51 | + project: job.data.tenant.ref, |
| 52 | + reqId: job.data.reqId, |
| 53 | + }) |
| 54 | + |
| 55 | + await storage.backend.deleteObjects(storageS3Bucket, prefixes) |
| 56 | + } catch (e) { |
| 57 | + logger.error( |
| 58 | + { |
| 59 | + error: e, |
| 60 | + jodId: job.id, |
| 61 | + type: 'event', |
| 62 | + event: 'ObjectAdminDeleteBatch', |
| 63 | + payload: JSON.stringify(job.data), |
| 64 | + objectPath: bucketId, |
| 65 | + resources: prefixes, |
| 66 | + tenantId: job.data.tenant.ref, |
| 67 | + project: job.data.tenant.ref, |
| 68 | + reqId: job.data.reqId, |
| 69 | + }, |
| 70 | + `[Admin]: ObjectAdminDeleteBatch ${bucketId} ${prefixes.length} - FAILED` |
| 71 | + ) |
| 72 | + throw e |
| 73 | + } finally { |
| 74 | + if (storage) { |
| 75 | + const tenant = storage.db.tenant() |
| 76 | + storage.db |
| 77 | + .destroyConnection() |
| 78 | + .then(() => { |
| 79 | + // no-op |
| 80 | + }) |
| 81 | + .catch((e) => { |
| 82 | + logger.error( |
| 83 | + { error: e }, |
| 84 | + `[Admin]: ObjectAdminDeleteBatch ${tenant.ref} - FAILED DISPOSING CONNECTION` |
| 85 | + ) |
| 86 | + }) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments