Refactor Docker Entrypoint and Environment Variables

- Removed the `IS_API_WORKER` environment variable from `docker-compose.yml`, simplifying the configuration for API services and aligning with the new role-based command handling.
- Updated the `php-fpm-entrypoint` script to determine the role (API, worker, or scheduler) based on the command being executed, enhancing flexibility and clarity in service initialization.
- Streamlined the setup process for different roles, ensuring that appropriate commands are executed based on the determined role, which improves maintainability and reduces potential errors.

These changes aim to enhance the Docker configuration by adopting a more dynamic approach to service roles, improving the overall deployment process.
This commit is contained in:
Julien Nahum 2025-05-22 13:21:56 +02:00
parent 3200253163
commit 7d7aba10b1
2 changed files with 46 additions and 31 deletions

View File

@ -7,7 +7,6 @@ services:
- ./api/storage:/usr/share/nginx/html/storage:rw
environment: &api-env
APP_ENV: production
IS_API_WORKER: "false"
# Database settings
DB_HOST: db
REDIS_HOST: redis
@ -41,7 +40,6 @@ services:
environment:
<<: *api-env
APP_ENV: production
IS_API_WORKER: "true"
healthcheck:
test: ["CMD-SHELL", "pgrep -f 'php artisan queue:work' > /dev/null || exit 1"]
interval: 60s
@ -56,8 +54,6 @@ services:
environment:
<<: *api-env
APP_ENV: production
IS_API_WORKER: "true" # This might not be strictly true for scheduler, but consistent with setup
CONTAINER_ROLE: scheduler
healthcheck:
test: ["CMD-SHELL", "php /usr/share/nginx/html/artisan app:scheduler-status --mode=check --max-minutes=3 || exit 1"]
interval: 60s

View File

@ -1,26 +1,50 @@
#!/bin/bash
main() {
if [ "$IS_API_WORKER" = "true" ]; then
# This is the API worker, skip setup and just run the command
apply_php_configuration
exec "$@"
else
# This is the API service or scheduler, run full setup
apply_php_configuration
prep_file_permissions
prep_storage
wait_for_db
local COMMAND_AS_STRING="$*"
local ROLE="api" # Default role
case "$COMMAND_AS_STRING" in
*"artisan queue:work"*)
ROLE="worker"
;;
*"artisan schedule:work"*)
ROLE="scheduler"
;;
*)
# Defaults to "api" for any other command (e.g., php-fpm)
ROLE="api"
;;
esac
echo "Determined role: $ROLE for command: $COMMAND_AS_STRING"
apply_php_configuration
prep_file_permissions
prep_storage
wait_for_db
if [ "$ROLE" = "api" ]; then
echo "Running setup for API role..."
apply_db_migrations
run_init_project
optimize_application
if [ "$CONTAINER_ROLE" = "scheduler" ]; then
echo "Initializing scheduler status for first run (entrypoint)"
./artisan app:scheduler-status --mode=record
fi
run_server "$@"
echo "Starting server for API role with command: $@"
exec "$@"
elif [ "$ROLE" = "scheduler" ]; then
echo "Running setup for Scheduler role..."
echo "Initializing scheduler status for first run (entrypoint)"
php ./artisan app:scheduler-status --mode=record
echo "Starting scheduler with command: $@"
exec "$@"
elif [ "$ROLE" = "worker" ]; then
echo "Running setup for Worker role..."
echo "Starting worker with command: $@"
exec "$@"
else
# This case should ideally not be reached if ROLE defaults to "api"
echo "Error: Unknown role '$ROLE' determined from command '$COMMAND_AS_STRING'. Exiting."
exit 1
fi
}
@ -78,34 +102,29 @@ prep_storage() {
# Run Laravel's storage link command (ensure script is run from app root or adjust path to artisan)
echo "Creating public storage symlink"
./artisan storage:link
php ./artisan storage:link
}
wait_for_db() {
echo "Waiting for DB to be ready"
until ./artisan migrate:status 2>&1 | grep -q -E "(Migration table not found|Migration name)"; do
until php ./artisan migrate:status 2>&1 | grep -q -E "(Migration table not found|Migration name)"; do
sleep 1
done
}
apply_db_migrations() {
echo "Running DB Migrations"
./artisan migrate --force
php ./artisan migrate --force
}
run_init_project() {
echo "Running app:init-project command"
./artisan app:init-project
php ./artisan app:init-project
}
optimize_application() {
echo "Optimizing application"
./artisan optimize
}
run_server() {
echo "Starting server $@"
exec "$@"
php ./artisan optimize
}
main "$@"