Skip to content

Commit 5bc7fbf

Browse files
committed
PEER-220: Add docker config
Signed-off-by: SeeuSim <[email protected]>
1 parent aadabcc commit 5bc7fbf

File tree

12 files changed

+102
-54
lines changed

12 files changed

+102
-54
lines changed

backend/matching/.env.compose

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
# PEERPREP_UI_HOST="http://frontend:3000"
33

44
EXPRESS_PORT=9004
5+
6+
# MATCHING_DB_HOSTNAME="match-db"
7+
MATCHING_DB_PORT=6379
8+
9+
MATCHING_DB_USERNAME="peerprep-match-express"
10+
MATCHING_DB_PASSWORD="G7jBgyz9wGAFQ5La"
11+
REDIS_ARGS="--requirepass G7jBgyz9wGAFQ5La --user ${MATCHING_DB_USERNAME} on >G7jBgyz9wGAFQ5La ~* allcommands --user default off nopass nocommands"
12+

backend/matching/.env.docker

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
PEERPREP_UI_HOST="http://host.docker.internal:5173"
1+
PEERPREP_UI_HOST=http://host.docker.internal:5173
22

33
EXPRESS_PORT=9004
4+
5+
MATCHING_DB_HOSTNAME=host.docker.internal
6+
MATCHING_DB_PORT=6379
7+
8+
MATCHING_DB_USERNAME=peerprep-match-express
9+
MATCHING_DB_PASSWORD=G7jBgyz9wGAFQ5La

backend/matching/entrypoint.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/sh
22

3-
# To insert MQ health checks
3+
npm run db:seed:prod
4+
5+
rm -rf src tsconfig.json
6+
7+
npm uninstall tsx
48

59
npm run start

backend/matching/express.Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ COPY --from=build --chown=node:node /data/match-express/dist ./dist
1212

1313
RUN npm ci --omit=dev
1414

15+
COPY src/lib/db ./src/lib/db
16+
COPY src/config.ts ./src
17+
COPY tsconfig.json .
1518
COPY entrypoint.sh .
1619

1720
ARG port

backend/matching/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"build:local": "env-cmd -f .env.local tsc && tsc-alias",
1010
"start:local": "env-cmd -f .env.local node dist/index.js",
1111
"db:seed": "env-cmd -f .env.local tsx src/lib/db/seed.ts",
12+
"db:seed:prod": "tsx src/lib/db/seed.ts",
1213
"fmt": "prettier --config .prettierrc src --write",
1314
"test": "echo \"Error: no test specified\" && exit 1"
1415
},
@@ -26,7 +27,8 @@
2627
"pino": "^9.4.0",
2728
"pino-http": "^10.3.0",
2829
"redis": "^4.7.0",
29-
"socket.io": "^4.8.0"
30+
"socket.io": "^4.8.0",
31+
"tsx": "^4.19.1"
3032
},
3133
"devDependencies": {
3234
"@types/async": "^3.2.24",
@@ -36,7 +38,6 @@
3638
"nodemon": "^3.1.4",
3739
"pino-pretty": "^11.2.2",
3840
"ts-node": "^10.9.2",
39-
"tsc-alias": "^1.8.10",
40-
"tsx": "^4.19.1"
41+
"tsc-alias": "^1.8.10"
4142
}
4243
}

backend/matching/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { logger } from '@/lib/utils';
55
import server, { io } from '@/server';
66
import { initWorker } from '@/workers';
77

8-
const workers: ChildProcess[] = [];
8+
const workers: { controller: AbortController; worker: ChildProcess }[] = [];
99

1010
const port = Number.parseInt(EXPRESS_PORT || '8001');
1111

@@ -18,7 +18,8 @@ server.listen(port, () => {
1818
});
1919

