From 512b827b40f780e7ff04c5d2ee63522413b874d7 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Wed, 5 Mar 2025 14:05:09 +0100 Subject: [PATCH] refactor db up check in functions --- src/db.js | 263 ++++++++++++++++++++---------------------------------- 1 file changed, 99 insertions(+), 164 deletions(-) diff --git a/src/db.js b/src/db.js index d05bc33..2dc344c 100644 --- a/src/db.js +++ b/src/db.js @@ -92,189 +92,124 @@ const database = { console.error("Pubkey is required"); return; } - const maxRetries = 5; - let retries = 0; - while (retries < maxRetries) { - const dbUp = await database.checkIfDatabaseIsUp(); - if (dbUp) { - try { - await pool.query( - 'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)', - [pubkey] - ); - console.log(`Added user with the public key ${pubkey}.`); - return; - } catch (err) { - console.error('Error adding user:', err); - throw err; - } - } else { - console.log('Waiting for the database to start...'); - retries += 1; - if (retries < maxRetries) { - await new Promise(resolve => setTimeout(resolve, 10000)); - } else { - console.error(`Failed to connect to the database after ${maxRetries} attempts.`); - } - } + await retryWithDelay(database.checkIfDatabaseIsUp, 5); + try { + await pool.query( + 'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)', + [pubkey] + ); + console.log(`Added user with the public key ${pubkey}.`); + return; + } catch (err) { + console.error('Error adding user:', err); + throw err; } }, createRoom: async (pubkey1, pubkey2) => { - const maxRetries = 5; - let retries = 0; - while (retries < maxRetries) { - const dbUp = await database.checkIfDatabaseIsUp(); - if (dbUp) { - try { - const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1'; - const uuidRes1 = await pool.query(userQuery, [pubkey1]); - const uuidRes2 = await pool.query(userQuery, [pubkey2]); + await retryWithDelay(database.checkIfDatabaseIsUp, 5); + try { + const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1'; + const uuidRes1 = await pool.query(userQuery, [pubkey1]); + const uuidRes2 = await pool.query(userQuery, [pubkey2]); - if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) { - throw new Error('One or both users not found'); - } - - const uuid1 = uuidRes1.rows[0].uuid; - const uuid2 = uuidRes2.rows[0].uuid; - - const roomRes = await pool.query( - 'INSERT INTO room (uuid) VALUES (DEFAULT) RETURNING uuid' - ); - const roomid = roomRes.rows[0].uuid; - - await pool.query( - `INSERT INTO room_members (room_uuid, user_uuid) - VALUES ($1, $2), ($1, $3)`, - [roomid, uuid1, uuid2] - ); - return roomid; - } catch (err) { - console.error('Error creating the room:', err); - throw err; - } - } else { - console.log('Waiting for the database to start...'); - retries += 1; - if (retries < maxRetries) { - await new Promise(resolve => setTimeout(resolve, 10000)); - } else { - console.error(`Failed to connect to the database after ${maxRetries} attempts.`); - } + if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) { + throw new Error('One or both users not found'); } + + const uuid1 = uuidRes1.rows[0].uuid; + const uuid2 = uuidRes2.rows[0].uuid; + + const roomRes = await pool.query( + 'INSERT INTO room (uuid) VALUES (DEFAULT) RETURNING uuid' + ); + const roomid = roomRes.rows[0].uuid; + + await pool.query( + `INSERT INTO room_members (room_uuid, user_uuid) + VALUES ($1, $2), ($1, $3)`, + [roomid, uuid1, uuid2] + ); + return roomid; + } catch (err) { + console.error('Error creating the room:', err); + throw err; } }, getRooms: async (pubkey) => { - const maxRetries = 5; - let retries = 0; - while (retries < maxRetries) { - const dbUp = await database.checkIfDatabaseIsUp(); - if (dbUp) { - try { - const roomsRes = await pool.query( - 'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1', - [pubkey] - ); - return roomsRes.rows.map(row => row.room_uuid); - } catch (err) { - console.error('Error retrieving rooms:', err); - throw err; - } - } else { - console.log('Waiting for the database to start...'); - retries += 1; - if (retries < maxRetries) { - await new Promise(resolve => setTimeout(resolve, 10000)); - } else { - console.error(`Failed to connect to the database after ${maxRetries} attempts.`); - } - } + await retryWithDelay(database.checkIfDatabaseIsUp, 5); + try { + const roomsRes = await pool.query( + 'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1', + [pubkey] + ); + return roomsRes.rows.map(row => row.room_uuid); + } catch (err) { + console.error('Error retrieving rooms:', err); + throw err; } }, getRoomMembers: async (roomid) => { - const maxRetries = 5; - let retries = 0; - while (retries < maxRetries) { - const dbUp = await database.checkIfDatabaseIsUp(); - if (dbUp) { - try { - const members = await pool.query( - 'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1', - [roomid] - ); - return members.rows.map(row => row.pubkey); - } catch (err) { - console.error('Error retrieving rooms:', err); - throw err; - } - } else { - console.log('Waiting for the database to start...'); - retries += 1; - if (retries < maxRetries) { - await new Promise(resolve => setTimeout(resolve, 10000)); - } else { - console.error(`Failed to connect to the database after ${maxRetries} attempts.`); - } - } + await retryWithDelay(database.checkIfDatabaseIsUp, 5); + try { + const members = await pool.query( + 'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1', + [roomid] + ); + return members.rows.map(row => row.pubkey); + } catch (err) { + console.error('Error retrieving rooms:', err); + throw err; } }, getPeers: async (pubkey) => { - const maxRetries = 5; - let retries = 0; - while (retries < maxRetries) { - const dbUp = await database.checkIfDatabaseIsUp(); - if (dbUp) { - try { - const peers = await pool.query( - `SELECT u1.pubkey - FROM room_members r1, room_members r2, users u1, users u2 - WHERE r1.user_uuid = u1.uuid - AND r2.user_uuid = u2.uuid - AND r1.room_uuid = r2.room_uuid - AND u2.pubkey != u1.pubkey - AND u2.pubkey = $1`, - [pubkey] - ); - return peers.rows.map(row => row.pubkey); - } catch (err) { - console.error('Error retrieving peers:', err); - throw err; - } - } else { - console.log('Waiting for the database to start...'); - retries += 1; - if (retries < maxRetries) { - await new Promise(resolve => setTimeout(resolve, 10000)); - } else { - console.error(`Failed to connect to the database after ${maxRetries} attempts.`); - } - } + await retryWithDelay(database.checkIfDatabaseIsUp, 5); + try { + const peers = await pool.query( + `SELECT u1.pubkey + FROM room_members r1, room_members r2, users u1, users u2 + WHERE r1.user_uuid = u1.uuid + AND r2.user_uuid = u2.uuid + AND r1.room_uuid = r2.room_uuid + AND u2.pubkey != u1.pubkey + AND u2.pubkey = $1`, + [pubkey] + ); + return peers.rows.map(row => row.pubkey); + } catch (err) { + console.error('Error retrieving peers:', err); + throw err; } }, getPublicKeys: async () => { - const maxRetries = 5; - let retries = 0; - while (retries < maxRetries) { - const dbUp = await database.checkIfDatabaseIsUp(); - if (dbUp) { - try { - const result = await pool.query('SELECT pubkey FROM users'); - const publicKeys = result.rows.map(row => row.pubkey); - return publicKeys; - } catch (err) { - console.error('Error retrieving public keys:', err); - throw new Error('Error retrieving public keys'); - } - } else { - console.log('Waiting for the database to start...'); - retries += 1; - if (retries < maxRetries) { - await new Promise(resolve => setTimeout(resolve, 10000)); - } else { - console.error(`Failed to connect to the database after ${maxRetries} attempts.`); - } - } + await retryWithDelay(database.checkIfDatabaseIsUp, 5); + try { + const result = await pool.query('SELECT pubkey FROM users'); + const publicKeys = result.rows.map(row => row.pubkey); + return publicKeys; + } catch (err) { + console.error('Error retrieving public keys:', err); + throw new Error('Error retrieving public keys'); } } }; +const retryWithDelay = (operation, retries = 5, delay = 5000) => new Promise((resolve, reject) => { + return operation() + .then(resolve) + .catch((err) => { + if (retries > 0) { + return wait(delay) + .then(retryWithDelay.bind(null, operation, retries - 1, delay)) + .then(resolve) + .catch(reject); + } + + return reject(err); + }); +}); + +const wait = (time_in_ms) => new Promise((resolve) => { + setTimeout(() => resolve(), time_in_ms); +}); + module.exports = database;