1- # \ !/bin/bash
1+ #! /bin/bash
22# Docker entrypoint script for FFmpeg API
33# Handles initialization and service startup
44
@@ -10,28 +10,28 @@ wait_for_service() {
1010 local port=$2
1111 local service=$3
1212 local timeout=${4:- 60}
13-
13+
1414 echo " Waiting for $service at $host :$port ..."
1515 for i in $( seq 1 $timeout ) ; do
1616 if nc -z " $host " " $port " 2> /dev/null; then
17- echo " $service is ready\ !"
17+ echo " $service is ready!"
1818 return 0
1919 fi
2020 echo " Waiting for $service ... ($i /$timeout )"
2121 sleep 1
2222 done
23-
23+
2424 echo " ERROR: $service at $host :$port not available after $timeout seconds"
2525 return 1
2626}
2727
2828# Function to run database migrations
2929run_migrations () {
3030 echo " Running database migrations..."
31-
31+
3232 # Wait for PostgreSQL
3333 wait_for_service postgres 5432 " PostgreSQL" 120
34-
34+
3535 # Run Alembic migrations
3636 if [ -f " alembic.ini" ]; then
3737 echo " Running Alembic migrations..."
@@ -45,54 +45,54 @@ run_migrations() {
4545# Function to initialize storage
4646init_storage () {
4747 echo " Initializing storage directories..."
48-
48+
4949 # Create storage directories
5050 mkdir -p /storage/input /storage/output /storage/temp
5151 mkdir -p /app/logs /app/temp
52-
52+
5353 # Set permissions
5454 chmod 755 /storage/input /storage/output /storage/temp
5555 chmod 755 /app/logs /app/temp
56-
56+
5757 echo " Storage directories initialized."
5858}
5959
6060# Function to validate environment
6161validate_environment () {
6262 echo " Validating environment..."
63-
63+
6464 # Check required environment variables
6565 if [ -z " $DATABASE_URL " ]; then
6666 echo " ERROR: DATABASE_URL environment variable is required"
6767 exit 1
6868 fi
69-
69+
7070 if [ -z " $REDIS_URL " ]; then
7171 echo " ERROR: REDIS_URL environment variable is required"
7272 exit 1
7373 fi
74-
74+
7575 # Check FFmpeg installation
76- if \ ! command -v ffmpeg & > /dev/null; then
76+ if ! command -v ffmpeg & > /dev/null; then
7777 echo " ERROR: FFmpeg is not installed"
7878 exit 1
7979 fi
80-
81- if \ ! command -v ffprobe & > /dev/null; then
80+
81+ if ! command -v ffprobe & > /dev/null; then
8282 echo " ERROR: FFprobe is not installed"
8383 exit 1
8484 fi
85-
85+
8686 echo " Environment validation passed."
8787}
8888
8989# Function to setup monitoring
9090setup_monitoring () {
9191 echo " Setting up monitoring..."
92-
92+
9393 # Create metrics directory
9494 mkdir -p /app/metrics
95-
95+
9696 # Setup log rotation if available
9797 if command -v logrotate & > /dev/null; then
9898 echo " Setting up log rotation..."
@@ -108,35 +108,35 @@ setup_monitoring() {
108108}
109109LOGROTATE_EOF
110110 fi
111-
111+
112112 echo " Monitoring setup completed."
113113}
114114
115115# Main execution
116116main () {
117117 local service_type=${1:- api}
118-
118+
119119 echo " Starting FFmpeg API Docker Container..."
120120 echo " Service Type: $service_type "
121121 echo " Environment: ${ENVIRONMENT:- production} "
122-
122+
123123 # Initialize
124124 validate_environment
125125 init_storage
126126 setup_monitoring
127-
127+
128128 # Service-specific initialization
129129 case $service_type in
130130 " api" )
131131 echo " Starting API service..."
132-
132+
133133 # Wait for dependencies
134134 wait_for_service postgres 5432 " PostgreSQL" 120
135135 wait_for_service redis 6379 " Redis" 60
136-
136+
137137 # Run migrations (API service is responsible for this)
138138 run_migrations
139-
139+
140140 # Start API server
141141 exec uvicorn api.main:app \
142142 --host 0.0.0.0 \
@@ -146,14 +146,14 @@ main() {
146146 --access-log \
147147 --log-level ${LOG_LEVEL:- info}
148148 ;;
149-
149+
150150 " worker" )
151151 echo " Starting worker service..."
152-
152+
153153 # Wait for dependencies
154154 wait_for_service postgres 5432 " PostgreSQL" 120
155155 wait_for_service redis 6379 " Redis" 60
156-
156+
157157 # Start Celery worker
158158 exec celery -A worker.main worker \
159159 --loglevel=${LOG_LEVEL:- info} \
@@ -162,20 +162,20 @@ main() {
162162 --max-tasks-per-child=${WORKER_MAX_TASKS_PER_CHILD:- 100} \
163163 --time-limit=${WORKER_TASK_TIME_LIMIT:- 21600}
164164 ;;
165-
165+
166166 " migrate" )
167167 echo " Running migration service..."
168168 run_migrations
169169 echo " Migration completed successfully."
170170 ;;
171-
171+
172172 " setup" )
173173 echo " Running setup tasks..."
174174 validate_environment
175175 init_storage
176176 echo " Setup completed successfully."
177177 ;;
178-
178+
179179 * )
180180 echo " Unknown service type: $service_type "
181181 echo " Available service types: api, worker, migrate, setup"
@@ -187,13 +187,13 @@ main() {
187187# Signal handlers for graceful shutdown
188188shutdown () {
189189 echo " Received shutdown signal..."
190-
190+
191191 # Kill child processes
192- if [ \ ! -z " $\ !" ]; then
193- kill -TERM " $\ !" 2> /dev/null || true
194- wait " $\ !" 2> /dev/null || true
192+ if [ ! -z " $! " ]; then
193+ kill -TERM " $! " 2> /dev/null || true
194+ wait " $! " 2> /dev/null || true
195195 fi
196-
196+
197197 echo " Shutdown completed."
198198 exit 0
199199}
@@ -203,4 +203,3 @@ trap shutdown SIGTERM SIGINT
203203
204204# Run main function with all arguments
205205main " $@ "
206- EOF < /dev/null
0 commit comments