/dev/null', $out, $rc); if ($rc === 0) return true; $ps = @shell_exec('ps -eo args= 2>/dev/null'); if (!is_string($ps) || $ps === '') return false; foreach (explode("\n", $ps) as $line) { if (strpos($line, 'mvlog_worker.py') !== false && strpos($line, '--remote-root /var/www/html') !== false) { return true; } } return false; } function mvlog_rm_rf(string $path): void { if (!is_dir($path)) return; $items = scandir($path); if (!is_array($items)) return; foreach ($items as $item) { if ($item === '.' || $item === '..') continue; $child = $path . '/' . $item; if (is_dir($child) && !is_link($child)) mvlog_rm_rf($child); else @unlink($child); } @rmdir($path); } function mvlog_cleanup_stale_processing(string $dir, string $name, array $state, string $lockDir, bool $locked, string $status, string $articleId): array { $stateFile = $dir . '/.movmaker-state.json'; $started = ''; if (is_file($lockDir . '/started_at')) $started = trim((string)@file_get_contents($lockDir . '/started_at')); if ($started === '') $started = (string)($state['started_at'] ?? $state['updated_at'] ?? ''); $backup = ''; if (is_file($stateFile)) { $backup = $stateFile . '.bak.' . date('YmdHis'); @copy($stateFile, $backup); @chmod($backup, 0664); } $state['status'] = 'error'; $state['updated_at'] = gmdate('c'); if ($articleId !== '') $state['article_id'] = $articleId; $state['message'] = 'Previous render appears abandoned: no mvlog-worker process is running; stale lock/processing state was cleaned by job_status.php. Re-enable Render to retry.'; $json = json_encode($state, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n"; @file_put_contents($stateFile, $json, LOCK_EX); @chmod($stateFile, 0664); if ($locked) mvlog_rm_rf($lockDir); mvlog_job_status_log('cleaned stale processing job=' . $name . ' article_id=' . ($articleId ?: '-') . ' started_at=' . ($started ?: '-') . ' status=' . ($status ?: '-') . ' lock=' . ($locked ? 'yes' : 'no') . ' backup=' . ($backup ?: '-')); return $state; } $jobs = []; $switches = []; $cleaned = []; $workerRunning = mvlog_worker_running(); $dirs = glob($config['uploads_dir'] . '/*', GLOB_ONLYDIR) ?: []; foreach ($dirs as $dir) { $name = basename($dir); $stateFile = $dir . '/.movmaker-state.json'; $lockDir = $dir . '/.movmaker-lock'; $idFile = $dir . '/.mvlog-id'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (!is_array($state)) $state = []; $status = (string)($state['status'] ?? ''); $articleId = strtolower((string)($state['article_id'] ?? '')); if ($articleId === '' && is_file($idFile)) $articleId = strtolower(trim((string)file_get_contents($idFile))); if (!preg_match('/^[0-9]{14}[a-f0-9]{16}$/', $articleId)) $articleId = ''; $locked = is_dir($lockDir); if (!$workerRunning && ($locked || $status === 'processing')) { $state = mvlog_cleanup_stale_processing($dir, $name, $state, $lockDir, $locked, $status, $articleId); $status = (string)($state['status'] ?? ''); $locked = is_dir($lockDir); $cleaned[] = ['dir' => $name, 'id' => $articleId, 'status' => $status]; } $switches[] = [ 'dir' => $name, 'id' => $articleId, 'enabled' => is_file($dir . '/.movmaker-enabled'), 'preview' => is_file($dir . '/.movmaker-preview'), ]; if ($locked || $status === 'processing') { $started = ''; if (is_file($lockDir . '/started_at')) $started = trim((string)file_get_contents($lockDir . '/started_at')); if ($started === '') $started = (string)($state['started_at'] ?? $state['updated_at'] ?? ''); $jobs[] = [ 'name' => $name, 'id' => $articleId, 'status' => $status ?: ($locked ? 'processing' : 'unknown'), 'started_at' => $started, ]; } } echo json_encode(['jobs' => $jobs, 'switches' => $switches, 'cleaned' => $cleaned, 'worker_running' => $workerRunning, 'checked_at' => date('c')], JSON_UNESCAPED_UNICODE);