|
1 | | -export function conditionalQueueProcessorProviders<T>(...providers: T[]): T[] { |
2 | | - return process.env.BACKEND_DISABLE_QUEUE_CONSUMER === 'true' ? [] : providers; |
| 1 | +import { Logger } from '@nestjs/common'; |
| 2 | + |
| 3 | +export enum QueueConsumerType { |
| 4 | + Automation = 'automation', |
| 5 | + ImportExport = 'import-export', |
| 6 | + ImageCrop = 'image-crop', |
| 7 | + Default = 'default', |
| 8 | +} |
| 9 | + |
| 10 | +export function conditionalQueueProcessorProviders<T>( |
| 11 | + ...opts: { |
| 12 | + consumer?: QueueConsumerType; |
| 13 | + providers: T[]; |
| 14 | + }[] |
| 15 | +): T[] { |
| 16 | + if (process.env.BACKEND_DISABLE_QUEUE_CONSUMER === 'true') { |
| 17 | + return []; |
| 18 | + } |
| 19 | + const selectedConsumer = (process.env.BACKEND_QUEUE_CONSUMER?.split(',')?.filter((v) => |
| 20 | + Object.values(QueueConsumerType).includes(v as QueueConsumerType) |
| 21 | + ) || []) as QueueConsumerType[]; |
| 22 | + |
| 23 | + // If selected consumer is provided, return providers for the selected consumer |
| 24 | + if (selectedConsumer.length > 0) { |
| 25 | + const providers: T[] = []; |
| 26 | + for (const opt of opts) { |
| 27 | + const consumer = opt.consumer || QueueConsumerType.Default; |
| 28 | + if (selectedConsumer.includes(consumer)) { |
| 29 | + providers.push(...opt.providers); |
| 30 | + } |
| 31 | + } |
| 32 | + providers.length > 0 && |
| 33 | + Logger.log( |
| 34 | + `Queue Consumer Providers (${selectedConsumer.join(', ')}): ${providers.map((p) => (typeof p === 'function' ? p.name : 'unknown')).join(', ')}` |
| 35 | + ); |
| 36 | + return providers; |
| 37 | + } |
| 38 | + |
| 39 | + // If no selected consumer is provided, return providers for all consumers |
| 40 | + const providers: T[] = []; |
| 41 | + for (const opt of opts) { |
| 42 | + providers.push(...opt.providers); |
| 43 | + } |
| 44 | + providers.length > 0 && |
| 45 | + Logger.log( |
| 46 | + `Queue Consumer Providers (ALL): ${providers.map((p) => (typeof p === 'function' ? p.name : 'unknown')).join(', ')}` |
| 47 | + ); |
| 48 | + return providers; |
3 | 49 | } |
0 commit comments