diff --git a/scripts/dev-feed-worker.sh b/scripts/dev-feed-worker.sh index ae18b405..acdc8568 100644 --- a/scripts/dev-feed-worker.sh +++ b/scripts/dev-feed-worker.sh @@ -1,35 +1,13 @@ #!/bin/bash -# Check if Docker daemon is running -if ! docker info > /dev/null 2>&1; then - echo "Error: Docker daemon is not running. Please start Docker and try again." - exit 1 -fi +# Source the Redis setup script +source ./scripts/setup-redis.sh -# Check if the Redis container is running -if [ "$(docker ps -q -f name=annos-redis)" ]; then - echo "Redis container is already running." - REDIS_RUNNING=true -else - echo "Starting Redis container..." - npm run redis:start - REDIS_RUNNING=false -fi - -# Trap SIGINT and SIGTERM to stop containers on exit, only if we started them -function cleanup { - if [ "$REDIS_RUNNING" = false ]; then - if [ "$(docker ps -q -f name=annos-redis)" ]; then - echo "Stopping Redis container..." - npm run redis:stop - npm run redis:remove - fi - fi -} -trap cleanup SIGINT SIGTERM +# Trap SIGINT and SIGTERM to cleanup on exit +trap cleanup_redis SIGINT SIGTERM # Run the feed worker dev command npm run dev:worker:feeds # Cleanup after dev command exits -cleanup +cleanup_redis diff --git a/scripts/dev.sh b/scripts/dev.sh index 33192eae..535d51f6 100644 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -1,36 +1,13 @@ #!/bin/bash -# Check if Docker daemon is running -if ! docker info > /dev/null 2>&1; then - echo "Error: Docker daemon is not running. Please start Docker and try again." - exit 1 -fi +# Source the Postgres setup script +source ./scripts/setup-postgres.sh -# Check if the Postgres container is running -if [ "$(docker ps -q -f name=annos-postgres)" ]; then - echo "Postgres container is already running." - DB_RUNNING=true -else - echo "Starting Postgres container..." - npm run db:start - DB_RUNNING=false -fi - - -# Trap SIGINT and SIGTERM to stop containers on exit, only if we started them -function cleanup { - if [ "$DB_RUNNING" = false ]; then - if [ "$(docker ps -q -f name=annos-postgres)" ]; then - echo "Stopping Postgres container..." - npm run db:stop - npm run db:remove - fi - fi -} -trap cleanup SIGINT SIGTERM +# Trap SIGINT and SIGTERM to cleanup on exit +trap cleanup_postgres SIGINT SIGTERM # Run the dev command npm run dev:inner # Cleanup after dev command exits -cleanup +cleanup_postgres diff --git a/scripts/setup-postgres.sh b/scripts/setup-postgres.sh new file mode 100644 index 00000000..f000822f --- /dev/null +++ b/scripts/setup-postgres.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Check if Docker daemon is running +if ! docker info > /dev/null 2>&1; then + echo "Error: Docker daemon is not running. Please start Docker and try again." + exit 1 +fi + +# Check if the Postgres container is running +if [ "$(docker ps -q -f name=annos-postgres)" ]; then + echo "Postgres container is already running." + export DB_RUNNING=true +else + echo "Starting Postgres container..." + npm run db:start + export DB_RUNNING=false +fi + +# Trap SIGINT and SIGTERM to stop containers on exit, only if we started them +function cleanup_postgres { + if [ "$DB_RUNNING" = false ]; then + if [ "$(docker ps -q -f name=annos-postgres)" ]; then + echo "Stopping Postgres container..." + npm run db:stop + npm run db:remove + fi + fi +} + +# Export the cleanup function so it can be called from other scripts +export -f cleanup_postgres diff --git a/scripts/setup-redis.sh b/scripts/setup-redis.sh new file mode 100644 index 00000000..14aaf8ef --- /dev/null +++ b/scripts/setup-redis.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Check if Docker daemon is running +if ! docker info > /dev/null 2>&1; then + echo "Error: Docker daemon is not running. Please start Docker and try again." + exit 1 +fi + +# Check if the Redis container is running +if [ "$(docker ps -q -f name=annos-redis)" ]; then + echo "Redis container is already running." + export REDIS_RUNNING=true +else + echo "Starting Redis container..." + npm run redis:start + export REDIS_RUNNING=false +fi + +# Trap SIGINT and SIGTERM to stop containers on exit, only if we started them +function cleanup_redis { + if [ "$REDIS_RUNNING" = false ]; then + if [ "$(docker ps -q -f name=annos-redis)" ]; then + echo "Stopping Redis container..." + npm run redis:stop + npm run redis:remove + fi + fi +} + +# Export the cleanup function so it can be called from other scripts +export -f cleanup_redis