This commit is contained in:
Lizandro Guarnizo
2026-02-21 10:49:39 -05:00
parent 72e7f8c242
commit a3e089caab
2 changed files with 15 additions and 8 deletions
+11 -4
View File
@@ -231,11 +231,15 @@ class MediaService {
} }
$tmpFile = tempnam(sys_get_temp_dir(), 'wget_'); $tmpFile = tempnam(sys_get_temp_dir(), 'wget_');
// Solo pasar Authorization header para evitar problemas con User-Agent personalizado en CDN
$headerArgs = ''; $headerArgs = '';
foreach ($headers as $h) { foreach ($headers as $h) {
$headerArgs .= ' --header=' . escapeshellarg($h); if (stripos($h, 'Authorization:') === 0) {
$headerArgs .= ' --header=' . escapeshellarg($h);
}
} }
$cmd = 'wget -q --prefer-family=IPv4 -O ' . escapeshellarg($tmpFile) . $headerArgs . ' ' . escapeshellarg($url) . ' 2>&1'; // --tries=5: reintenta automáticamente ante Connection reset by peer (problema con lookaside.fbsbx.com)
$cmd = 'wget --tries=5 --prefer-family=IPv4 -O ' . escapeshellarg($tmpFile) . $headerArgs . ' ' . escapeshellarg($url) . ' 2>&1';
try { try {
exec($cmd, $output, $exitCode); exec($cmd, $output, $exitCode);
} catch (\Error $e) { } catch (\Error $e) {
@@ -244,14 +248,17 @@ class MediaService {
return $this->procOpenDownload($url, $headers); return $this->procOpenDownload($url, $headers);
} }
if ($exitCode === 0 && file_exists($tmpFile) && filesize($tmpFile) > 0) { $fileSize = (file_exists($tmpFile) ? filesize($tmpFile) : 0);
error_log("[MediaService::shellDownload] wget exit={$exitCode} size={$fileSize} output=" . implode(' | ', array_slice($output, -3)));
if ($exitCode === 0 && $fileSize > 0) {
$content = file_get_contents($tmpFile); $content = file_get_contents($tmpFile);
unlink($tmpFile); unlink($tmpFile);
error_log("[MediaService::shellDownload] wget OK, " . strlen($content) . " bytes"); error_log("[MediaService::shellDownload] wget OK, " . strlen($content) . " bytes");
return $content; return $content;
} }
error_log("[MediaService::shellDownload] wget FAIL exit={$exitCode}"); error_log("[MediaService::shellDownload] wget FAIL exit={$exitCode} size={$fileSize}");
if (file_exists($tmpFile)) unlink($tmpFile); if (file_exists($tmpFile)) unlink($tmpFile);
return false; return false;
} }
+4 -4
View File
@@ -119,16 +119,16 @@ fi
echo "⏰ Configurando cron jobs..." echo "⏰ Configurando cron jobs..."
cat > /etc/crontabs/root <<EOF cat > /etc/crontabs/root <<EOF
# Procesar mensajes delayed cada minuto # 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 * * * * * 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');" >> /tmp/delayed_processor.log 2>&1
# Procesar cola de media pendiente cada 2 minutos # Procesar cola de media pendiente cada 2 minutos
*/2 * * * * cd /var/www/html && /usr/local/bin/php scripts/media_worker.php 2>&1 | logger -t media-worker */2 * * * * cd /var/www/html && /usr/local/bin/php scripts/media_worker.php >> /tmp/media_worker.log 2>&1
# Limpiar logs antiguos (diario a las 3 AM) # 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 0 3 * * * cd /var/www/html && /usr/local/bin/php -r "require 'classes/LoggerFactory.php'; echo LoggerFactory::cleanup(30) . ' logs eliminados';" >> /tmp/log_cleanup.log 2>&1
# Health check de workers (cada 5 minutos) # 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 */5 * * * * supervisorctl status whatsapp-worker:* | grep -q RUNNING || supervisorctl restart whatsapp-worker:* >> /tmp/worker_monitor.log 2>&1
EOF EOF
echo "✅ Configuración completada" echo "✅ Configuración completada"