Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Nov 5, 2023
1 parent a355fec commit 5be5612
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.env
venv/
Dockerfile
k8s/
.github/
CODEOWNERS
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DB_HOST=localhost
DB_PORT=3306
DB_PASS=your_db_password
DB_USER=root
DB_NAME=akatsuki
DISCORD_AC_WEBHOOK=https://discordapp.com/api/webhooks/your_webhook
SERVICE_READINESS_TIMEOUT=60
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config.py
__pycache__/
venv/
.env
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM python:3.9

WORKDIR /srv/root

COPY . .
ENV PYTHONUNBUFFERED=1

COPY requirements.txt .
RUN pip install -r requirements.txt

CMD ["python", "main.py"]
COPY scripts /scripts

COPY . /srv/root
WORKDIR /srv/root

ENTRYPOINT ["/scripts/bootstrap.sh"]
8 changes: 0 additions & 8 deletions config.sample.py

This file was deleted.

24 changes: 15 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

import aioredis
import asyncio
import config
import uvloop
import time

import settings

db = AsyncSQLPool()
redis: "aioredis.Redis"


async def connect() -> None:
await db.connect(config.sql)
await db.connect(
{
"db": settings.DB_NAME,
"host": settings.DB_HOST,
"password": settings.DB_PASS,
"user": settings.DB_USER,
"port": settings.DB_PORT,
}
)

global redis
redis = await aioredis.create_redis_pool("redis://localhost:6379/0")
Expand All @@ -33,12 +42,9 @@ async def disconnect() -> None:

print("Disconnected from database and redis")

STR_TO_INT_MODE = {
"std": 0,
"taiko": 1,
"ctb": 2,
"mania": 3
}

STR_TO_INT_MODE = {"std": 0, "taiko": 1, "ctb": 2, "mania": 3}


async def recalc_ranks() -> None:
print("Recalculating all user ranks")
Expand Down Expand Up @@ -257,7 +263,7 @@ async def freeze_expired_freeze_timers() -> None:
)

# post to webhook
webhook = Webhook(config.ac_webhook)
webhook = Webhook(settings.DISCORD_AC_WEBHOOK)
embed = Embed(color=0x542CB8)

embed.add_field(name="** **", value=f"{user['username']} {FREEZE_MESSAGE}")
Expand Down
30 changes: 30 additions & 0 deletions scripts/await-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -eo pipefail

await_service()
{
local start_ts=$(date +%s)
while [ $(date +%s) -lt $((start_ts + $3)) ];
do
(echo -n > /dev/tcp/$1/$2) > /dev/null
if [[ $? -eq 0 ]]; then
break
fi
sleep 1
done
local end_ts=$(date +%s)

if [ $(date +%s) -ge $((start_ts + $3)) ]; then
echo "Timeout occurred while waiting for $1:$2 to become available"
exit 1
fi

echo "$1:$2 is available after $((end_ts - start_ts)) seconds"
}

if [[ $# -ne 3 ]]; then
echo "Usage: $0 <host> <port> <timeout>"
exit 1
fi

await_service $1 $2 $3
21 changes: 21 additions & 0 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -eo pipefail

cd /srv/root

if [ -z "$APP_ENV" ]; then
echo "Please set APP_ENV"
exit 1
fi

if [[ $PULL_SECRETS_FROM_VAULT -eq 1 ]]; then
pip install -i $PYPI_INDEX_URL akatsuki-cli
# TODO: revert to $APP_ENV
akatsuki vault get bancho-service production-k8s -o .env
source .env
fi

# await database availability
/scripts/await-service.sh $DB_HOST $DB_PORT $SERVICE_READINESS_TIMEOUT

exec python3 main.py
8 changes: 8 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

DB_NAME = os.environ["DB_NAME"]
DB_HOST = os.environ["DB_HOST"]
DB_PORT = int(os.environ["DB_PORT"])
DB_PASS = os.environ["DB_PASS"]
DB_USER = os.environ["DB_USER"]
DISCORD_AC_WEBHOOK = os.environ["DISCORD_AC_WEBHOOK"]

0 comments on commit 5be5612

Please sign in to comment.