&1"); $r = @file_get_contents($url); $d = @json_decode($r, true); return trim($d['output'] ?? ''); } if ($action === 'status') { $out = ['ok' => true, 'ts' => date('c'), 'doctrine' => '62-CRM-PIPE-GUARDS', 'option' => 'B (reactivation with guards)']; // send_contacts total live $out['send_contacts_total'] = intval(s95_query("SELECT COUNT(*) FROM admin.send_contacts")); // Last 20 runs $rows_raw = s95_query("SELECT action, count_before, count_after, delta, rc, ts FROM admin.deliverads_runs ORDER BY ts DESC LIMIT 20"); $runs = []; foreach (explode("\n", $rows_raw) as $line) { $parts = explode('|', $line); if (count($parts) >= 6) { $runs[] = [ 'action' => $parts[0], 'count_before' => intval($parts[1]), 'count_after' => intval($parts[2]), 'delta' => intval($parts[3]), 'rc' => intval($parts[4]), 'ts' => $parts[5], ]; } } $out['runs'] = $runs; // Stats 24h $ok_24h = intval(s95_query("SELECT COUNT(*) FROM admin.deliverads_runs WHERE ts > NOW() - interval '24 hours' AND rc = 0")); $err_24h = intval(s95_query("SELECT COUNT(*) FROM admin.deliverads_runs WHERE ts > NOW() - interval '24 hours' AND rc != 0")); $delta_today = intval(s95_query("SELECT COALESCE(SUM(delta), 0) FROM admin.deliverads_runs WHERE ts > DATE_TRUNC('day', NOW())")); $out['runs_ok_24h'] = $ok_24h; $out['runs_err_24h'] = $err_24h; $out['delta_today'] = $delta_today; // Last run age if (!empty($runs)) { $last_ts = strtotime($runs[0]['ts']); if ($last_ts) { $age = time() - $last_ts; $out['last_run_age'] = $age < 60 ? $age.'s' : ($age < 3600 ? round($age/60).'min' : round($age/3600).'h'); } } // Cron status $cron_url = "http://10.1.0.3:5890/api/sentinel-brain.php?action=exec&cmd=" . urlencode("ls /etc/cron.d/deliverads-automation 2>/dev/null && echo ACTIVE || echo DISABLED"); $cron_r = @json_decode(@file_get_contents($cron_url), true); $out['cron_status'] = strpos($cron_r['output'] ?? '', 'ACTIVE') !== false ? 'active' : 'disabled'; echo json_encode($out, JSON_PRETTY_PRINT); exit; } if ($action === 'run_now') { $url = "http://10.1.0.3:5890/api/sentinel-brain.php?action=exec&cmd=" . urlencode("sudo -n -u www-data /usr/local/bin/deliverads-guard.sh sync_all 2>&1 | tail -c 500"); $r = @file_get_contents($url); $d = @json_decode($r, true); echo json_encode(['ok' => true, 'output' => $d['output'] ?? '']); exit; } if ($action === 'disable') { $url = "http://10.1.0.3:5890/api/sentinel-brain.php?action=exec&cmd=" . urlencode("sudo -n mv /etc/cron.d/deliverads-automation /etc/cron.d/deliverads-automation.emergency-stop-" . date('Ymd-His')); $r = @file_get_contents($url); $d = @json_decode($r, true); echo json_encode(['ok' => true, 'action' => 'disabled', 'note' => 'renamed to .emergency-stop, not deleted (doctrine 59)', 'output' => $d['output'] ?? '']); exit; } if ($action === 'enable') { $url = "http://10.1.0.3:5890/api/sentinel-brain.php?action=exec&cmd=" . urlencode("sudo -n cp /tmp/deliverads.cron /etc/cron.d/deliverads-automation && ls -la /etc/cron.d/deliverads-automation"); $r = @file_get_contents($url); $d = @json_decode($r, true); echo json_encode(['ok' => true, 'action' => 'enabled', 'output' => $d['output'] ?? '']); exit; } echo json_encode(['ok' => true, 'actions' => ['status', 'run_now', 'disable', 'enable']]);