Skip to content

Commit 91ed2af

Browse files
committed
api: Hopefully fix DB connection
1 parent b22b339 commit 91ed2af

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

packages/api/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ COPY packages/api/emails /app/emails
2424
COPY packages/api/dist/ /app/
2525

2626
EXPOSE 3001
27-
CMD ["pm2", "start", "--no-daemon", "-i", "1", "index.mjs"]
27+
CMD ["pm2", "start", "--no-daemon", "-i", "max", "index.mjs"]
2828

2929
FROM api AS api-story-search
3030
RUN mv /app/scripts/import-entryurl-vectors.mjs /app/

packages/api/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ dotenv.config({
4949
path: "./.env",
5050
});
5151

52+
// Validate required environment variables
53+
const requiredEnvVars = [
54+
'DATABASE_URL_DM',
55+
'DATABASE_URL_COA',
56+
'DATABASE_URL_DM_STATS',
57+
'DATABASE_URL_EDGECREATOR',
58+
'DATABASE_URL_COVER_INFO'
59+
];
60+
61+
for (const envVar of requiredEnvVars) {
62+
if (!process.env[envVar]) {
63+
console.error(`Missing required environment variable: ${envVar}`);
64+
process.exit(1);
65+
}
66+
}
67+
5268
if (process.env.SENTRY_DSN) {
5369
Sentry.init({
5470
dsn: process.env.SENTRY_DSN,

packages/prisma-schemas/utils/singleton-clients.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ import { PrismaMariaDb } from '@prisma/adapter-mariadb';
88
import prismaExtendedDm from "../schemas/dm/extended";
99
import prismaExtendedCoa from "../schemas/coa/extended";
1010

11+
// Ensure connection string has proper pool parameters
12+
const ensureConnectionString = (url: string): string => {
13+
const urlObj = new URL(url);
14+
15+
// Add pool parameters if not present
16+
if (!urlObj.searchParams.has('acquireTimeout')) {
17+
urlObj.searchParams.set('acquireTimeout', '60000');
18+
}
19+
if (!urlObj.searchParams.has('connectionLimit')) {
20+
urlObj.searchParams.set('connectionLimit', '10');
21+
}
22+
if (!urlObj.searchParams.has('idleTimeout')) {
23+
urlObj.searchParams.set('idleTimeout', '300000');
24+
}
25+
if (!urlObj.searchParams.has('minimumIdle')) {
26+
urlObj.searchParams.set('minimumIdle', '2');
27+
}
28+
29+
return urlObj.toString();
30+
};
31+
1132
let dmClient: ReturnType<typeof prismaExtendedDm> | null = null;
1233
let dmStatsClient: PrismaClientDmStats | null = null;
1334
let coaClient: ReturnType<typeof prismaExtendedCoa> | null = null;
@@ -16,20 +37,28 @@ let coverInfoClient: PrismaClientCoverInfo | null = null;
1637

1738
export const getDmClient = () => {
1839
if (!dmClient) {
19-
console.log('Creating new DM PrismaClient instance');
20-
dmClient = prismaExtendedDm(new PrismaClientDm({
21-
adapter: new PrismaMariaDb(process.env.DATABASE_URL_DM!),
22-
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
23-
}));
40+
try {
41+
console.log('Creating new DM PrismaClient instance');
42+
const connectionString = ensureConnectionString(process.env.DATABASE_URL_DM!);
43+
console.log('DM connection string configured with pool parameters');
44+
dmClient = prismaExtendedDm(new PrismaClientDm({
45+
adapter: new PrismaMariaDb(connectionString),
46+
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
47+
}));
48+
} catch (error) {
49+
console.error('Failed to create DM PrismaClient:', error);
50+
throw error;
51+
}
2452
}
2553
return dmClient;
2654
};
2755

2856
export const getDmStatsClient = () => {
2957
if (!dmStatsClient) {
3058
console.log('Creating new DM Stats PrismaClient instance');
59+
const connectionString = ensureConnectionString(process.env.DATABASE_URL_DM_STATS!);
3160
dmStatsClient = new PrismaClientDmStats({
32-
adapter: new PrismaMariaDb(process.env.DATABASE_URL_DM_STATS!),
61+
adapter: new PrismaMariaDb(connectionString),
3362
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
3463
});
3564
}
@@ -39,8 +68,9 @@ export const getDmStatsClient = () => {
3968
export const getCoaClient = () => {
4069
if (!coaClient) {
4170
console.log('Creating new COA PrismaClient instance');
71+
const connectionString = ensureConnectionString(process.env.DATABASE_URL_COA!);
4272
coaClient = prismaExtendedCoa(new PrismaClientCoa({
43-
adapter: new PrismaMariaDb(process.env.DATABASE_URL_COA!),
73+
adapter: new PrismaMariaDb(connectionString),
4474
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
4575
}));
4676
}
@@ -50,8 +80,9 @@ export const getCoaClient = () => {
5080
export const getEdgeCreatorClient = () => {
5181
if (!edgeCreatorClient) {
5282
console.log('Creating new EdgeCreator PrismaClient instance');
83+
const connectionString = ensureConnectionString(process.env.DATABASE_URL_EDGECREATOR!);
5384
edgeCreatorClient = new PrismaClientEdgeCreator({
54-
adapter: new PrismaMariaDb(process.env.DATABASE_URL_EDGECREATOR!),
85+
adapter: new PrismaMariaDb(connectionString),
5586
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
5687
});
5788
}
@@ -61,8 +92,9 @@ export const getEdgeCreatorClient = () => {
6192
export const getCoverInfoClient = () => {
6293
if (!coverInfoClient) {
6394
console.log('Creating new CoverInfo PrismaClient instance');
95+
const connectionString = ensureConnectionString(process.env.DATABASE_URL_COVER_INFO!);
6496
coverInfoClient = new PrismaClientCoverInfo({
65-
adapter: new PrismaMariaDb(process.env.DATABASE_URL_COVER_INFO!),
97+
adapter: new PrismaMariaDb(connectionString),
6698
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
6799
});
68100
}

0 commit comments

Comments
 (0)