adaptic-backend 1.0.245 → 1.0.246
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server.cjs +79 -3
package/package.json
CHANGED
package/server.cjs
CHANGED
@@ -18,6 +18,37 @@ const ws_2 = require("graphql-ws/lib/use/ws");
|
|
18
18
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
19
19
|
const auth_1 = require("./middleware/auth");
|
20
20
|
const prismaClient_1 = __importDefault(require("./prismaClient"));
|
21
|
+
const child_process_1 = require("child_process");
|
22
|
+
const dotenv_1 = __importDefault(require("dotenv"));
|
23
|
+
dotenv_1.default.config();
|
24
|
+
let dbUnreachableCount = 0;
|
25
|
+
let lastRestartAttempt = 0;
|
26
|
+
async function restartDatabase() {
|
27
|
+
return new Promise((resolve, reject) => {
|
28
|
+
console.log("Attempting to redeploy the Railway Postgres service in production...");
|
29
|
+
// Check for both types of tokens
|
30
|
+
const projectToken = process.env.RAILWAY_TOKEN;
|
31
|
+
const apiToken = process.env.RAILWAY_API_TOKEN;
|
32
|
+
if (!projectToken && !apiToken) {
|
33
|
+
return reject(new Error('Neither RAILWAY_TOKEN nor RAILWAY_API_TOKEN found in environment variables'));
|
34
|
+
}
|
35
|
+
// Simplified command based on Railway CLI documentation
|
36
|
+
const deployCommand = `RAILWAY_TOKEN=${projectToken || ''} RAILWAY_API_TOKEN=${apiToken || ''} railway redeploy --service Postgres -y`;
|
37
|
+
(0, child_process_1.exec)(deployCommand, {
|
38
|
+
env: process.env,
|
39
|
+
shell: '/bin/sh'
|
40
|
+
}, (error, stdout, stderr) => {
|
41
|
+
if (error) {
|
42
|
+
console.error('Failed to redeploy DB via Railway CLI:', error);
|
43
|
+
console.error('Command output:', stdout);
|
44
|
+
console.error('Command errors:', stderr);
|
45
|
+
return reject(error);
|
46
|
+
}
|
47
|
+
console.log('Railway deployment output:', stdout);
|
48
|
+
resolve();
|
49
|
+
});
|
50
|
+
});
|
51
|
+
}
|
21
52
|
const startServer = async () => {
|
22
53
|
const schema = await (0, type_graphql_1.buildSchema)({
|
23
54
|
resolvers: typegraphql_prisma_1.resolvers,
|
@@ -33,6 +64,48 @@ const startServer = async () => {
|
|
33
64
|
formatError: (err) => {
|
34
65
|
var _a;
|
35
66
|
console.error('GraphQL Error:', JSON.stringify(err, null, 2));
|
67
|
+
// Check if this error is due to unreachable DB
|
68
|
+
const message = err.message || '';
|
69
|
+
if (message.includes("Can't reach database server")) {
|
70
|
+
dbUnreachableCount += 1;
|
71
|
+
console.log(`DB unreachable count: ${dbUnreachableCount}`);
|
72
|
+
// If we've hit 3 (for example) attempts
|
73
|
+
if (dbUnreachableCount >= 3) {
|
74
|
+
const now = Date.now();
|
75
|
+
// Optional: Check if we've tried restarting recently
|
76
|
+
if (now - lastRestartAttempt > 5 * 60 * 1000) {
|
77
|
+
lastRestartAttempt = now;
|
78
|
+
try {
|
79
|
+
restartDatabase();
|
80
|
+
// Reset the counter after attempting a restart
|
81
|
+
dbUnreachableCount = 0;
|
82
|
+
}
|
83
|
+
catch (restartError) {
|
84
|
+
console.error('Error trying to restart DB:', restartError);
|
85
|
+
// If the restart fails, we can try again after a delay
|
86
|
+
const backoffTime = Math.min(30000, 1000 * Math.pow(2, dbUnreachableCount - 3)); // Exponential backoff with a max of 30 seconds
|
87
|
+
console.log(`Waiting for ${backoffTime / 1000} seconds before next restart attempt...`);
|
88
|
+
setTimeout(() => {
|
89
|
+
restartDatabase()
|
90
|
+
.then(() => {
|
91
|
+
dbUnreachableCount = 0; // Reset the counter after a successful restart
|
92
|
+
})
|
93
|
+
.catch((restartError) => {
|
94
|
+
console.error('Error trying to restart DB:', restartError);
|
95
|
+
// We do not reset the counter here if the restart fails,
|
96
|
+
// so it can try again next time.
|
97
|
+
});
|
98
|
+
}, backoffTime);
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
else {
|
104
|
+
// If the error is not a DB unreachable error, we might want
|
105
|
+
// to reset the counter or leave it as is. Generally, if we see
|
106
|
+
// a successful query or a different error, we might reset:
|
107
|
+
dbUnreachableCount = 0;
|
108
|
+
}
|
36
109
|
return {
|
37
110
|
message: err.message,
|
38
111
|
locations: err.locations,
|
@@ -81,7 +154,6 @@ const startServer = async () => {
|
|
81
154
|
schema,
|
82
155
|
context: async (ctx, msg, args) => {
|
83
156
|
var _a, _b;
|
84
|
-
// Handle context for WebSocket connections, including JWT verification
|
85
157
|
const token = ((_b = (_a = ctx.connectionParams) === null || _a === void 0 ? void 0 : _a.authorization) === null || _b === void 0 ? void 0 : _b.split(' ')[1]) || '';
|
86
158
|
let user = null;
|
87
159
|
if (token) {
|
@@ -108,14 +180,18 @@ startServer().catch((error) => {
|
|
108
180
|
});
|
109
181
|
process.on('unhandledRejection', (reason, promise) => {
|
110
182
|
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
111
|
-
// Application specific logging, throwing an error, or other logic here
|
112
183
|
});
|
113
184
|
process.on('uncaughtException', (error) => {
|
114
185
|
console.error('Uncaught Exception:', error);
|
115
|
-
// Application specific logging, throwing an error, or other logic here
|
116
186
|
});
|
117
187
|
process.on('SIGINT', async () => {
|
118
188
|
await prismaClient_1.default.$disconnect();
|
119
189
|
process.exit(0);
|
120
190
|
});
|
191
|
+
// // run restartDatabase() function
|
192
|
+
// restartDatabase().then(() => {
|
193
|
+
// console.log('Database restarted successfully');
|
194
|
+
// }).catch((error) => {
|
195
|
+
// console.error('Error restarting database:', error);
|
196
|
+
// });
|
121
197
|
//# sourceMappingURL=server.js.map
|