Files
whatsapp/docker/entrypoint.sh
T
2026-01-27 23:56:49 -05:00

97 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Entrypoint script para el contenedor
set -e
echo "🚀 Iniciando WhatsApp Bot Manager..."
# Esperar a que Redis esté disponible
echo "⏳ Esperando Redis..."
until redis-cli -h ${REDIS_HOST:-redis} ping 2>/dev/null; do
echo " Redis no disponible, esperando..."
sleep 2
done
echo "✅ Redis conectado"
# Esperar a que MySQL esté disponible (solo si no es localhost ni IP interna)
if [ "${DB_HOST}" != "localhost" ] && [ "${DB_HOST}" != "127.0.0.1" ] && [ "${DB_HOST}" != "db" ]; then
echo "️ Usando base de datos externa (${DB_HOST}), omitiendo verificación de conectividad..."
else
echo "⏳ Esperando MySQL..."
until mysql -h ${DB_HOST:-db} -u ${DB_USER:-whatsapp_user} -p${DB_PASS} -e "SELECT 1" >/dev/null 2>&1; do
echo " MySQL no disponible, esperando..."
sleep 2
done
echo "✅ MySQL conectado"
fi
# Crear directorios si no existen
mkdir -p /var/www/html/logs
mkdir -p /var/www/html/uploads
mkdir -p /var/cache/nginx
mkdir -p /run/php
# Establecer permisos
chown -R www:www /var/www/html/logs
chown -R www:www /var/www/html/uploads
chmod -R 755 /var/www/html/logs
chmod -R 755 /var/www/html/uploads
# Ejecutar migraciones de BD (si existen)
if [ -f /var/www/html/database/migrate.php ]; then
echo "🔄 Ejecutando migraciones..."
php /var/www/html/database/migrate.php || echo "⚠️ Migraciones fallaron o no aplicables"
fi
# Generar archivo .env desde variables de entorno (si no existe)
if [ ! -f /var/www/html/.env ]; then
echo "📝 Generando .env desde variables de entorno..."
cat > /var/www/html/.env <<EOF
# Generado automáticamente por Docker
DB_HOST=${DB_HOST:-db}
DB_PORT=${DB_PORT:-3306}
DB_NAME=${DB_NAME:-whatsapp_bot}
DB_USER=${DB_USER:-whatsapp_user}
DB_PASS=${DB_PASS}
REDIS_HOST=${REDIS_HOST:-redis}
REDIS_PORT=${REDIS_PORT:-6379}
REDIS_PASSWORD=${REDIS_PASSWORD:-}
REDIS_DB=0
WHATSAPP_TOKEN=${WHATSAPP_TOKEN}
WHATSAPP_PHONE_NUMBER_ID=${WHATSAPP_PHONE_NUMBER_ID}
WEBHOOK_VERIFY_TOKEN=${WEBHOOK_VERIFY_TOKEN}
LOG_LEVEL=${LOG_LEVEL:-INFO}
ENABLE_RATE_LIMIT=${ENABLE_RATE_LIMIT:-true}
EOF
chown www:www /var/www/html/.env
fi
# Limpiar cache de OPcache
if [ -f /usr/local/bin/php ]; then
echo "🧹 Limpiando cache de OPcache..."
php -r "if(function_exists('opcache_reset')) opcache_reset();"
fi
# Configurar crontab para tareas programadas
echo "⏰ Configurando cron jobs..."
cat > /etc/crontabs/www <<EOF
# Procesar mensajes delayed cada minuto
* * * * * cd /var/www/html && /usr/local/bin/php -r "require 'vendor/autoload.php'; use WhatsApp\Queue\RedisQueue; \$q = new RedisQueue(); \$q->processDelayed('messages'); \$q->processDelayed('media');" 2>&1 | logger -t delayed-processor
# Limpiar logs antiguos (diario a las 3 AM)
0 3 * * * cd /var/www/html && /usr/local/bin/php -r "require 'classes/LoggerFactory.php'; echo LoggerFactory::cleanup(30) . ' logs eliminados';" 2>&1 | logger -t log-cleanup
# Health check de workers (cada 5 minutos)
*/5 * * * * supervisorctl status whatsapp-worker:* | grep -q RUNNING || supervisorctl restart whatsapp-worker:* 2>&1 | logger -t worker-monitor
EOF
chown www:www /etc/crontabs/www
echo "✅ Configuración completada"
echo "🎯 Iniciando servicios..."
# Ejecutar comando pasado al contenedor
exec "$@"