@@ -44,6 +44,7 @@ import {
4444import { runDetached } from '@/lib/core/utils/background'
4545import { generateRequestId } from '@/lib/core/utils/request'
4646import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
47+ import { tryAcquireAdvisoryXactLock } from '@/lib/db/advisory-locks'
4748import type { DbOrTx } from '@/lib/db/types'
4849import {
4950 registerManualExecutionAborter ,
@@ -885,10 +886,12 @@ async function recoverStaleDatabaseScheduleJobs(now: Date): Promise<void> {
885886 const disabledScheduleIds = new Set < string > ( )
886887
887888 await db . transaction ( async ( tx ) => {
888- const [ lock ] = await tx . execute < { acquired : boolean } > (
889- sql `SELECT pg_try_advisory_xact_lock(hashtextextended(${ SCHEDULE_EXECUTION_QUEUE_NAME } , 0)) AS acquired`
889+ const acquired = await tryAcquireAdvisoryXactLock (
890+ tx ,
891+ 'schedule_execution_queue' ,
892+ SCHEDULE_EXECUTION_QUEUE_NAME
890893 )
891- if ( ! lock ?. acquired ) {
894+ if ( ! acquired ) {
892895 logger . info (
893896 'Skipped stale database schedule job recovery because another worker holds the lock'
894897 )
@@ -1065,10 +1068,12 @@ async function tryStartDatabaseScheduleJob(jobId: string): Promise<DatabaseSched
10651068 const now = new Date ( )
10661069
10671070 return db . transaction ( async ( tx ) => {
1068- const [ lock ] = await tx . execute < { acquired : boolean } > (
1069- sql `SELECT pg_try_advisory_xact_lock(hashtextextended(${ SCHEDULE_EXECUTION_QUEUE_NAME } , 0)) AS acquired`
1071+ const acquired = await tryAcquireAdvisoryXactLock (
1072+ tx ,
1073+ 'schedule_execution_queue' ,
1074+ SCHEDULE_EXECUTION_QUEUE_NAME
10701075 )
1071- if ( ! lock ?. acquired ) return 'capacity_full'
1076+ if ( ! acquired ) return 'capacity_full'
10721077
10731078 const [ row ] = await tx
10741079 . select ( {
0 commit comments