fix: Don't crash on failed DB connection

This commit is contained in:
April Hall 2025-02-13 10:09:11 -05:00
parent 7cf2c138b6
commit 644deb31d7
No known key found for this signature in database
GPG Key ID: A49AC35CB186266C

View File

@ -5,6 +5,10 @@ interface Messages {
error: Error | null; error: Error | null;
} }
function createDelay(ms: number) {
return new Promise((res) => setTimeout(res, ms));
}
function sanitizeChannelName(channelName: string) { function sanitizeChannelName(channelName: string) {
return channelName return channelName
.toLowerCase() .toLowerCase()
@ -21,17 +25,20 @@ class Db {
// Initalize and connect // Initalize and connect
async init() { async init() {
while (true) {
try { try {
await this.client.connect(); await this.client.connect();
} catch (e) { break;
console.log(`Error connecting to DB: ${e as Error}`); } catch {
process.exit(1); console.error(`Error connecting to DB. Retrying.....}`);
await createDelay(1000);
}
} }
try { try {
await this.client.execute(`CREATE KEYSPACE IF NOT EXISTS channels WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`); await this.client.execute(`CREATE KEYSPACE IF NOT EXISTS channels WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
} catch (e) { } catch (e) {
console.log(`Error generating keyspaces: ${e as Error}`); console.error(`Error generating keyspace: ${e as Error}`);
process.exit(1); process.exit(1);
} }
} }
@ -50,7 +57,7 @@ class Db {
PRIMARY KEY (channel_name, timestamp) PRIMARY KEY (channel_name, timestamp)
) WITH CLUSTERING ORDER BY (timestamp DESC);`); ) WITH CLUSTERING ORDER BY (timestamp DESC);`);
} catch (e) { } catch (e) {
console.log(`Error creating new channel: ${e as Error}`); console.error(`Error creating new channel: ${e as Error}`);
} }
} }
@ -67,7 +74,7 @@ class Db {
sender, sender,
}); });
} catch (e) { } catch (e) {
console.log(`Error storing message: ${e as Error}`); console.error(`Error storing message: ${e as Error}`);
} }
} }
@ -81,7 +88,7 @@ class Db {
return res.rowLength !== 0; return res.rowLength !== 0;
} catch (e) { } catch (e) {
console.log(`Error checking channel existance: ${e as Error}`); console.error(`Error checking channel existance: ${e as Error}`);
return false; return false;
} }
} }
@ -92,7 +99,7 @@ class Db {
const res = await this.client.execute(`SELECT table_name FROM system_schema.tables WHERE keyspace_name = 'channels'`); const res = await this.client.execute(`SELECT table_name FROM system_schema.tables WHERE keyspace_name = 'channels'`);
return res.rows; return res.rows;
} catch (e) { } catch (e) {
console.log(`Error fetching channels: ${e as Error}`); console.error(`Error fetching channels: ${e as Error}`);
return; return;
} }
} }
@ -109,7 +116,7 @@ class Db {
error: null, error: null,
}; };
} catch (e) { } catch (e) {
console.log(`Error fetching messages: ${(e as Error).message}`); console.error(`Error fetching messages: ${(e as Error).message}`);
return { return {
messages: null, messages: null,
error: e as Error, error: e as Error,