db code, check if database is up before any select operation

This commit is contained in:
Sam Hadow 2025-03-05 13:37:03 +01:00
parent 10f25b8810
commit 16b6b4edf9

View File

@ -92,17 +92,39 @@ const database = {
console.error("Pubkey is required"); console.error("Pubkey is required");
return; return;
} }
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try { try {
await pool.query( await pool.query(
'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)', 'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)',
[pubkey,] [pubkey]
); );
console.log(`Added user with the public key ${pubkey} .`); console.log(`Added user with the public key ${pubkey}.`);
return;
} catch (err) { } catch (err) {
console.error('Error adding user:', 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.`);
}
}
} }
}, },
createRoom: async (pubkey1, pubkey2) => { createRoom: async (pubkey1, pubkey2) => {
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try { try {
const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1'; const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1';
const uuidRes1 = await pool.query(userQuery, [pubkey1]); const uuidRes1 = await pool.query(userQuery, [pubkey1]);
@ -128,10 +150,25 @@ const database = {
return roomid; return roomid;
} catch (err) { } catch (err) {
console.error('Error creating the room:', err); console.error('Error creating the room:', err);
throw err; // Re-throw to handle in calling code 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.`);
}
}
} }
}, },
getRooms: async (pubkey) => { getRooms: async (pubkey) => {
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try { try {
const roomsRes = await pool.query( 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', 'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1',
@ -142,8 +179,23 @@ const database = {
console.error('Error retrieving rooms:', err); console.error('Error retrieving rooms:', err);
throw 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.`);
}
}
}
}, },
getRoomMembers: async (roomid) => { getRoomMembers: async (roomid) => {
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try { try {
const members = await pool.query( 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', 'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1',
@ -154,8 +206,23 @@ const database = {
console.error('Error retrieving rooms:', err); console.error('Error retrieving rooms:', err);
throw 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.`);
}
}
}
}, },
getPeers: async (pubkey) => { getPeers: async (pubkey) => {
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try { try {
const peers = await pool.query( const peers = await pool.query(
`SELECT u1.pubkey `SELECT u1.pubkey
@ -172,8 +239,23 @@ const database = {
console.error('Error retrieving peers:', err); console.error('Error retrieving peers:', err);
throw 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.`);
}
}
}
}, },
getPublicKeys: async () => { getPublicKeys: async () => {
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try { try {
const result = await pool.query('SELECT pubkey FROM users'); const result = await pool.query('SELECT pubkey FROM users');
const publicKeys = result.rows.map(row => row.pubkey); const publicKeys = result.rows.map(row => row.pubkey);
@ -182,6 +264,16 @@ const database = {
console.error('Error retrieving public keys:', err); console.error('Error retrieving public keys:', err);
throw new Error('Error retrieving public keys'); 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.`);
}
}
}
} }
}; };