@@ -7,13 +7,16 @@ export type SyncProvider = "GITLAB" | "AZURE_DEVOPS" | "GITHUB";
77export type QueueBackend = "bull" | "supabase" ;
88
99const backend : QueueBackend = process . env . SYNC_QUEUE_BACKEND === "supabase" ? "supabase" : "bull" ;
10-
11- const connection = new IORedis ( process . env . REDIS_URL ?? "redis://localhost:6379" , {
12- maxRetriesPerRequest : null ,
13- enableReadyCheck : false ,
14- } ) ;
15-
16- export const syncQueue = new Queue ( "contribution-sync" , { connection } ) ;
10+ let queueInstance : Queue | null = null ;
11+ function getSyncQueue ( ) : Queue {
12+ if ( queueInstance ) return queueInstance ;
13+ const connection = new IORedis ( process . env . REDIS_URL ?? "redis://localhost:6379" , {
14+ maxRetriesPerRequest : null ,
15+ enableReadyCheck : false ,
16+ } ) ;
17+ queueInstance = new Queue ( "contribution-sync" , { connection } ) ;
18+ return queueInstance ;
19+ }
1720
1821export type SyncJobOptions = {
1922 from ?: string ;
@@ -60,7 +63,7 @@ export async function enqueueUserSync(userId: string, options?: SyncJobOptions)
6063 return ;
6164 }
6265
63- await syncQueue . add ( "sync-user" , { userId, options } , { attempts : 3 , backoff : { type : "exponential" , delay : 1000 } } ) ;
66+ await getSyncQueue ( ) . add ( "sync-user" , { userId, options } , { attempts : 3 , backoff : { type : "exponential" , delay : 1000 } } ) ;
6467}
6568
6669export async function scheduleNightlySync ( ) {
@@ -69,7 +72,7 @@ export async function scheduleNightlySync() {
6972 return ;
7073 }
7174
72- await syncQueue . add (
75+ await getSyncQueue ( ) . add (
7376 "nightly-sync" ,
7477 { } ,
7578 {
@@ -99,7 +102,7 @@ export async function listBackfillJobsForUser(userId: string, limit = 8): Promis
99102 }
100103
101104 try {
102- const jobs = await syncQueue . getJobs ( [ "waiting" , "active" , "completed" , "failed" , "delayed" ] , 0 , 200 ) ;
105+ const jobs = await getSyncQueue ( ) . getJobs ( [ "waiting" , "active" , "completed" , "failed" , "delayed" ] , 0 , 200 ) ;
103106 const collected : BackfillJobView [ ] = [ ] ;
104107
105108 for ( const job of jobs ) {
@@ -145,7 +148,7 @@ export async function retryBackfillJobForUser(userId: string, jobId: string): Pr
145148 }
146149
147150 try {
148- const job = await syncQueue . getJob ( jobId ) ;
151+ const job = await getSyncQueue ( ) . getJob ( jobId ) ;
149152 if ( ! job || job . name !== "sync-user" || ! isBackfillJob ( job . data ) || job . data . userId !== userId ) {
150153 return { ok : false } ;
151154 }
@@ -177,7 +180,7 @@ export async function removeBackfillJobForUser(userId: string, jobId: string): P
177180 }
178181
179182 try {
180- const job = await syncQueue . getJob ( jobId ) ;
183+ const job = await getSyncQueue ( ) . getJob ( jobId ) ;
181184 if ( ! job || job . name !== "sync-user" || ! isBackfillJob ( job . data ) || job . data . userId !== userId ) {
182185 return { ok : false } ;
183186 }
@@ -198,7 +201,7 @@ export async function removeCompletedBackfillJobsForUser(userId: string): Promis
198201 }
199202
200203 try {
201- const jobs = await syncQueue . getJobs ( [ "completed" ] , 0 , 300 ) ;
204+ const jobs = await getSyncQueue ( ) . getJobs ( [ "completed" ] , 0 , 300 ) ;
202205 let removed = 0 ;
203206
204207 for ( const job of jobs ) {
0 commit comments