лаба на 3
This commit is contained in:
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
mkdir -p secrets
|
||||
chmod 700 secrets
|
||||
umask 077
|
||||
|
||||
create_random_secret() {
|
||||
local name="$1"
|
||||
local value="$2"
|
||||
|
||||
if [[ ! -s "secrets/$name" ]]; then
|
||||
printf '%s\n' "$value" > "secrets/$name"
|
||||
fi
|
||||
|
||||
chmod 644 "secrets/$name"
|
||||
}
|
||||
|
||||
create_random_secret postgres_password "$(python3 -c 'import secrets; print(secrets.token_hex(24))')"
|
||||
create_random_secret rabbitmq_password "$(python3 -c 'import secrets; print(secrets.token_hex(24))')"
|
||||
|
||||
if [[ ! -s secrets/database_url ]]; then
|
||||
PASSWORD="$(cat secrets/postgres_password)"
|
||||
printf 'postgresql+asyncpg://lab2:%s@postgres:5432/messenger\n' "$PASSWORD" > secrets/database_url
|
||||
fi
|
||||
|
||||
create_random_secret cache_url 'redis://redis:6379/0'
|
||||
create_random_secret minio_root_user "minio-$(python3 -c 'import secrets; print(secrets.token_hex(12))')"
|
||||
create_random_secret minio_root_password "$(python3 -c 'import secrets; print(secrets.token_hex(32))')"
|
||||
create_random_secret s3_access_key_id "$(python3 -c 'import secrets; print(secrets.token_hex(20))')"
|
||||
create_random_secret s3_secret_access_key "$(python3 -c 'import secrets; print(secrets.token_hex(32))')"
|
||||
|
||||
chmod 644 secrets/*
|
||||
|
||||
if [[ ! -f .env ]]; then
|
||||
cp .env.example .env
|
||||
fi
|
||||
|
||||
mkdir -p infra/rabbitmq
|
||||
|
||||
printf 'default_user = messenger\ndefault_pass = %s\nlisteners.tcp.default = 5672\nmanagement.tcp.port = 15672\n' \
|
||||
"$(cat secrets/rabbitmq_password)" > infra/rabbitmq/rabbitmq.conf
|
||||
|
||||
chmod 644 infra/rabbitmq/rabbitmq.conf
|
||||
|
||||
if ! grep -qE '^GRAFANA_ADMIN_PASSWORD=.+$' .env; then
|
||||
sed -i '/^GRAFANA_ADMIN_PASSWORD=/d' .env
|
||||
printf '\nGRAFANA_ADMIN_PASSWORD=%s\n' \
|
||||
"$(python3 -c 'import secrets; print(secrets.token_hex(24))')" >> .env
|
||||
fi
|
||||
|
||||
chmod 600 .env
|
||||
|
||||
docker compose config --quiet
|
||||
@@ -0,0 +1,68 @@
|
||||
import argparse
|
||||
import asyncio
|
||||
import os
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy import text
|
||||
|
||||
from src.database import SessionFactory, engine
|
||||
from src.storage import delete_object, object_exists
|
||||
|
||||
|
||||
RETENTION_SECONDS = int(os.getenv("STAGING_RETENTION_SECONDS", "86400"))
|
||||
|
||||
|
||||
async def cleanup(apply: bool):
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(seconds=RETENTION_SECONDS)
|
||||
|
||||
async with SessionFactory() as session:
|
||||
result = await session.execute(
|
||||
text("""
|
||||
SELECT a.id, a.chat_id, a.content_type, a.s3_key,
|
||||
a.created_at
|
||||
FROM attachments a
|
||||
JOIN attachment_jobs j ON j.attachment_id = a.id
|
||||
WHERE a.status = 'ready'
|
||||
AND j.status = 'ready'
|
||||
AND a.s3_key LIKE 'attachments/%'
|
||||
AND a.created_at < :cutoff
|
||||
"""),
|
||||
{"cutoff": cutoff},
|
||||
)
|
||||
|
||||
rows = result.all()
|
||||
|
||||
candidates = 0
|
||||
|
||||
for row in rows:
|
||||
extension = "png" if row.content_type == "image/png" else "jpg"
|
||||
staging_key = f"staging/{row.chat_id}/{row.id}.{extension}"
|
||||
|
||||
if not await object_exists(staging_key):
|
||||
continue
|
||||
|
||||
candidates += 1
|
||||
|
||||
if not apply:
|
||||
print(f"DRY RUN: {staging_key}")
|
||||
continue
|
||||
|
||||
await delete_object(staging_key)
|
||||
print(f"DELETED: {staging_key}")
|
||||
|
||||
print(f"Candidates: {candidates}")
|
||||
|
||||
|
||||
async def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--apply", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
await cleanup(args.apply)
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,83 @@
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import text
|
||||
|
||||
from src.database import SessionFactory, engine
|
||||
|
||||
|
||||
async def replay(job_id: UUID):
|
||||
async with SessionFactory() as session:
|
||||
async with session.begin():
|
||||
result = await session.execute(
|
||||
text("""
|
||||
SELECT j.id, j.attachment_id, j.status,
|
||||
j.generation, a.status AS attachment_status
|
||||
FROM attachment_jobs j
|
||||
JOIN attachments a ON a.id = j.attachment_id
|
||||
WHERE j.id = :job_id
|
||||
FOR UPDATE OF j, a
|
||||
"""),
|
||||
{"job_id": job_id},
|
||||
)
|
||||
|
||||
row = result.one_or_none()
|
||||
|
||||
if row is None:
|
||||
print("REJECTED: job not found")
|
||||
return
|
||||
|
||||
if row.status != "failed" or row.attachment_status != "failed":
|
||||
print("REJECTED: job and attachment must be failed")
|
||||
return
|
||||
|
||||
await session.execute(
|
||||
text("""
|
||||
UPDATE attachment_jobs
|
||||
SET status = 'queued',
|
||||
generation = generation + 1,
|
||||
attempt = 1,
|
||||
published_at = NULL,
|
||||
started_at = NULL,
|
||||
execution_id = NULL,
|
||||
next_attempt_at = NULL,
|
||||
last_error_code = NULL,
|
||||
finished_at = NULL,
|
||||
failed_published_at = NULL
|
||||
WHERE id = :job_id
|
||||
"""),
|
||||
{"job_id": job_id},
|
||||
)
|
||||
|
||||
await session.execute(
|
||||
text("""
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = :attachment_id
|
||||
"""),
|
||||
{"attachment_id": row.attachment_id},
|
||||
)
|
||||
|
||||
print(
|
||||
f"REPLAY ACCEPTED: job={job_id}, "
|
||||
f"generation={row.generation + 1}"
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("job_id", type=UUID)
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
await replay(args.job_id)
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
docker compose restart postgres redis api-1 api-2
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?}"
|
||||
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
api-1|api-2)
|
||||
docker compose stop "$TARGET_INSTANCE_ID"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown instance: $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user