Spaces:
Runtime error
Runtime error
fix(db): retry postgres startup connections on render
Browse files- package.json +1 -1
- server.js +30 -2
package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"name": "discord-like-app",
|
| 3 |
-
"version": "3.2.
|
| 4 |
"main": "server.js",
|
| 5 |
"scripts": {
|
| 6 |
"dev": "nodemon server.js",
|
|
|
|
| 1 |
{
|
| 2 |
"name": "discord-like-app",
|
| 3 |
+
"version": "3.2.12",
|
| 4 |
"main": "server.js",
|
| 5 |
"scripts": {
|
| 6 |
"dev": "nodemon server.js",
|
server.js
CHANGED
|
@@ -126,6 +126,34 @@ const sequelize = new Sequelize(resolvedConnectionString, {
|
|
| 126 |
dialectOptions: isLocalConnection ? {} : { ssl: { require: true, rejectUnauthorized: false } }
|
| 127 |
});
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
const User = sequelize.define('User', {
|
|
@@ -1851,7 +1879,7 @@ async function ensureShopItemsAvailable() {
|
|
| 1851 |
const PORT = process.env.PORT || 3000;
|
| 1852 |
const HOST = process.env.HOST || '0.0.0.0';
|
| 1853 |
|
| 1854 |
-
|
| 1855 |
await migrateMediaTypeEnum();
|
| 1856 |
await seedShopItems();
|
| 1857 |
server.listen(PORT, HOST, () => {
|
|
@@ -1877,7 +1905,7 @@ sequelize.sync({ alter: true }).then(async () => {
|
|
| 1877 |
`);
|
| 1878 |
});
|
| 1879 |
}).catch(err => {
|
| 1880 |
-
console.error('Database
|
| 1881 |
process.exit(1);
|
| 1882 |
});
|
| 1883 |
|
|
|
|
| 126 |
dialectOptions: isLocalConnection ? {} : { ssl: { require: true, rejectUnauthorized: false } }
|
| 127 |
});
|
| 128 |
|
| 129 |
+
function sleep(ms) {
|
| 130 |
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
async function syncDatabaseWithRetry(maxAttempts = 8) {
|
| 134 |
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
| 135 |
+
try {
|
| 136 |
+
await sequelize.authenticate();
|
| 137 |
+
await sequelize.sync({ alter: true });
|
| 138 |
+
return;
|
| 139 |
+
} catch (err) {
|
| 140 |
+
const message = err && err.message ? err.message : String(err);
|
| 141 |
+
const retryable = /ECONNRESET|ECONNREFUSED|ETIMEDOUT|SequelizeConnection/i.test(message);
|
| 142 |
+
const shouldRetry = retryable && attempt < maxAttempts;
|
| 143 |
+
|
| 144 |
+
console.error(`Database connect/sync attempt ${attempt}/${maxAttempts} failed:`, err);
|
| 145 |
+
|
| 146 |
+
if (!shouldRetry) {
|
| 147 |
+
throw err;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
const delayMs = Math.min(30000, 2000 * attempt);
|
| 151 |
+
console.log(`Retrying database connection in ${delayMs}ms...`);
|
| 152 |
+
await sleep(delayMs);
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
|
| 158 |
|
| 159 |
const User = sequelize.define('User', {
|
|
|
|
| 1879 |
const PORT = process.env.PORT || 3000;
|
| 1880 |
const HOST = process.env.HOST || '0.0.0.0';
|
| 1881 |
|
| 1882 |
+
syncDatabaseWithRetry().then(async () => {
|
| 1883 |
await migrateMediaTypeEnum();
|
| 1884 |
await seedShopItems();
|
| 1885 |
server.listen(PORT, HOST, () => {
|
|
|
|
| 1905 |
`);
|
| 1906 |
});
|
| 1907 |
}).catch(err => {
|
| 1908 |
+
console.error('Database startup error:', err);
|
| 1909 |
process.exit(1);
|
| 1910 |
});
|
| 1911 |
|