Skip to content

Commit

Permalink
improve docker config
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrowM committed Jun 22, 2024
1 parent b8489d9 commit 2385b57
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 21 deletions.
5 changes: 3 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ docker-compose.yml
.gitignore
.idea

data/main.sqlite
data/backups
data/backups
logs
docs
25 changes: 16 additions & 9 deletions README-dev.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- TOC -->
* [Running Locally](#running-locally)
* [Option 1: Install and run with Docker (recommended)](#option-1-install-and-run-with-docker-recommended)
* [Other useful docker commands](#other-useful-docker-commands)
* [Option 2: Manually install and run](#option-2-manually-install-and-run)
* [Data storage and access](#data-storage-and-access)
* [How to create and edit commands](#how-to-create-and-edit-commands)
Expand All @@ -21,10 +22,10 @@ Clone the repository:
git clone https://github.com/ArrowM/Queue-Bot
```

Create or reuse a Discord bot application and invite it to your server.
Create a Discord bot application and invite it to your server.
See [Discord.js guide](https://discordjs.guide/preparations/setting-up-a-bot-application.html).

Update the `.env` file with your bot's TOKEN and CLIENT_ID.
Update the `.env` file with your bot's `TOKEN` and `CLIENT_ID`.

### Option 1: Install and run with Docker (recommended)

Expand All @@ -33,23 +34,27 @@ Update the `.env` file with your bot's TOKEN and CLIENT_ID.
You may need to grant yourself docker perms (replacing `<username>` with your actual username, `pi` in my case:

```bash
chmod +x launch-docker.sh
sudo usermod -aG docker <username>
sudo reboot
```

Setup (**run each time you update the project**):
Run `./launch-docker.sh` or `launch-docker.bat` to:
- dump logs to `./logs` and close the previous container (if applicable)
- build the image & container, then start the bot in a detached state
- attach to the container (which can safely be exited with the `Ctrl+p Ctrl+q` key sequence. Using `CTRL-c` while attached will stop the container)

```bash
docker compose build
````
*Note you may need to respond to a migration prompt if you have updated your project*.

#### Other useful docker commands

Start the bot in a detached container:
Attach to the bot container (`Ctrl+p Ctrl+q` to detach):

```bash
docker compose up -d
docker attach queue-bot
```

View the logs:
View live logs:

```bash
docker logs -f queue-bot
Expand All @@ -63,6 +68,8 @@ docker compose down

### Option 2: Manually install and run

This method is not recommended, because it requires installing node and lacks the logging, auto-restart, and rebuild speed of Docker.

[Install Node.js](https://nodejs.org/en/download/package-manager).

Run the setup script (**run each time you update the project**):
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ services:
- ./.env:/app/.env # Bind mount for .env file
container_name: queue-bot
restart: always
command: ["npm", "run", "docker:start"]
logging:
options:
max-size: "10m"
max-file: "5"
stdin_open: true # Allow stdin to be open
tty: true # Allocate a pseudo-TTY
36 changes: 36 additions & 0 deletions launch-docker.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@echo off
rem Set the container name
set CONTAINER_NAME=queue-bot

rem Check if the container is running
docker ps -q -f name=%CONTAINER_NAME% > nul 2>&1
if %errorlevel% equ 0 (
rem Create a dated log file name
set LOG_FILE=logs\%CONTAINER_NAME%_%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.log

rem Save the logs to the file
mkdir logs 2>nul
docker logs %CONTAINER_NAME% > %LOG_FILE%

rem Check if the logs were saved successfully
if %errorlevel% equ 0 (
echo Logs saved to %LOG_FILE%
) else (
echo Failed to save logs
exit /b 1
)
) else (
echo Container %CONTAINER_NAME% is not running. Skipping log saving.
)

git pull

docker-compose down

npx drizzle-kit push

docker-compose up -d --build

docker image prune -f

docker logs -f queue-bot
32 changes: 32 additions & 0 deletions launch-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Set the container name
CONTAINER_NAME="queue-bot"

# Check if the container is running
if [ "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then
# Create a dated log file name
LOG_FILE="logs/${CONTAINER_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').log"

# Save the logs to the file
mkdir -p logs
docker logs -t $CONTAINER_NAME >& $LOG_FILE

# Check if the logs were saved successfully
if [ $? -eq 0 ]; then
echo "Logs saved to $LOG_FILE"
else
echo "Failed to save logs"
exit 1
fi
else
echo "Container ${CONTAINER_NAME} is not running. Skipping log saving."
fi

git pull

docker compose down

docker compose up -d --build

docker image prune -f

docker attach queue-bot
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"setup": "npm ci && drizzle-kit push",
"start": "node --loader @swc-node/register/esm --no-warnings --enable-source-maps --env-file .env src/index.ts",
"docker:start": "drizzle-kit push && npm run start",
"docker:start": "drizzle-kit push && npm start",
"lint": "eslint src --fix"
},
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions src/db/legacy-migration/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function loadExportData() {
await Promise.all(files.map(file => new Promise<void>((resolve, reject) => {
const data: any[] = [];
fs.createReadStream(`${LEGACY_EXPORT_DIR}/${file}`)
// @ts-ignore
.pipe(csv())
.on("data", (row) => {
data.push(row);
Expand Down
16 changes: 8 additions & 8 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
export const GUILD_TABLE = sqliteTable("guild", ({
guildId: text("guild_id").$type<Snowflake>().primaryKey(),

logChannelId: text("log_channel_id").$type<Snowflake | null>(),
logScope: text("log_scope").$type<Scope | null>(),
logChannelId: text("log_channel_id").$type<Snowflake>(),
logScope: text("log_scope").$type<Scope>(),

joinTime: integer("joinTime").$type<bigint>().notNull().$defaultFn(() => BigInt(Date.now())),
lastUpdateTime: integer("last_updated_time").$type<bigint>().notNull().$defaultFn(() => BigInt(Date.now())),
Expand Down Expand Up @@ -72,11 +72,11 @@ export const QUEUE_TABLE = sqliteTable("queue", ({
pullMessage: text("pull_message"),
rejoinCooldownPeriod: integer("rejoin_cooldown_period").$type<bigint>().notNull().default(0 as any),
rejoinGracePeriod: integer("rejoin_grace_period").$type<bigint>().notNull().default(0 as any),
roleInQueueId: text("role_in_queue_id").$type<Snowflake | null>(),
roleOnPullId: text("role_on_pull_id").$type<Snowflake | null>(),
roleInQueueId: text("role_in_queue_id").$type<Snowflake>(),
roleOnPullId: text("role_on_pull_id").$type<Snowflake>(),
size: integer("size").$type<bigint>(),
timestampType: text("time_display_type").$type<TimestampType>().default(TimestampType.Off),
voiceDestinationChannelId: text("voice_destination_channel_id").$type<Snowflake | null>(),
voiceDestinationChannelId: text("voice_destination_channel_id").$type<Snowflake>(),
voiceOnlyToggle: integer("voice_only_toggle", { mode: "boolean" }).notNull().default(false),
}),
(table) => ({
Expand Down Expand Up @@ -131,7 +131,7 @@ export const DISPLAY_TABLE = sqliteTable("display", ({
guildId: text("guild_id").notNull().references(() => GUILD_TABLE.guildId, { onDelete: "cascade" }),
queueId: integer("queue_id").$type<bigint>().notNull().references(() => QUEUE_TABLE.id, { onDelete: "cascade" }),
displayChannelId: text("display_channel_id").notNull(),
lastMessageId: text("last_message_id").$type<Snowflake | null>(),
lastMessageId: text("last_message_id").$type<Snowflake>(),
}),
(table) => ({
unq: unique().on(table.queueId, table.displayChannelId),
Expand Down Expand Up @@ -185,10 +185,10 @@ export const SCHEDULE_TABLE = sqliteTable("schedule", ({

guildId: text("guild_id").notNull().references(() => GUILD_TABLE.guildId, { onDelete: "cascade" }),
queueId: integer("queue_id").$type<bigint>().notNull().references(() => QUEUE_TABLE.id, { onDelete: "cascade" }),
command: text("command").notNull().$type<ScheduleCommand | null>(),
command: text("command").notNull().$type<ScheduleCommand>(),
cron: text("cron").notNull(),
timezone: text("timezone"),
messageChannelId: text("message_channel_id").$type<Snowflake | null>(),
messageChannelId: text("message_channel_id").$type<Snowflake>(),
reason: text("reason"),
}),
(table) => ({
Expand Down

0 comments on commit 2385b57

Please sign in to comment.