Files
weval-consulting/api/wedroid-scheduler.php

58 lines
2.7 KiB
PHP

<?php
// === WEVAL SECRETS LOADER ===
$_WEVAL_SECRETS = [];
if (file_exists('/etc/weval/secrets.env')) {
foreach (file('/etc/weval/secrets.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
if (strpos($line, '#') === 0) continue;
if (strpos($line, '=') !== false) {
list($k, $v) = explode('=', $line, 2);
$_WEVAL_SECRETS[trim($k)] = trim($v);
}
}
}
function _weval_secret_dup($key, $default='') {
global $_WEVAL_SECRETS;
return $_WEVAL_SECRETS[$key] ?? getenv($key) ?: $default;
}
require_once('/opt/wevads/config/credentials.php');
/**
* WEDROID Scheduler v1.0
* Autonomous scheduled tasks: nonreg, backup, health checks
*/
function wedroidRunSchedule() {
$pdo = new PDO("pgsql:host=127.0.0.1;dbname=adx_system","postgres","");
$pdo->exec("CREATE TABLE IF NOT EXISTS admin.wedroid_schedule (id SERIAL PRIMARY KEY, name TEXT UNIQUE, cmd TEXT, interval_min INT DEFAULT 60, last_run TIMESTAMP, next_run TIMESTAMP, enabled BOOLEAN DEFAULT true, last_result TEXT)");
// Default schedules
$defaults = [
['nonreg', 'bash /opt/wevads-v2/nonreg-all.sh 2>&1 | tail -3', 360],
['health_s95', 'echo "DISK:$(df -h / | tail -1 | awk \'{print $5}\')" && echo "APACHE:$(systemctl is-active apache2)" && echo "PG:$(systemctl is-active postgresql)"', 30],
['backup_check', 'ls -lh /opt/backups/20260318_030001/ | tail -4', 1440],
['git_status', 'cd /opt/wevads-arsenal && git status --short | wc -l', 120],
];
foreach ($defaults as $d) {
$stmt = $pdo->prepare("INSERT INTO admin.wedroid_schedule (name, cmd, interval_min, next_run) VALUES (?,?,?,NOW()) ON CONFLICT (name) DO NOTHING");
$stmt->execute($d);
}
// Run due tasks
$due = $pdo->query("SELECT * FROM admin.wedroid_schedule WHERE enabled=true AND (next_run IS NULL OR next_run <= NOW())")->fetchAll(PDO::FETCH_ASSOC);
$results = [];
foreach ($due as $task) {
$b64 = base64_encode($task['cmd']);
$ch = curl_init("https://weval-consulting.com/api/droid");
curl_setopt_array($ch, [CURLOPT_POST=>true, CURLOPT_RETURNTRANSFER=>true, CURLOPT_TIMEOUT=>30,
CURLOPT_POSTFIELDS=>"k=DROID2026&c=$b64"]);
$r = curl_exec($ch); curl_close($ch);
$d = @json_decode($r, true);
$output = substr($d['output'] ?? '', 0, 500);
$pdo->prepare("UPDATE admin.wedroid_schedule SET last_run=NOW(), next_run=NOW() + (interval_min || ' minutes')::interval, last_result=? WHERE id=?")
->execute([$output, $task['id']]);
$results[] = ['name'=>$task['name'], 'output'=>$output, 'status'=>($d['ok']??false)?'ok':'error'];
}
return ['ok'=>true, 'tasks_run'=>count($results), 'results'=>$results];
}