Skip to content

Commit

Permalink
Removed mongoose but it only does *some* session negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
duogenesis committed Feb 13, 2025
1 parent fde4520 commit 579d0a9
Show file tree
Hide file tree
Showing 24 changed files with 445 additions and 504 deletions.
7 changes: 6 additions & 1 deletion api.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# syntax=docker.io/docker/dockerfile:1.7-labs
FROM python:3.11

ENV DUO_USE_VENV=false
ENV PYTHONUNBUFFERED=true

WORKDIR /app

COPY . /app
COPY \
--exclude=antiabuse/antiporn \
--exclude=test \
--exclude=vm \
. /app

RUN : \
&& apt update \
Expand Down
8 changes: 4 additions & 4 deletions api.main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ fi

if [ "$DUO_ENV" = "prod" ]
then
python3 database/init.py
python3 database/initapi.py
exec gunicorn \
--workers "${DUO_WORKERS:-4}" \
--bind "0.0.0.0:$PORT" \
--timeout 0 \
service.application:app
service.api:app
elif [ "$DUO_ENV" = "dev" ]
then
python3 database/init.py
python3 database/initapi.py
exec flask \
--app service.application:app \
--app service.api:app \
--debug run \
--host 0.0.0.0 \
--port "$PORT"
Expand Down
5 changes: 5 additions & 0 deletions api.requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ confusable-homoglyphs
erlastic
flask-cors
gunicorn
lxml
nltk
numpy
openai
psycopg[binary]
pydantic[email]
redis
regex
uvicorn
websockets
41 changes: 8 additions & 33 deletions chat.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
FROM mongooseim/mongooseim:6.1.0-5-gabdcd0b48
# syntax=docker.io/docker/dockerfile:1.7-labs
FROM python:3.11

ENV JOIN_CLUSTER=false
ENV DUO_USE_VENV=false
ENV PYTHONUNBUFFERED=true

WORKDIR /app

# MongooseIM config
COPY service/chat/container/init-db.sh /init-db.sh
COPY service/chat/container/init.sql /init.sql
COPY service/chat/container/mongooseim.toml /usr/lib/mongooseim/etc/mongooseim.toml

# Proxy
COPY antiabuse/__init__.py /app/antiabuse/__init__.py
COPY antiabuse/antirude /app/antiabuse/antirude
COPY antiabuse/antispam /app/antiabuse/antispam
COPY antiabuse/normalize /app/antiabuse/normalize
COPY database /app/database
COPY duohash /app/duohash
COPY batcher /app/batcher
COPY notify /app/notify
COPY async_lru_cache /app/async_lru_cache
COPY sql /app/sql
COPY service/chat /app/service/chat
COPY chat.main.sh /app
COPY chat.auth.main.sh /app
COPY chat.requirements.txt /app
COPY \
--exclude=antiabuse/antiporn \
--exclude=test \
--exclude=vm \
. /app

RUN : \
&& apt update \
&& apt install -y lsb-release wget \
&& sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' \
&& wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& apt update \
&& apt install -y postgresql-client python3-pip libpq5 \
&& rm -rf /var/lib/apt/lists/* \
&& pip install --no-cache-dir -r /app/chat.requirements.txt

CMD : \
&& /init-db.sh \
&& ( /app/chat.main.sh & ) \
&& /start.sh
CMD /app/chat.main.sh
12 changes: 0 additions & 12 deletions chat.auth.main.sh

This file was deleted.

61 changes: 26 additions & 35 deletions chat.main.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# TODO: Remove useless dependencies from api and chat containers
# TODO: Check that running in prod works

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "$script_dir"

Expand All @@ -21,45 +24,33 @@ then
python3 -m pip install -r chat.requirements.txt
fi

if [ -z "$DUO_CHAT_PORTS" ]
if [ -z "$PORT" ]
then
DUO_CHAT_PORTS='5443'
PORT=5443
fi

# Array to hold the PIDs of the child processes
child_pids=()

# Function to kill all child processes
cleanup() {
echo "Cleaning up child processes..."
for pid in "${child_pids[@]}"; do
kill -TERM "$pid" 2>/dev/null
done
}
if [ "$DUO_ENV" = "prod" ]
then
python3 database/initchat.py

# Set trap to call cleanup when the script exits
trap cleanup EXIT
touch /tmp/chat-db-initialized

# Helper function to handle port ranges
expand_ports() {
if [[ $1 =~ ^([0-9]+)-([0-9]+)$ ]]; then
local lower=${BASH_REMATCH[1]}
local upper=${BASH_REMATCH[2]}
seq $lower $upper
else
echo $1
fi
}
exec uvicorn \
--host 0.0.0.0 \
--port "$PORT" \
--workers "${DUO_WORKERS:-4}" \
service.chat:app
elif [ "$DUO_ENV" = "dev" ]
then
python3 database/initchat.py

# Iterate over all command-line arguments
for port_entry in $DUO_CHAT_PORTS
do
for duo_chat_port in $(expand_ports $port_entry)
do
python3 service/chat/__init__.py "$duo_chat_port" &
child_pids+=($!)
done
done
touch /tmp/chat-db-initialized

# Wait for all background jobs to complete
wait
exec uvicorn \
--host 0.0.0.0 \
--port "$PORT" \
--reload \
service.chat:app
else
echo "The environment variable DUO_ENV must be set and have the value 'dev' or 'prod'"
fi
3 changes: 3 additions & 0 deletions chat.requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
confusable-homoglyphs
erlastic
fastapi
lxml
nltk
psycopg[binary]
redis
regex
uvicorn
websockets
6 changes: 5 additions & 1 deletion cron.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# syntax=docker.io/docker/dockerfile:1.7-labs
FROM python:3.11

ENV DUO_USE_VENV=false
ENV PYTHONUNBUFFERED=true

WORKDIR /app

COPY . /app
COPY \
--exclude=test \
--exclude=vm \
. /app

RUN pip install --no-cache-dir -r /app/cron.requirements.txt

Expand Down
11 changes: 5 additions & 6 deletions database/init.py → database/initapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,29 @@ def create_db(name):
)
time.sleep(1)

create_db('duo_chat')
create_db('duo_api')

def init_db():
# Now DB_NAME exists, we do do the rest of the init.
from service import (
application,
api,
location,
person,
question,
)

init_funcs = [
application.init_db,
api.init_db,
location.init_db,
question.init_db,
person.init_db,
question.init_db,
]

print('Initializing DB...')
print('Initializing api DB...')
for i, init_func in enumerate(init_funcs, start=1):
print(f' * {i} of {len(init_funcs)}')
init_func()
print('Finished initializing DB')
print('Finished initializing api DB')

create_dbs()
init_db()
59 changes: 59 additions & 0 deletions database/initchat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
def create_dbs():
# All this stuff just to run `CREATE DATABASE IF NOT EXISTS DB_NAME`
import os
import psycopg
import time

DB_HOST = os.environ['DUO_DB_HOST']
DB_PORT = os.environ['DUO_DB_PORT']
DB_USER = os.environ['DUO_DB_USER']
DB_PASS = os.environ['DUO_DB_PASS']

_conninfo = psycopg.conninfo.make_conninfo(
host=DB_HOST,
port=DB_PORT,
user=DB_USER,
password=DB_PASS,
)

def create_db(name):
for _ in range(10):
try:
with psycopg.connect(_conninfo, autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(f"CREATE DATABASE {name}")
print(f'Created database: {name}')
break
except (
psycopg.errors.DuplicateDatabase,
psycopg.errors.UniqueViolation,
):
print(f'Database already exists: {name}')
break
except psycopg.errors.OperationalError:
print(
'Creating database(s) failed; waiting and trying again:',
e
)
time.sleep(1)

create_db('duo_chat')

def init_db():
# Now DB_NAME exists, we do do the rest of the init.
from service import (
chat,
)

init_funcs = [
chat.init_db,
]

print('Initializing chat DB...')
for i, init_func in enumerate(init_funcs, start=1):
print(f' * {i} of {len(init_funcs)}')
init_func()
print('Finished initializing chat DB')

create_dbs()
init_db()
20 changes: 5 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,18 @@ services:
- "5442:5442"
- "5443:5443"
volumes:
- ./database/__init__.py:/app/database/__init__.py:ro
- ./database/asyncdatabase/__init__.py:/app/database/asyncdatabase/__init__.py:ro
- ./service/chat:/app/service/chat:ro
- ./service/chat/container/mongooseim.toml:/usr/lib/mongooseim/etc/mongooseim.toml:ro
- ./service/chat/container/init.sql:/init.sql:ro
- ./service/chat/container/init-db.sh:/init-db.sh:ro
- ./chat.main.sh:/app/chat.main.sh:ro
- ./chat.auth.main.sh:/app/chat.auth.main.sh:ro
- ./notify:/app/notify:ro
- ./batcher:/app/batcher:ro
- ./antiabuse:/app/antiabuse:ro
- ./async_lru_cache:/app/async_lru_cache:ro
- ./sql:/app/sql:ro
- .:/app:ro
environment:
DUO_API_HOST: http://api:5000
DUO_ENV: dev

DUO_DB_HOST: postgres
DUO_DB_PORT: 5432
DUO_DB_USER: postgres
DUO_DB_PASS: password

DUO_CHAT_PORTS: '5443'
healthcheck:
test: ["CMD", "bash", "-c", "[[ -e /db-initialized ]]"]
test: ["CMD", "bash", "-c", "[[ -e /tmp/chat-db-initialized ]]"]
interval: 30s
timeout: 10s
retries: 3
Expand Down
File renamed without changes.
Loading

0 comments on commit 579d0a9

Please sign in to comment.