Skip to content

Commit

Permalink
Optimize container memory usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ninele7 committed Oct 18, 2023
1 parent a325308 commit 0dba384
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Optimized Docker container to run JS code directly with node instead of yarn, npm and tsx. Reduces memory usage.

## [2.4.3] - 2023-09-10

### Fixed
Expand Down
21 changes: 17 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@ COPY package.json .
COPY yarn.lock .

RUN yarn install --prod
RUN cp -R node_modules /usr/app/prod_node_modules

RUN yarn install

FROM dependencies AS builder

COPY . .

# Run tsc build
RUN yarn prisma generate
RUN yarn build

# Only keep what's necessary to run
FROM base AS runner

WORKDIR /usr/app

COPY --from=dependencies /usr/app/node_modules node_modules
COPY --from=builder /usr/app/dist ./dist
COPY --from=dependencies /usr/app/prod_node_modules node_modules
COPY --from=builder /usr/app/node_modules/.prisma/client ./node_modules/.prisma/client

COPY . .

RUN yarn prisma generate

ARG COMMIT_HASH=unknown
ARG BUILD_DATE=unknown

Expand All @@ -34,4 +45,6 @@ ENV NODE_ENV production
ENV COMMIT_HASH $COMMIT_HASH
ENV BUILD_DATE $BUILD_DATE

CMD ["tini", "--", "yarn", "start"]
RUN echo "DATABASE_URL=$(node dist/scripts/print-database-url.js)" > .env

CMD ["tini", "--", "node", "dist/scripts/migrate-and-start.js"]
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"migrations:run": "npm run prisma:with-env migrate deploy",
"prisma:with-env": "npm run env:set-database-url prisma",
"env:set-database-url": "tsx src/scripts/run-with-database-url.ts",
"release": "release-it"
"release": "release-it",
"build": "tsc"
},
"devDependencies": {
"@release-it/keep-a-changelog": "^2.3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ChatInputCommandInteraction, EmbedBuilder, PermissionFlagsBits} from 'di
import {injectable} from 'inversify';
import {prisma} from '../utils/db.js';
import Command from './index.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

@injectable()
export default class implements Command {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player.js';
import Command from '.';
import {SlashCommandBuilder} from '@discordjs/builders';
import {STATUS} from '../services/player';
import {STATUS} from '../services/player.js';

@injectable()
export default class implements Command {
Expand Down
2 changes: 1 addition & 1 deletion src/events/voice-state-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import container from '../inversify.config.js';
import {TYPES} from '../types.js';
import PlayerManager from '../managers/player.js';
import {getSizeWithoutBots} from '../utils/channels.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

export default async (oldState: VoiceState, _: VoiceState): Promise<void> => {
const playerManager = container.get<PlayerManager>(TYPES.Managers.Player);
Expand Down
2 changes: 1 addition & 1 deletion src/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Config from './commands/config.js';
import Disconnect from './commands/disconnect.js';
import Favorites from './commands/favorites.js';
import ForwardSeek from './commands/fseek.js';
import Loop from './commands/loop';
import Loop from './commands/loop.js';
import Move from './commands/move.js';
import Next from './commands/next.js';
import NowPlaying from './commands/now-playing.js';
Expand Down
7 changes: 6 additions & 1 deletion src/scripts/migrate-and-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {DATA_DIR} from '../services/config.js';
const client = new Prisma.PrismaClient();

const migrateFromSequelizeToPrisma = async () => {
await execa('prisma', ['migrate', 'resolve', '--applied', '20220101155430_migrate_from_sequelize'], {preferLocal: true});
await execa('prisma', ['migrate', 'resolve', '--applied', '20220101155430_migrate_from_sequelize'], {
preferLocal: true,

Check failure on line 19 in src/scripts/migrate-and-start.ts

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed

Check failure on line 19 in src/scripts/migrate-and-start.ts

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
env: {
DATABASE_URL: process.env.DATABASE_URL

Check failure on line 21 in src/scripts/migrate-and-start.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma

Check failure on line 21 in src/scripts/migrate-and-start.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma
}

Check failure on line 22 in src/scripts/migrate-and-start.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma

Check failure on line 22 in src/scripts/migrate-and-start.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma
});
};

const doesUserHaveExistingDatabase = async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/print-database-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import createDatabaseUrl from '../utils/create-database-url.js';
import {DATA_DIR} from '../services/config.js';

console.log(createDatabaseUrl(DATA_DIR))

Check failure on line 4 in src/scripts/print-database-url.ts

View workflow job for this annotation

GitHub Actions / Lint

Newline required at end of file but not found

Check failure on line 4 in src/scripts/print-database-url.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon

Check failure on line 4 in src/scripts/print-database-url.ts

View workflow job for this annotation

GitHub Actions / Lint

Newline required at end of file but not found

Check failure on line 4 in src/scripts/print-database-url.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
2 changes: 1 addition & 1 deletion src/services/add-query-to-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {SongMetadata, STATUS} from './player.js';
import PlayerManager from '../managers/player.js';
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
import {getMemberVoiceChannel, getMostPopularVoiceChannel} from '../utils/channels.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

@injectable()
export default class AddQueryToQueue {
Expand Down
2 changes: 1 addition & 1 deletion src/services/get-songs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {inject, injectable} from 'inversify';
import * as spotifyURI from 'spotify-uri';
import {SongMetadata, QueuedPlaylist, MediaSource} from './player';
import {SongMetadata, QueuedPlaylist, MediaSource} from './player.js';
import {TYPES} from '../types.js';
import ffmpeg from 'fluent-ffmpeg';
import YoutubeAPI from './youtube-api.js';
Expand Down
2 changes: 1 addition & 1 deletion src/services/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@discordjs/voice';
import FileCacheProvider from './file-cache.js';
import debug from '../utils/debug.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

export enum MediaSource {
Youtube,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/get-guild-settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Setting} from '@prisma/client';
import {prisma} from './db';
import {createGuildSettings} from '../events/guild-create';
import {prisma} from './db.js';
import {createGuildSettings} from '../events/guild-create.js';

export async function getGuildSettings(guildId: string): Promise<Setting> {
const config = await prisma.setting.findUnique({where: {guildId}});
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"strict": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"sourceMap": true,
"sourceMap": false,
"resolveJsonModule": true,
"noEmit": true
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down

0 comments on commit 0dba384

Please sign in to comment.