db code, check if database is up before any select operation
This commit is contained in:
parent
10f25b8810
commit
16b6b4edf9
96
src/db.js
96
src/db.js
@ -92,17 +92,39 @@ 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,]
|
||||
[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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
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]);
|
||||
@ -128,10 +150,25 @@ const database = {
|
||||
return roomid;
|
||||
} catch (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) => {
|
||||
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',
|
||||
@ -142,8 +179,23 @@ const database = {
|
||||
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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
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',
|
||||
@ -154,8 +206,23 @@ const database = {
|
||||
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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
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
|
||||
@ -172,8 +239,23 @@ const database = {
|
||||
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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
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);
|
||||
@ -182,6 +264,16 @@ const database = {
|
||||
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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user