@@ -14,11 +14,7 @@ import {
1414 removeUserFromQueue ,
1515 updateCurrentEloRangeForUser ,
1616} from './queryDB'
17- import {
18- createMatch ,
19- timeSpentInQueue ,
20- updateQueueMessage ,
21- } from './queueHelpers'
17+ import { createMatch , timeSpentInQueue } from './queueHelpers'
2218import { updateMatchCountChannel } from './matchHelpers'
2319import { pool } from '../db'
2420import * as fs from 'fs'
@@ -163,44 +159,21 @@ export async function incrementEloCronJobAllQueues() {
163159 if ( bestPair . length === 2 ) {
164160 const matchupUsers = bestPair . map ( ( u ) => u . userId )
165161
166- // Check with a lock to ensure that only one match is created at a time
167- const dbClient = await pool . connect ( )
168- try {
169- await dbClient . query ( 'BEGIN' )
170-
171- // Lock the rows and check if all users are still in queue
172- const checkResult = await dbClient . query (
173- `SELECT user_id FROM queue_users
174- WHERE user_id = ANY($1::varchar[])
175- AND queue_join_time IS NOT NULL
176- FOR UPDATE` ,
177- [ matchupUsers ] ,
178- )
179-
180- // If not all users are still available, skip this match
181- if ( checkResult . rowCount !== matchupUsers . length ) {
182- await dbClient . query ( 'ROLLBACK' )
183- dbClient . release ( )
184- continue
185- }
186-
187- // Remove users from queue (set queue_join_time to NULL)
188- await dbClient . query (
189- `UPDATE queue_users SET queue_join_time = NULL
190- WHERE user_id = ANY($1::varchar[])` ,
191- [ matchupUsers ] ,
192- )
193-
194- await dbClient . query ( 'COMMIT' )
195- dbClient . release ( )
162+ // remove users from all queues they are in
163+ // Wait for all removal operations to complete before creating match
164+ await Promise . all (
165+ matchupUsers . map ( async ( userId ) => {
166+ const userQueues = await getUserQueues ( userId )
167+ await Promise . all (
168+ userQueues . map ( ( queue ) =>
169+ removeUserFromQueue ( queue . id , userId ) ,
170+ ) ,
171+ )
172+ } ) ,
173+ )
196174
197- // Now create the match (users are already removed from queue)
198- await createMatch ( matchupUsers , bestPair [ 0 ] . queueId )
199- } catch ( err ) {
200- await dbClient . query ( 'ROLLBACK' )
201- dbClient . release ( )
202- console . error ( 'Error creating match in incrementEloCronJob:' , err )
203- }
175+ // queue them for the match
176+ await createMatch ( matchupUsers , bestPair [ 0 ] . queueId )
204177 }
205178 }
206179 } catch ( err ) {
@@ -285,14 +258,3 @@ export async function deleteExpiredStrikesCronJob() {
285258 2 * 60 * 60 * 1000 ,
286259 ) // every 2 hours
287260}
288-
289- // update queue message every 2 seconds
290- export async function updateQueueMessageCronJob ( ) {
291- setInterval ( async ( ) => {
292- try {
293- await updateQueueMessage ( )
294- } catch ( err ) {
295- console . error ( 'Error updating queue message:' , err )
296- }
297- } , 2 * 1000 ) // every 2 seconds
298- }
0 commit comments