Implement messenger functionality

This commit is contained in:
2026-10-30 16:12:37 +03:00
parent 65003b6119
commit add9fbb9f4
15 changed files with 986 additions and 32 deletions
+20
View File
@@ -0,0 +1,20 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
restore() { docker compose --env-file .env start db redis >/dev/null; }
trap restore EXIT
docker compose --env-file .env stop redis
python3 tests/dependency_failure.py redis
docker compose --env-file .env start redis
for attempt in $(seq 1 30); do
if curl -fsS http://localhost:8080/health/ready >/dev/null; then break; fi
sleep 2
done
docker compose --env-file .env stop db
python3 tests/dependency_failure.py db
docker compose --env-file .env start db
for attempt in $(seq 1 30); do
if curl -fsS http://localhost:8080/health/ready >/dev/null; then break; fi
sleep 2
done
curl -fsS http://localhost:8080/health/ready >/dev/null
+30
View File
@@ -0,0 +1,30 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
backup=$(mktemp)
cp .env "$backup"
restore() {
cp "$backup" .env
rm -f "$backup"
docker compose --env-file .env up -d --force-recreate api
for attempt in $(seq 1 30); do
if curl -fsS http://localhost:8080/health/ready >/dev/null; then return; fi
sleep 2
done
return 1
}
trap restore EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
if grep -q '^SESSION_TTL_SECONDS=' .env; then
sed -i 's/^SESSION_TTL_SECONDS=.*/SESSION_TTL_SECONDS=3/' .env
else
printf '\nSESSION_TTL_SECONDS=3\n' >> .env
fi
docker compose --env-file .env up -d --force-recreate api
for attempt in $(seq 1 30); do
if curl -fsS http://localhost:8080/health/ready >/dev/null; then break; fi
sleep 2
done
curl -fsS http://localhost:8080/health/ready >/dev/null
SESSION_TTL_SECONDS=3 python3 tests/expiry.py
+23
View File
@@ -0,0 +1,23 @@
BEGIN;
CREATE TEMP TABLE lab_messages (
id UUID NOT NULL,
chat_id INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
text VARCHAR(4000) NOT NULL
);
CREATE INDEX lab_messages_history_idx ON lab_messages(chat_id, created_at, id);
INSERT INTO lab_messages(id, chat_id, created_at, text)
SELECT md5(g::text)::uuid,
CASE WHEN g <= 10000 THEN 1 ELSE 2 + (g % 100) END,
TIMESTAMPTZ '2026-01-01 00:00:00+00' + g * INTERVAL '1 millisecond',
repeat('x', 40)
FROM generate_series(1, 100000) AS g;
ANALYZE lab_messages;
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, created_at, text
FROM lab_messages
WHERE chat_id = 1
AND (created_at, id) > (TIMESTAMPTZ '2026-01-01 00:00:05+00', md5('5000')::uuid)
ORDER BY created_at, id
LIMIT 50;
ROLLBACK;
+15
View File
@@ -0,0 +1,15 @@
import os
from pathlib import Path
import psycopg
url = os.environ["DATABASE_URL"]
with psycopg.connect(url, connect_timeout=5) as conn:
conn.execute("CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT now())")
for path in sorted((Path(__file__).resolve().parents[1] / "migrations").glob("*.sql")):
version = path.name
if conn.execute("SELECT 1 FROM schema_migrations WHERE version=%s", (version,)).fetchone():
continue
with conn.transaction():
conn.execute(path.read_text(encoding="utf-8"))
conn.execute("INSERT INTO schema_migrations(version) VALUES (%s)", (version,))
print("Migrations applied")
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart db redis api