|
| 1 | +/** |
| 2 | + * Billing job queue — wires payment/dunning jobs to the WFQ scheduler. |
| 3 | + */ |
| 4 | + |
| 5 | +import type { ConnectionOptions } from 'bullmq'; |
| 6 | +import { |
| 7 | + createJobQueueSystem, |
| 8 | + type JobHandlerMap, |
| 9 | + type JobQueueSystem, |
| 10 | + type QueueJob, |
| 11 | + type WeightedFairQueue, |
| 12 | +} from '../../shared/queue'; |
| 13 | +import { createPaymentJobHandlers } from './paymentJobHandlers'; |
| 14 | +import { |
| 15 | + PAYMENT_JOB_PRIORITY, |
| 16 | + type ChargeSettlementPayload, |
| 17 | + type PaymentConfirmationPayload, |
| 18 | + enqueueChargeSettlement, |
| 19 | + enqueuePaymentConfirmation, |
| 20 | +} from './paymentConfirmationJob'; |
| 21 | + |
| 22 | +export interface BillingJobQueueConfig { |
| 23 | + connection: ConnectionOptions; |
| 24 | + baseQueueName?: string; |
| 25 | + maxQueueSize?: number; |
| 26 | + paymentHandlers?: JobHandlerMap; |
| 27 | +} |
| 28 | + |
| 29 | +export class BillingJobQueue { |
| 30 | + private readonly system: JobQueueSystem; |
| 31 | + private readonly handlers: JobHandlerMap; |
| 32 | + |
| 33 | + constructor(config: BillingJobQueueConfig) { |
| 34 | + this.system = createJobQueueSystem({ |
| 35 | + connection: config.connection, |
| 36 | + baseQueueName: config.baseQueueName ?? 'subtrackr:billing', |
| 37 | + maxQueueSize: config.maxQueueSize, |
| 38 | + }); |
| 39 | + this.handlers = config.paymentHandlers ?? createPaymentJobHandlers(); |
| 40 | + } |
| 41 | + |
| 42 | + get scheduler(): WeightedFairQueue { |
| 43 | + return this.system.scheduler; |
| 44 | + } |
| 45 | + |
| 46 | + /** Enqueue a payment confirmation at critical priority (with auto backpressure). */ |
| 47 | + async schedulePaymentConfirmation( |
| 48 | + payload: PaymentConfirmationPayload, |
| 49 | + ): Promise<QueueJob<PaymentConfirmationPayload>> { |
| 50 | + return this.system.scheduler.enqueue( |
| 51 | + PAYMENT_JOB_PRIORITY, |
| 52 | + 'payment:confirmation-email', |
| 53 | + payload, |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** Enqueue charge settlement at critical priority. */ |
| 58 | + async scheduleChargeSettlement( |
| 59 | + payload: ChargeSettlementPayload, |
| 60 | + ): Promise<QueueJob<ChargeSettlementPayload>> { |
| 61 | + return this.system.scheduler.enqueue(PAYMENT_JOB_PRIORITY, 'payment:charge-settlement', payload); |
| 62 | + } |
| 63 | + |
| 64 | + /** Process one job from the queue. */ |
| 65 | + async processNext(): Promise<boolean> { |
| 66 | + return this.system.scheduler.processNext(this.handlers); |
| 67 | + } |
| 68 | + |
| 69 | + /** Start the background worker loop. */ |
| 70 | + start(intervalMs = 50): void { |
| 71 | + this.system.scheduler.startProcessing(this.handlers, intervalMs); |
| 72 | + } |
| 73 | + |
| 74 | + stop(): void { |
| 75 | + this.system.scheduler.stopProcessing(); |
| 76 | + } |
| 77 | + |
| 78 | + async close(): Promise<void> { |
| 79 | + await this.system.scheduler.close(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Re-export convenience enqueue functions that use WFQ |
| 84 | +export { enqueuePaymentConfirmation, enqueueChargeSettlement }; |
0 commit comments