|
| 1 | +import { BaseEvent } from '../base-event' |
| 2 | +import { Job, Queue as PgBossQueue, SendOptions, WorkOptions } from 'pg-boss' |
| 3 | +import { BasePayload, Queue } from '@internal/queue' |
| 4 | +import { multitenantKnex } from '@internal/database' |
| 5 | +import { logger, logSchema } from '@internal/monitoring' |
| 6 | + |
| 7 | +interface MoveJobsPayload extends BasePayload { |
| 8 | + fromQueue: string |
| 9 | + toQueue: string |
| 10 | + deleteJobsFromOriginalQueue?: boolean |
| 11 | +} |
| 12 | + |
| 13 | +export class MoveJobs extends BaseEvent<MoveJobsPayload> { |
| 14 | + static queueName = 'move-jobs' |
| 15 | + |
| 16 | + static getQueueOptions(): PgBossQueue { |
| 17 | + return { |
| 18 | + name: this.queueName, |
| 19 | + policy: 'exactly_once', |
| 20 | + } as const |
| 21 | + } |
| 22 | + |
| 23 | + static getWorkerOptions(): WorkOptions { |
| 24 | + return { |
| 25 | + includeMetadata: true, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + static getSendOptions(payload: MoveJobsPayload): SendOptions { |
| 30 | + return { |
| 31 | + expireInHours: 2, |
| 32 | + singletonKey: `move_${payload.fromQueue}_to_${payload.toQueue}`, |
| 33 | + singletonHours: 12, |
| 34 | + retryLimit: 3, |
| 35 | + retryDelay: 5, |
| 36 | + priority: 10, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static async handle(job: Job<MoveJobsPayload>) { |
| 41 | + await multitenantKnex.transaction(async (tnx) => { |
| 42 | + const resultLock = await tnx.raw('SELECT pg_try_advisory_xact_lock(-5525285245963000611)') |
| 43 | + const lockAcquired = resultLock.rows.shift()?.pg_try_advisory_xact_lock || false |
| 44 | + |
| 45 | + if (!lockAcquired) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + const schema = 'pgboss_v10' |
| 50 | + const fromQueueName = job.data.fromQueue |
| 51 | + const toQueue = await Queue.getInstance().getQueue(job.data.toQueue) |
| 52 | + |
| 53 | + if (!toQueue) { |
| 54 | + logSchema.error(logger, `[PgBoss] Target queue ${job.data.toQueue} does not exist`, { |
| 55 | + type: 'pgboss', |
| 56 | + }) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + const sql = ` |
| 62 | + INSERT INTO ${schema}.job ( |
| 63 | + id, |
| 64 | + name, |
| 65 | + priority, |
| 66 | + data, |
| 67 | + retry_limit, |
| 68 | + retry_count, |
| 69 | + retry_delay, |
| 70 | + retry_backoff, |
| 71 | + start_after, |
| 72 | + singleton_key, |
| 73 | + singleton_on, |
| 74 | + expire_in, |
| 75 | + created_on, |
| 76 | + keep_until, |
| 77 | + output, |
| 78 | + policy, |
| 79 | + state |
| 80 | + ) |
| 81 | + SELECT |
| 82 | + id, |
| 83 | + '${toQueue.name}' as name, |
| 84 | + priority, |
| 85 | + data, |
| 86 | + retry_limit, |
| 87 | + retry_count, |
| 88 | + retry_delay, |
| 89 | + retry_backoff, |
| 90 | + start_after, |
| 91 | + singleton_key, |
| 92 | + singleton_on, |
| 93 | + expire_in, |
| 94 | + created_on, |
| 95 | + keep_until, |
| 96 | + output, |
| 97 | + '${toQueue.policy}' as policy, |
| 98 | + 'created' as state |
| 99 | + FROM ${schema}.job |
| 100 | + WHERE name = '${fromQueueName}' |
| 101 | + AND state IN ('created', 'active', 'retry') |
| 102 | + ON CONFLICT DO NOTHING |
| 103 | + ` |
| 104 | + |
| 105 | + await tnx.raw(sql) |
| 106 | + |
| 107 | + if (job.data.deleteJobsFromOriginalQueue) { |
| 108 | + const deleteSql = ` |
| 109 | + DELETE FROM ${schema}.job |
| 110 | + WHERE name = '${fromQueueName}' |
| 111 | + AND state IN ('created', 'active', 'retry') |
| 112 | + ` |
| 113 | + await tnx.raw(deleteSql) |
| 114 | + } |
| 115 | + } catch (error) { |
| 116 | + logSchema.error(logger, '[PgBoss] Error while copying jobs', { |
| 117 | + type: 'pgboss', |
| 118 | + error, |
| 119 | + }) |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments