Spaces:
Sleeping
Sleeping
File size: 1,669 Bytes
6931883 43d9b90 6931883 ff8ff68 6931883 f29a569 ff8ff68 f29a569 ff8ff68 f29a569 ff8ff68 f29a569 ff8ff68 f29a569 6931883 43d9b90 f29a569 6931883 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | const app = require('./app');
const env = require('./config/env');
const { query } = require('./config/db');
const { startReminderPushWorker } = require('./workers/reminderPushWorker');
function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function startServer() {
let isDatabaseConnected = false;
const attempts = env.dbRequiredOnStartup ? env.dbConnectRetries + 1 : 1;
let lastDbError = null;
for (let attempt = 1; attempt <= attempts; attempt += 1) {
try {
await query('SELECT 1 AS db_ok');
isDatabaseConnected = true;
console.log('Database connection established.');
break;
} catch (error) {
lastDbError = error;
if (attempt >= attempts) {
break;
}
console.warn(
`Database not ready (attempt ${attempt}/${attempts}). Retrying in ${env.dbConnectRetryDelayMs}ms...`,
);
await wait(env.dbConnectRetryDelayMs);
}
}
if (!isDatabaseConnected) {
if (env.dbRequiredOnStartup) {
throw lastDbError;
}
console.error('Database unavailable at startup; continuing in degraded mode.');
console.error(lastDbError?.message || lastDbError || 'Unknown DB error');
}
app.listen(env.port, () => {
console.log(
`Care People backend listening on http://localhost:${env.port}`,
);
if (isDatabaseConnected) {
startReminderPushWorker();
} else {
console.log('[push-worker] Skipped startup because database is unavailable.');
}
});
}
startServer().catch((error) => {
console.error('Failed to start the backend server.');
console.error(error);
process.exit(1);
});
|