Реализовала лабораторную 3 на оценку 4

This commit is contained in:
MacBook
2026-10-16 12:42:16 +03:00
parent 0ab3a341aa
commit c1d62f7f23
32 changed files with 2141 additions and 35 deletions
+42
View File
@@ -0,0 +1,42 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p evidence
tmp_file=$(mktemp)
trap 'rm -f "$tmp_file"' EXIT
docker compose --env-file .env exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 >"$tmp_file" <<'SQL'
INSERT INTO users(id, username, display_name, password_hash)
VALUES ('00000000-0000-4000-8000-000000000001', 'explain_user', 'EXPLAIN fixture', 'fixture-not-for-login')
ON CONFLICT (id) DO NOTHING;
INSERT INTO chats(id, title)
VALUES ('00000000-0000-4000-8000-000000000002', 'EXPLAIN history fixture')
ON CONFLICT (id) DO NOTHING;
INSERT INTO chat_members(chat_id, user_id)
VALUES ('00000000-0000-4000-8000-000000000002', '00000000-0000-4000-8000-000000000001')
ON CONFLICT DO NOTHING;
DELETE FROM messages WHERE chat_id = '00000000-0000-4000-8000-000000000002';
INSERT INTO messages(id, chat_id, sender_id, text, created_at)
SELECT md5('history-message-' || n::text)::uuid,
'00000000-0000-4000-8000-000000000002',
'00000000-0000-4000-8000-000000000001',
'message ' || n::text,
TIMESTAMPTZ '2026-01-01 00:00:00+00' + n * INTERVAL '1 microsecond'
FROM generate_series(1, 10000) AS n;
ANALYZE messages;
SELECT count(*) AS fixture_messages
FROM messages
WHERE chat_id = '00000000-0000-4000-8000-000000000002';
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = '00000000-0000-4000-8000-000000000002'
ORDER BY created_at, id
LIMIT 50;
SQL
cp "$tmp_file" evidence/explain-history.txt
cat "$tmp_file"
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
set -eu
mkdir -p secrets
if [ ! -f secrets/postgres_password ]; then
umask 077
openssl rand -hex 24 > secrets/postgres_password
fi
if [ ! -f secrets/redis_password ]; then
umask 077
openssl rand -hex 24 > secrets/redis_password
fi
postgres_password=$(cat secrets/postgres_password)
redis_password=$(cat secrets/redis_password)
umask 077
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$postgres_password" > secrets/database_url
printf 'redis://:%s@redis:6379/0\n' "$redis_password" > secrets/cache_url
if [ ! -f .env ]; then cp .env.example .env; fi
printf '%s\n' 'Local .env and secret files are ready.'
+26
View File
@@ -0,0 +1,26 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
wait_healthy() {
service=$1
container_id=$(docker compose --env-file .env ps -q "$service")
attempts=0
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container_id" 2>/dev/null || true)" = healthy ]; do
attempts=$((attempts + 1))
if [ "$attempts" -ge 60 ]; then
printf '%s\n' "$service did not become healthy" >&2
exit 1
fi
sleep 1
done
}
docker compose --env-file .env restart postgres redis >/dev/null
wait_healthy postgres
wait_healthy redis
docker compose --env-file .env restart api1 api2 >/dev/null
wait_healthy api1
wait_healthy api2
docker compose --env-file .env restart nginx >/dev/null
wait_healthy nginx
+25
View File
@@ -0,0 +1,25 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
wait_healthy() {
service=$1
container_id=$(docker compose --env-file .env ps -q "$service")
attempts=0
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container_id" 2>/dev/null || true)" = healthy ]; do
attempts=$((attempts + 1))
if [ "$attempts" -ge 60 ]; then
printf '%s\n' "$service did not become healthy" >&2
exit 1
fi
sleep 1
done
}
docker compose --env-file .env build migrate >/dev/null
docker compose --env-file .env run --rm --no-deps migrate >/dev/null
for service in api1 api2; do
docker compose --env-file .env up -d --no-deps --force-recreate "$service" >/dev/null
wait_healthy "$service"
docker compose --env-file .env exec -T nginx nginx -s reload >/dev/null
done
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env start api1 api2 >/dev/null
for service in api1 api2; do
container_id=$(docker compose --env-file .env ps -q "$service")
attempts=0
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container_id" 2>/dev/null || true)" = healthy ]; do
attempts=$((attempts + 1))
if [ "$attempts" -ge 60 ]; then
printf '%s\n' "$service did not become healthy" >&2
exit 1
fi
sleep 1
done
done
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
case "$TARGET_INSTANCE_ID" in
api-1) service=api1 ;;
api-2) service=api2 ;;
*) printf '%s\n' "Unknown API instance alias" >&2; exit 2 ;;
esac
docker compose --env-file .env stop --timeout 3 "$service" >/dev/null