![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/public_html/ |
<?php
/**
* Script to update existing failed tracks with correct error messages from API
* This fixes tracks that show "Track generation failed. Please try again."
* instead of the actual error message like "Song Description contained artist name: skank"
*/
require_once 'config/database.php';
header('Content-Type: text/html; charset=utf-8');
echo "<!DOCTYPE html><html><head><title>Update Failed Track Errors</title>";
echo "<style>
body { font-family: Arial, sans-serif; margin: 20px; background: #1a1a1a; color: #e0e0e0; }
.success { color: #4ade80; }
.error { color: #f87171; }
.warning { color: #fbbf24; }
.info { color: #60a5fa; }
table { border-collapse: collapse; width: 100%; margin: 20px 0; background: #2a2a2a; }
th, td { border: 1px solid #444; padding: 10px; text-align: left; }
th { background: #333; }
tr:hover { background: #333; }
.stats { background: #2a2a2a; padding: 15px; border-radius: 8px; margin: 20px 0; }
</style></head><body>";
echo "<h1>đ§ Update Failed Track Error Messages</h1>";
echo "<p>This script updates existing failed tracks with correct error messages from the API.</p>";
$pdo = getDBConnection();
if (!$pdo) {
echo "<p class='error'>â Database connection failed</p>";
exit;
}
// Get all failed tracks that might need error message updates
// We'll check tracks that either:
// 1. Don't have 'msg' in metadata
// 2. Have metadata but 'msg' is empty or generic
$stmt = $pdo->query("
SELECT
id,
user_id,
task_id,
title,
status,
metadata,
created_at
FROM music_tracks
WHERE status = 'failed'
ORDER BY created_at DESC
LIMIT 500
");
$failed_tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($failed_tracks)) {
echo "<p class='success'>â
No failed tracks found!</p>";
exit;
}
echo "<div class='stats'>";
echo "<h3>đ Statistics</h3>";
echo "<p>Found <strong>" . count($failed_tracks) . "</strong> failed tracks to check</p>";
echo "</div>";
$api_key = '63edba40620216c5aa2c04240ac41dbd';
$updated_count = 0;
$skipped_count = 0;
$error_count = 0;
$no_task_id_count = 0;
$already_has_error_count = 0;
echo "<table>";
echo "<tr><th>ID</th><th>Title</th><th>Task ID</th><th>Current Error</th><th>API Error</th><th>Status</th></tr>";
foreach ($failed_tracks as $track) {
$track_id = $track['id'];
$task_id = $track['task_id'];
$title = htmlspecialchars($track['title'] ?? 'Untitled');
// Parse existing metadata
$metadata = [];
$current_error_msg = null;
if (!empty($track['metadata'])) {
$metadata = json_decode($track['metadata'], true) ?: [];
$current_error_msg = $metadata['msg'] ??
$metadata['error'] ??
$metadata['error_msg'] ??
$metadata['message'] ??
null;
}
// Check if track already has a proper error message (not generic)
$has_proper_error = !empty($current_error_msg) &&
$current_error_msg !== 'Track generation failed. Please try again.' &&
$current_error_msg !== 'Generation failed' &&
stripos($current_error_msg, 'please try again') === false;
if ($has_proper_error) {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id ?? 'N/A', 0, 20) . "...</td>";
echo "<td class='success'>" . htmlspecialchars(substr($current_error_msg, 0, 50)) . "...</td>";
echo "<td>-</td>";
echo "<td class='info'>â
Already has error message</td>";
echo "</tr>";
$already_has_error_count++;
continue;
}
// Skip if no valid task_id
if (empty($task_id) || $task_id === 'unknown' ||
str_starts_with($task_id, 'temp_') ||
str_starts_with($task_id, 'retry_')) {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td class='warning'>Invalid/No Task ID</td>";
echo "<td>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td>-</td>";
echo "<td class='warning'>â ī¸ Skipped (no valid task_id)</td>";
echo "</tr>";
$no_task_id_count++;
$skipped_count++;
continue;
}
// Check API status for this track
$api_url = 'https://api.api.box/api/v1/status/' . $task_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json',
'User-Agent: SoundStudioPro/1.0'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error || $http_code !== 200 || !$response) {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id, 0, 20) . "...</td>";
echo "<td>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td>-</td>";
echo "<td class='error'>â API Error (HTTP $http_code" . ($curl_error ? ", cURL: $curl_error" : "") . ")</td>";
echo "</tr>";
$error_count++;
continue;
}
$api_data = json_decode($response, true);
if (!$api_data) {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id, 0, 20) . "...</td>";
echo "<td>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td>-</td>";
echo "<td class='error'>â Invalid API response</td>";
echo "</tr>";
$error_count++;
continue;
}
// Extract error information from API response
$api_error_msg = null;
$error_code = null;
// Check for error codes
if (isset($api_data['code']) && ($api_data['code'] == 400 || $api_data['code'] == 531 || $api_data['code'] >= 400)) {
$error_code = $api_data['code'];
$api_error_msg = $api_data['msg'] ?? $api_data['error'] ?? $api_data['error_msg'] ?? null;
} elseif (isset($api_data['status']) && in_array($api_data['status'], ['failed', 'error', 'rejected'])) {
$api_error_msg = $api_data['error'] ?? $api_data['msg'] ?? $api_data['error_msg'] ?? null;
$error_code = $api_data['code'] ?? 531;
}
if (!$api_error_msg) {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id, 0, 20) . "...</td>";
echo "<td>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td>-</td>";
echo "<td class='warning'>â ī¸ No error message in API response</td>";
echo "</tr>";
$skipped_count++;
continue;
}
// Merge error information into existing metadata
$metadata['code'] = $error_code ?? 531;
$metadata['msg'] = $api_error_msg;
$metadata['error_type'] = ($error_code == 400) ? 'content_violation' : 'generation_failed';
$metadata['error_timestamp'] = date('Y-m-d H:i:s');
$metadata['updated_by'] = 'update_failed_track_errors.php';
// Update database
$update_stmt = $pdo->prepare("
UPDATE music_tracks
SET metadata = ?, updated_at = NOW()
WHERE id = ?
");
if ($update_stmt->execute([json_encode($metadata), $track_id])) {
$rows_affected = $update_stmt->rowCount();
if ($rows_affected > 0) {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id, 0, 20) . "...</td>";
echo "<td class='error'>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td class='success'>" . htmlspecialchars(substr($api_error_msg, 0, 60)) . "...</td>";
echo "<td class='success'>â
Updated</td>";
echo "</tr>";
$updated_count++;
} else {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id, 0, 20) . "...</td>";
echo "<td>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td>" . htmlspecialchars(substr($api_error_msg, 0, 60)) . "...</td>";
echo "<td class='warning'>â ī¸ No rows affected</td>";
echo "</tr>";
$skipped_count++;
}
} else {
echo "<tr>";
echo "<td>$track_id</td>";
echo "<td>$title</td>";
echo "<td>" . substr($task_id, 0, 20) . "...</td>";
echo "<td>" . htmlspecialchars($current_error_msg ?? 'None') . "</td>";
echo "<td>" . htmlspecialchars(substr($api_error_msg, 0, 60)) . "...</td>";
echo "<td class='error'>â Database update failed</td>";
echo "</tr>";
$error_count++;
}
// Small delay to avoid overwhelming the API
usleep(100000); // 0.1 second
}
echo "</table>";
echo "<div class='stats'>";
echo "<h3>đ Summary</h3>";
echo "<p class='success'>â
Updated: <strong>$updated_count</strong> tracks</p>";
echo "<p class='info'>âšī¸ Already had error message: <strong>$already_has_error_count</strong> tracks</p>";
echo "<p class='warning'>â ī¸ Skipped (no valid task_id): <strong>$no_task_id_count</strong> tracks</p>";
echo "<p class='warning'>â ī¸ Skipped (other reasons): <strong>" . ($skipped_count - $no_task_id_count) . "</strong> tracks</p>";
echo "<p class='error'>â Errors: <strong>$error_count</strong> tracks</p>";
echo "<p><strong>Total processed:</strong> " . count($failed_tracks) . " tracks</p>";
echo "</div>";
echo "<p class='success'><strong>â
Update complete!</strong> Failed tracks now have correct error messages.</p>";
echo "</body></html>";
?>