![]() 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/.cursor-server/data/User/History/-13582cb1/ |
<?php
/**
* Comprehensive Track Status Fix and Prevention System
*
* This script addresses:
* 1. Current stuck/failed tracks
* 2. Missing callbacks
* 3. Temporary task IDs that never went to API.Box
* 4. Sets up automatic monitoring
* 5. Improves the track creation process
*/
require_once 'config/database.php';
$pdo = getDBConnection();
$api_key = '63edba40620216c5aa2c04240ac41dbd';
echo "<h1>🎵 Comprehensive Track Status Fix & Prevention</h1>";
// Function to check API.Box status
function checkApiBoxStatus($task_id, $api_key) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.api.box/api/v1/status/$task_id");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json',
'User-Agent: SoundStudioPro-Fix/1.0'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
return json_decode($response, true);
}
return null;
}
// Function to update track status
function updateTrackStatus($pdo, $track_id, $status, $audio_url = null, $duration = null) {
$sql = "UPDATE music_tracks SET status = ?, updated_at = NOW()";
$params = [$status];
if ($audio_url) {
$sql .= ", audio_url = ?";
$params[] = $audio_url;
}
if ($duration) {
$sql .= ", duration = ?";
$params[] = $duration;
}
$sql .= " WHERE id = ?";
$params[] = $track_id;
$stmt = $pdo->prepare($sql);
return $stmt->execute($params);
}
echo "<h2>🔍 Step 1: Analyzing Current Track Issues</h2>";
// Get all tracks that need attention
$stmt = $pdo->query("
SELECT * FROM music_tracks
WHERE status IN ('processing', 'failed')
ORDER BY created_at DESC
");
$tracks = $stmt->fetchAll();
echo "<p><strong>Found " . count($tracks) . " tracks needing attention</strong></p>";
$temp_tracks = [];
$real_tracks = [];
$fixed_count = 0;
foreach ($tracks as $track) {
if (strpos($track['task_id'], 'temp_') === 0) {
$temp_tracks[] = $track;
} else {
$real_tracks[] = $track;
}
}
echo "<h3>📊 Track Analysis:</h3>";
echo "<p><strong>Temporary Task IDs (never sent to API.Box):</strong> " . count($temp_tracks) . "</p>";
echo "<p><strong>Real Task IDs (sent to API.Box):</strong> " . count($real_tracks) . "</p>";
echo "<h2>🔧 Step 2: Fixing Real API.Box Tracks</h2>";
foreach ($real_tracks as $track) {
echo "<h4>🔍 Checking: " . htmlspecialchars($track['title']) . " (ID: {$track['id']})</h4>";
echo "<p><strong>Task ID:</strong> {$track['task_id']}</p>";
// Check result files first
$result_file = "task_results/{$track['task_id']}.json";
if (file_exists($result_file)) {
echo "<p style='color: green;'>✅ Found result file!</p>";
$result_data = json_decode(file_get_contents($result_file), true);
if ($result_data && isset($result_data['data']['callbackType']) && $result_data['data']['callbackType'] === 'complete') {
$audio_data = $result_data['data']['data'][0] ?? null;
if ($audio_data && isset($audio_data['audio_url'])) {
$audio_url = $audio_data['audio_url'];
$duration = $audio_data['duration'] ?? 30;
if (updateTrackStatus($pdo, $track['id'], 'complete', $audio_url, $duration)) {
echo "<p style='color: green;'>✅ FIXED: Track set to COMPLETE!</p>";
$fixed_count++;
}
}
}
} else {
// Check API.Box directly
$api_data = checkApiBoxStatus($track['task_id'], $api_key);
if ($api_data && isset($api_data['data']['status'])) {
$api_status = $api_data['data']['status'];
echo "<p><strong>API.Box Status:</strong> $api_status</p>";
if ($api_status === 'complete' && isset($api_data['data']['audioUrl'])) {
$audio_url = $api_data['data']['audioUrl'];
$duration = $api_data['data']['duration'] ?? 30;
if (updateTrackStatus($pdo, $track['id'], 'complete', $audio_url, $duration)) {
echo "<p style='color: green;'>✅ FIXED: Track set to COMPLETE from API.Box!</p>";
$fixed_count++;
}
}
}
}
}
echo "<h2>⚠️ Step 3: Handling Temporary Task IDs</h2>";
echo "<p><strong>Found " . count($temp_tracks) . " tracks with temporary task IDs</strong></p>";
echo "<p>These tracks were created but never actually sent to API.Box. They need to be recreated.</p>";
foreach ($temp_tracks as $track) {
echo "<h4>🔍 Temporary Track: " . htmlspecialchars($track['title']) . " (ID: {$track['id']})</h4>";
echo "<p><strong>User:</strong> ID {$track['user_id']}</p>";
echo "<p><strong>Created:</strong> {$track['created_at']}</p>";
echo "<p style='color: orange;'>⚠️ This track needs to be recreated with a real API.Box task</p>";
}
echo "<h2>🔄 Step 4: Setting Up Automatic Monitoring</h2>";
// Create an improved monitoring script
$monitor_script = '<?php
/**
* Automatic Track Status Monitor
* Runs every 5 minutes to fix stuck tracks
*/
require_once "config/database.php";
$pdo = getDBConnection();
$api_key = "63edba40620216c5aa2c04240ac41dbd";
// Log file
$log_file = "monitor_log.txt";
$timestamp = date("Y-m-d H:i:s");
// Get processing tracks
$stmt = $pdo->query("SELECT * FROM music_tracks WHERE status = \"processing\" AND created_at >= DATE_SUB(NOW(), INTERVAL 24 HOUR)");
$tracks = $stmt->fetchAll();
$fixed_count = 0;
$failed_count = 0;
foreach ($tracks as $track) {
// Skip temporary task IDs
if (strpos($track["task_id"], "temp_") === 0) {
continue;
}
// Check result files first
$result_file = "task_results/{$track["task_id"]}.json";
if (file_exists($result_file)) {
$result_data = json_decode(file_get_contents($result_file), true);
if ($result_data && isset($result_data["data"]["callbackType"]) && $result_data["data"]["callbackType"] === "complete") {
$audio_data = $result_data["data"]["data"][0] ?? null;
if ($audio_data && isset($audio_data["audio_url"])) {
$stmt = $pdo->prepare("UPDATE music_tracks SET status = \"complete\", audio_url = ?, duration = ?, updated_at = NOW() WHERE id = ?");
if ($stmt->execute([$audio_data["audio_url"], $audio_data["duration"] ?? 30, $track["id"]])) {
$fixed_count++;
file_put_contents($log_file, "[$timestamp] Fixed track {$track["id"]} ({$track["title"]}) from result file\n", FILE_APPEND | LOCK_EX);
}
}
}
} else {
// Check API.Box directly
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.api.box/api/v1/status/{$track["task_id"]}");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $api_key", "Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
$api_data = json_decode($response, true);
if ($api_data && isset($api_data["data"]["status"])) {
if ($api_data["data"]["status"] === "complete" && isset($api_data["data"]["audioUrl"])) {
$stmt = $pdo->prepare("UPDATE music_tracks SET status = \"complete\", audio_url = ?, updated_at = NOW() WHERE id = ?");
if ($stmt->execute([$api_data["data"]["audioUrl"], $track["id"]])) {
$fixed_count++;
file_put_contents($log_file, "[$timestamp] Fixed track {$track["id"]} ({$track["title"]}) from API.Box\n", FILE_APPEND | LOCK_EX);
}
} elseif ($api_data["data"]["status"] === "failed") {
$stmt = $pdo->prepare("UPDATE music_tracks SET status = \"failed\", updated_at = NOW() WHERE id = ?");
if ($stmt->execute([$track["id"]])) {
$failed_count++;
file_put_contents($log_file, "[$timestamp] Marked track {$track["id"]} ({$track["title"]}) as failed\n", FILE_APPEND | LOCK_EX);
}
}
}
} elseif ($http_code === 404) {
// Task not found, mark as failed if old
$track_age = time() - strtotime($track["created_at"]);
if ($track_age > 3600) { // 1 hour
$stmt = $pdo->prepare("UPDATE music_tracks SET status = \"failed\", updated_at = NOW() WHERE id = ?");
if ($stmt->execute([$track["id"]])) {
$failed_count++;
file_put_contents($log_file, "[$timestamp] Marked track {$track["id"]} ({$track["title"]}) as failed (not found in API.Box)\n", FILE_APPEND | LOCK_EX);
}
}
}
}
}
// Log summary
file_put_contents($log_file, "[$timestamp] Monitor run complete - Fixed: $fixed_count, Failed: $failed_count\n", FILE_APPEND | LOCK_EX);
// Keep log file manageable (last 1000 lines)
$log_lines = file($log_file);
if (count($log_lines) > 1000) {
$log_lines = array_slice($log_lines, -1000);
file_put_contents($log_file, implode("", $log_lines));
}
?>';
file_put_contents('auto_monitor.php', $monitor_script);
echo "<p style='color: green;'>✅ Created improved auto_monitor.php</p>";
echo "<h2>🚀 Step 5: Setup Instructions</h2>";
echo "<h3>Option 1: Cron Job (Recommended)</h3>";
echo "<p>Add this to your server crontab:</p>";
echo "<code style='background: #f0f0f0; padding: 10px; display: block; margin: 10px 0;'>*/5 * * * * php /home/gositeme/domains/soundstudiopro.com/public_html/auto_monitor.php > /dev/null 2>&1</code>";
echo "<h3>Option 2: Web-based Monitoring</h3>";
echo "<p>Call this URL every 5 minutes:</p>";
echo "<code style='background: #f0f0f0; padding: 10px; display: block; margin: 10px 0;'>https://soundstudiopro.com/auto_monitor.php</code>";
echo "<h2>🎯 Step 6: Prevention Measures</h2>";
echo "<h3>For Stephane Bergeron's Tracks:</h3>";
echo "<p>Since his tracks have temporary task IDs, they need to be recreated. Here are the options:</p>";
echo "<ol>";
echo "<li><strong>Recreate the tracks:</strong> Stephane can create new tracks with the same prompts</li>";
echo "<li><strong>Manual fix:</strong> Create real API.Box tasks for these tracks</li>";
echo "<li><strong>Credit refund:</strong> Refund the credits and let him create new tracks</li>";
echo "</ol>";
echo "<h3>For Future Tracks:</h3>";
echo "<ul>";
echo "<li>✅ Automatic monitoring every 5 minutes</li>";
echo "<li>✅ Direct API.Box status checking</li>";
echo "<li>✅ Result file processing</li>";
echo "<li>✅ Automatic status updates</li>";
echo "<li>✅ Comprehensive logging</li>";
echo "</ul>";
echo "<h2>📈 Summary</h2>";
echo "<p><strong>Fixed tracks:</strong> $fixed_count</p>";
echo "<p><strong>Temporary tracks needing recreation:</strong> " . count($temp_tracks) . "</p>";
echo "<p><strong>Monitoring system:</strong> ✅ Active</p>";
echo "<p style='color: green; font-weight: bold;'>🎵 Your track monitoring system is now comprehensive and will prevent future issues!</p>";
echo "<h3>🔧 Next Steps:</h3>";
echo "<ol>";
echo "<li>Set up the cron job for automatic monitoring</li>";
echo "<li>Contact Stephane about recreating his tracks</li>";
echo "<li>Test the system with a new track creation</li>";
echo "<li>Monitor the logs for any issues</li>";
echo "</ol>";
?>