Skip to content

Commit

Permalink
move startup sequence around
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed Jan 14, 2025
1 parent d965f43 commit c0deb3a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
16 changes: 8 additions & 8 deletions backend/deploy_tmp/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
DB_HOST="127.0.0.1"
DB_PORT="3141"
KV_HOST="127.0.0.1"
KV_PORT="6379"
MAIL_ENABLED ="True"
SMTP_SERVER ="mail.dsavdodeka.nl"
SMTP_PORT ="465"
RECREATE="yes"
export DB_HOST="127.0.0.1"
export DB_PORT="3141"
export KV_HOST="127.0.0.1"
export KV_PORT="6379"
export MAIL_ENABLED ="True"
export SMTP_SERVER ="mail.dsavdodeka.nl"
export SMTP_PORT ="465"
export RECREATE="yes"
21 changes: 4 additions & 17 deletions backend/deploy_tmp/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,10 @@ set -a
cd ../../authpage
echo "VITE_AUTHPAGE_AUTH_URL=https://api.dsavdodeka.nl" > .env.production
echo "VITE_AUTHPAGE_CLIENT_URL=https://dsavdodeka.nl" >> .env.production
npm ci
npm ci --force
npm run build -- --mode production

cd ../../deploy_tmp
cd ../backend/deploy_tmp

mv -f ./define.toml ../src/apiserver/resources/define.toml
mv -f ./env.toml ../src/apiserver/resources/env.toml

# Define the names of the environment variables
ENV_VARS=("DB_PASS" "KV_PASS" "MAIL_PASS", "KEY_PASS")

echo "Please enter values for the following environment variables:"
for VAR in "${ENV_VARS[@]}"; do
read -p "Enter value for $VAR: " VALUE
export "$VAR=$VALUE"
done

source .env

echo "Environment variables have been set in your current shell."
cp -f ./define.toml ../src/apiserver/resources/define.toml
cp -f ./env.toml ../src/apiserver/resources/env.toml
14 changes: 14 additions & 0 deletions backend/deploy_tmp/set_envs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Define the names of the environment variables
ENV_VARS=("DB_PASS" "KV_PASS" "MAIL_PASS" "KEY_PASS")

echo "Please enter values for the following environment variables:"
for VAR in "${ENV_VARS[@]}"; do
read -p "Enter value for $VAR: " VALUE
export "$VAR=$VALUE"
done

. ./.env

echo "Environment variables have been set in your current shell."
28 changes: 15 additions & 13 deletions backend/src/apiserver/app/ops/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from apiserver.data.api.classifications import insert_classification
from apiserver.data.source import KeyState
from apiserver import data
from apiserver.data import Source
from apiserver.data import Source, get_kv
from apiserver.data.admin import drop_recreate_database


Expand All @@ -39,24 +39,15 @@ async def startup(dsrc: Source, config: Config, recreate: bool = False) -> None:
attempting this, it uses a simple lock mechanism by letting the first process set a value in the KV.
"""

try:
# Store startup (tests connection)
await dsrc.store.startup()
except StoreObjectError as e:
raise AppError(
ErrorKeys.STARTUP,
"<magenta>Failed to start store! Did you start the databases?</magenta>",
"startup_store_failure",
) from e

# Checks lock: returns True if it is the first lock since at least 25 seconds (lock expire time)
is_first_process = await wait_for_lock_is_first(dsrc)
is_first_process = await wait_for_lock_is_first(get_kv(dsrc))

logger.debug(f"Unlocked startup, first={is_first_process}")
# Only recreates if it is also the first lock since at least 25 seconds (lock expire time)
logger.debug(f"Startup with recreate={recreate and is_first_process}")

# Set lock
await data.trs.startup.set_startup_lock(dsrc)
await data.trs.startup.set_startup_lock(get_kv(dsrc))
if is_first_process and recreate:
logger.warning(
"Dropping and recreating... Set `recreate='no'` in your config if you don't"
Expand All @@ -71,6 +62,17 @@ async def startup(dsrc: Source, config: Config, recreate: bool = False) -> None:
logger.warning("Initial population...")
await initial_population(dsrc, config)

# We do this after to ensure the main database exists
try:
# Store startup (tests connection)
await dsrc.store.startup()
except StoreObjectError as e:
raise AppError(
ErrorKeys.STARTUP,
"<magenta>Failed to start store! Did you start the databases?</magenta>",
"startup_store_failure",
) from e

# Load keys
logger.debug("Loading keys.")
key_state = await load_keys(dsrc, config)
Expand Down
9 changes: 5 additions & 4 deletions backend/src/apiserver/data/trs/startup.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from typing import Optional

from apiserver.data import Source, get_kv
from redis import Redis
from store.kv import store_string, get_string


async def set_startup_lock(dsrc: Source, value: str = "locked") -> None:
await store_string(get_kv(dsrc), "startup_lock", value, 25)
async def set_startup_lock(kv: Redis, value: str = "locked") -> None:
await store_string(kv, "startup_lock", value, 25)


async def startup_is_locked(dsrc: Source) -> Optional[bool]:
async def startup_is_locked(kv: Redis) -> Optional[bool]:
"""Returns None if it did not exist (so it is the first), True if it is locked, False if it is not locked but was
previously locked."""

lock = await get_string(get_kv(dsrc), "startup_lock")
lock = await get_string(kv, "startup_lock")

if lock is not None:
return lock == "locked"
Expand Down
4 changes: 2 additions & 2 deletions deploy/deploy_tmp/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
# <volume name in volumes below>:<container destination directory>
# This means the volume will be accessible from the container in the dest. directory in the container
- dodeka-db-volume:/dodeka-db
command: -c config_file=/dodeka-conf/postgresql.conf
command: -c config_file=/dodeka_conf/postgresql.conf
# Some environment variables depend on deployment variables, so we load those in manually
environment:
- PGDATA=/dodeka-db
Expand All @@ -30,7 +30,7 @@ services:
timeout: 10s
kv:
container_name: "dodeka-kv-production"
image: "ghcr.io/dsav-dodeka/kv:v3.0.0"
image: "ghcr.io/dsav-dodeka/redis:v3.0.0"
environment:
- REDIS_PASSWORD
ports:
Expand Down

0 comments on commit c0deb3a

Please sign in to comment.