Skip to content

Commit

Permalink
Merge pull request #146 from game-node-app/dev
Browse files Browse the repository at this point in the history
Adds a bullmq exclusive redis server
  • Loading branch information
Lamarcke authored Jan 12, 2025
2 parents 5e70d73 + 9ffe720 commit 9718f67
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DOMAIN_API=localhost:5000
DOMAIN_WEBSITE=localhost:3000
SUPERTOKENS_CORE_URI=http://localhost:3567
REDIS_URL=redis://localhost:6379
BULLMQ_REDIS_URL=redis://localhost:6379
SERVER_PORT=5000
RABBITMQ_URI=amqp://rabbitmq:rabbitmq@localhost:5672
STEAM_API_KEY=
Expand Down
28 changes: 25 additions & 3 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ services:
timeout: 20s
retries: 10


# Redis server for caching
redis:
image: redis:latest
hostname: server-redis
command: ["redis-server", "--appendonly", "no", "--maxmemory", "4096mb", "--maxmemory-policy", "allkeys-lru"]
command: ["redis-server", "--appendonly", "yes", "--maxmemory", "2048mb", "--maxmemory-policy", "allkeys-lru"]
environment:
ALLOW_EMPTY_PASSWORD: 'yes'

Expand All @@ -114,6 +114,24 @@ services:
networks:
- game_node_app

# Redis server exclusive to BullMQ
redis_bullmq:
image: redis:latest
hostname: server-redis-bullmq
command: ["redis-server", "--appendonly", "yes", "--maxmemory", "6144mb", "--maxmemory-policy", "noeviction"]
environment:
ALLOW_EMPTY_PASSWORD: 'yes'
restart: always
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
volumes:
- redis_bullmq:/var/lib/redis

networks:
- game_node_app



server:
image: lamarcke/game-node-server:latest
hostname: server
Expand All @@ -122,6 +140,7 @@ services:
# the docker resources are accessible.
environment:
REDIS_URL: redis://server-redis:6379
BULLMQ_REDIS_URL: redis://server-redis-bullmq:6379
SUPERTOKENS_CORE_URI: http://@supertokens:3567
SERVER_PORT: 5000
DOMAIN_API: https://server.gamenode.app
Expand Down Expand Up @@ -174,7 +193,6 @@ services:
image: lamarcke/game-node-server:latest
hostname: server-migration
environment:
REDIS_URL: redis://server-redis:6379
DB_HOST: server-db
DB_DATABASE: gamenode
DB_PASS: ${DB_PASS}
Expand All @@ -183,6 +201,9 @@ services:

restart: always

# The server process is not meant to actually be started here.
# This could be done better, but we need to manually check every migration
# before running.
command: "sleep infinity"

deploy:
Expand All @@ -201,6 +222,7 @@ networks:

volumes:
redis:
redis_bullmq:
db:
gamenode:
supertokens-db:
Expand Down
14 changes: 9 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ import { PlaytimeWatchModule } from "./playtime/watch/playtime-watch.module";
/**
* Should only be called after 'ConfigModule' is loaded (e.g. in useFactory)
*/
function getRedisConfig() {
function getRedisConfig(target: "cache" | "bullmq" = "cache") {
/**
* While the "redis" property below accepts a string, and it works fine on local,
* it fails on Docker, so use host and port instead.
*/
const redisUrl = process.env.REDIS_URL;
let redisUrl = process.env.REDIS_URL;
if (target === "bullmq") {
redisUrl = process.env.BULLMQ_REDIS_URL;
}
const redisHost = new URL(redisUrl!).hostname;
const redisPort = new URL(redisUrl!).port;

Expand Down Expand Up @@ -101,18 +104,19 @@ function getRedisConfig() {
};
},
}),

CacheModule.registerAsync({
isGlobal: true,
inject: [ConfigService],
useFactory: async () => ({
store: await redisStore({
url: process.env.REDIS_URL,
url: getRedisConfig().url,
}),
}),
}),
BullModule.forRootAsync({
inject: [ConfigService],
useFactory: async () => {
const { port, host } = getRedisConfig();
const { port, host } = getRedisConfig("bullmq");

return {
connection: {
Expand Down

0 comments on commit 9718f67

Please sign in to comment.