104 lines
4.0 KiB
PHP
104 lines
4.0 KiB
PHP
<?php
|
|
// Blade Task Queue API — polled by sentinel-agent.ps1 every 60s
|
|
header("Content-Type: application/json");
|
|
$k = $_GET["k"] ?? $_POST["k"] ?? "";
|
|
if ($k !== "BLADE2026") { echo json_encode(["error"=>"auth"]); exit; }
|
|
|
|
$action = $_GET["action"] ?? $_POST["action"] ?? "poll";
|
|
$queue_dir = "/var/www/html/api/blade-tasks/";
|
|
@mkdir($queue_dir, 0777, true);
|
|
|
|
switch ($action) {
|
|
case "poll":
|
|
// Return pending tasks
|
|
$tasks = [];
|
|
foreach (glob($queue_dir . "*.json") as $f) {
|
|
$t = json_decode(file_get_contents($f), true);
|
|
if (($t["status"] ?? "") === "pending") {
|
|
$tasks[] = $t;
|
|
}
|
|
}
|
|
echo json_encode(["tasks" => $tasks, "count" => count($tasks)]);
|
|
break;
|
|
|
|
case "complete":
|
|
$id = $_POST["task_id"] ?? "";
|
|
$result = $_POST["result"] ?? "";
|
|
$f = $queue_dir . $id . ".json";
|
|
if (file_exists($f)) {
|
|
$t = json_decode(file_get_contents($f), true);
|
|
$t["status"] = "completed";
|
|
$t["result"] = $result;
|
|
$t["completed_at"] = date("c");
|
|
file_put_contents($f, json_encode($t, JSON_PRETTY_PRINT));
|
|
echo json_encode(["ok" => true]);
|
|
}
|
|
break;
|
|
|
|
|
|
case "flush":
|
|
$older_days = max(1, intval($_GET["older_days"] ?? $_POST["older_days"] ?? 7));
|
|
$cutoff = time() - ($older_days * 86400);
|
|
$archived = 0; $kept = 0;
|
|
$archive_dir = $queue_dir . "archived_" . date("Y-m-d") . "/";
|
|
@mkdir($archive_dir, 0777, true);
|
|
foreach (glob($queue_dir . "*.json") as $ff) {
|
|
$t = @json_decode(@file_get_contents($ff), true);
|
|
if (!$t) continue;
|
|
if (($t["status"] ?? "") === "done" || ($t["status"] ?? "") === "completed") {
|
|
$t_time = strtotime($t["completed_at"] ?? $t["created_at"] ?? "");
|
|
if ($t_time > 0 && $t_time < $cutoff) {
|
|
// MOVE to archive, not delete (doctrine 59 NO-DELETE)
|
|
@rename($ff, $archive_dir . basename($ff));
|
|
$archived++;
|
|
} else { $kept++; }
|
|
}
|
|
}
|
|
echo json_encode([
|
|
"ok" => true,
|
|
"action" => "flush",
|
|
"older_days" => $older_days,
|
|
"archived" => $archived,
|
|
"kept" => $kept,
|
|
"archive_dir" => $archive_dir,
|
|
"note" => "done tasks moved to archive (not deleted, doctrine 59)"
|
|
]);
|
|
break;
|
|
|
|
case "add":
|
|
$id = "task_" . date("YmdHis") . "_" . substr(md5(rand()), 0, 6);
|
|
$task = [
|
|
"id" => $id,
|
|
"name" => $_POST["name"] ?? $_GET["name"] ?? "Unnamed",
|
|
"type" => $_POST["type"] ?? $_GET["type"] ?? "powershell",
|
|
"command" => $_POST["cmd"] ?? $_POST["command"] ?? $_GET["cmd"] ?? "",
|
|
"cmd" => $_POST["cmd"] ?? $_POST["command"] ?? $_GET["cmd"] ?? "",
|
|
"priority" => $_POST["priority"] ?? "normal",
|
|
"status" => "pending",
|
|
"created" => date("c"),
|
|
"created_by" => "blade-control-ui"
|
|
];
|
|
if (!$task["command"]) {
|
|
echo json_encode(["error" => "cmd required"]);
|
|
break;
|
|
}
|
|
file_put_contents($queue_dir . $id . ".json", json_encode($task, JSON_PRETTY_PRINT));
|
|
echo json_encode(["ok" => true, "task_id" => $id, "task" => $task]);
|
|
break;
|
|
|
|
case "create":
|
|
$id = "task_" . date("YmdHis") . "_" . substr(md5(rand()), 0, 6);
|
|
$task = [
|
|
"id" => $id,
|
|
"goal" => $_POST["goal"] ?? "",
|
|
"type" => $_POST["type"] ?? "powershell",
|
|
"commands" => json_decode($_POST["commands"] ?? "[]", true) ?: [],
|
|
"priority" => $_POST["priority"] ?? "normal",
|
|
"status" => "pending",
|
|
"created_at" => date("c")
|
|
];
|
|
file_put_contents($queue_dir . $id . ".json", json_encode($task, JSON_PRETTY_PRINT));
|
|
echo json_encode(["ok" => true, "task_id" => $id]);
|
|
break;
|
|
}
|