2020
const shutdown = () => {
21-
workers.forEach((worker) => {
21+
workers.forEach(({ controller, worker }) => {
22+
controller.abort();
2223
worker.kill();
2324
});
2425
io.close(() => {

backend/matching/src/workers/cleaner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { client } from '@/lib/db';
22
import { STREAM_CLEANER, STREAM_GROUP, STREAM_NAME } from '@/lib/db/constants';
33
import { decodePoolTicket, getPoolKey } from '@/lib/utils';
44
import { MATCH_SVC_EVENT } from '@/ws';
5-
import { sendNotif } from './common';
5+
import { connectClient, sendNotif } from './common';
66

77
const logger = {
88
info: (message: unknown) => process.send && process.send(message),
@@ -25,11 +25,12 @@ const shutdown = () => {
2525
.then(process.exit(0));
2626
};
2727

28+
process.on('exit', shutdown);
2829
process.on('SIGINT', shutdown);
2930
process.on('SIGTERM', shutdown);
3031

3132
async function clean() {
32-
const redisClient = client.isReady || client.isOpen ? client : await client.connect();
33+
const redisClient = await connectClient(client);
3334
const response = await redisClient.xAutoClaim(
3435
STREAM_NAME,
3536
STREAM_GROUP,

backend/matching/src/workers/common.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { client } from '@/lib/db';
2+
import { logger } from '@/lib/utils';
13
import type { IChildProcessMessage, IMatchEvent } from '@/ws';
24

35
export const sendNotif = (roomIds: Array<string>, event: IMatchEvent, message?: unknown) => {
@@ -10,3 +12,20 @@ export const sendNotif = (roomIds: Array<string>, event: IMatchEvent, message?:
1012
process.send(payload);
1113
}
1214
};
15+
16+
export const connectClient = async (importedClient: typeof client) => {
17+
let redisClient: typeof client;
18+
try {
19+
redisClient =
20+
importedClient.isOpen || importedClient.isReady
21+
? importedClient
22+
: await importedClient.connect();
23+
} catch (error) {
24+
const { name, message, cause, stack } = error as Error;
25+
logger.error(
26+
`An error occurred in connecting: ${JSON.stringify({ name, message, cause, stack })}`
27+
);
28+
process.exit(1);
29+
}
30+
return redisClient;
31+
};

backend/matching/src/workers/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { logger } from '@/lib/utils';
77
import { MATCH_SVC_EVENT, type IChildProcessMessage } from '@/ws';
88

99
export const initWorker = (name: string, io: Server) => {
10+
const controller = new AbortController();
1011
const lCaseName = name.toLowerCase();
11-
const worker = fork(path.join(__dirname, `${lCaseName}.js`));
12+
const worker = fork(path.join(__dirname, `${lCaseName}.js`), { signal: controller.signal });
1213
const upperCaseName = name.replace(/^[A-Za-z]/, (c) => c.toUpperCase());
1314
worker.on('message', (message) => {
1415
logger.info(`Received message from '${upperCaseName}': ${JSON.stringify(message)}`);
@@ -23,5 +24,5 @@ export const initWorker = (name: string, io: Server) => {
2324
worker.on('exit', (code) => {
2425
logger.error(`${upperCaseName} exited with code ${code}.`);
2526
});
26-
return worker;
27+
return { worker, controller };
2728
};

backend/matching/src/workers/matcher.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { POOL_INDEX, STREAM_GROUP, STREAM_NAME, STREAM_WORKER } from '@/lib/db/c
33
import { decodePoolTicket, getPoolKey, getStreamId } from '@/lib/utils';
44
import { getMatchItems } from '@/services';
55
import { MATCH_SVC_EVENT } from '@/ws';
6-
import { sendNotif } from './common';
6+
import { connectClient, sendNotif } from './common';
77

88
const logger = {
99
info: (message: unknown) => process.send && process.send(message),
@@ -25,6 +25,7 @@ const shutdown = () => {
2525
.then(process.exit(0));
2626
};
2727

28+
process.on('exit', shutdown);
2829
process.on('SIGINT', shutdown);
2930
process.on('SIGTERM', shutdown);
3031

@@ -74,7 +75,7 @@ async function processMatch(
7475
}
7576

7677
async function match() {
77-
const redisClient = client.isReady || client.isOpen ? client : await client.connect();
78+
const redisClient = await connectClient(client);
7879
const stream = await redisClient.xReadGroup(
7980
STREAM_GROUP,
8081
STREAM_WORKER,

0 commit comments

Comments
 (0)