feat(scripts): run script executions in background and poll for completion

Community scripts (build.func) install packages like Java/OpenSearch and
can take minutes, well past the nginx/proxy HTTP timeout. execute_script
now returns immediately with status "running" and finishes the job on a
background thread with its own DB session; the frontend polls
/api/scripts/executions/{id} every 3s while running.

Also set TERM=xterm for script execution over SSH — community scripts
call `clear` and print colored output, which fails with "TERM
environment variable not set" when run without a PTY.
This commit is contained in:
markmorado
2026-07-23 21:59:18 +05:00
parent 761bd7bca8
commit 0b0b11c689
4 changed files with 86 additions and 19 deletions

View File

@@ -9,7 +9,7 @@ import { Label } from '@/components/ui/label';
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from '@/components/ui/select';
import { useServers, useNodes } from '@/hooks/use-nodes';
import { useVirtualMachines } from '@/hooks/use-instances';
import { useExecuteScript } from '@/hooks/use-scripts';
import { useExecuteScript, useScriptExecution } from '@/hooks/use-scripts';
import type { ScriptCatalogItem } from '@/types/scripts';
import type { ScriptExecution } from '@/types/scripts';
@@ -29,7 +29,13 @@ export default function ScriptExecuteDialog({ script, onOpenChange }: Props) {
const [node, setNode] = useState('');
const [guestKey, setGuestKey] = useState(''); // `${server_id}:${vmid}:${type}`
const [paramValues, setParamValues] = useState<Record<string, string>>({});
const [result, setResult] = useState<ScriptExecution | null>(null);
const [executionId, setExecutionId] = useState<number | null>(null);
// Выполнение фоновое: запуск возвращает execution со статусом `running`,
// дальше поллим его до завершения (см. useScriptExecution).
const { data: liveExec } = useScriptExecution(executionId ?? 0);
const result: ScriptExecution | null = liveExec ?? null;
const isRunning = result?.status === 'running';
const serverIdNum = serverId ? Number(serverId) : 0;
const { data: nodesData } = useNodes(serverIdNum || 0);
@@ -42,7 +48,7 @@ export default function ScriptExecuteDialog({ script, onOpenChange }: Props) {
useEffect(() => {
if (!open) return;
setServerId(''); setNode(''); setGuestKey(''); setResult(null);
setServerId(''); setNode(''); setGuestKey(''); setExecutionId(null);
const defaults: Record<string, string> = {};
for (const p of script?.params_schema || []) {
defaults[p.name] = p.default != null ? String(p.default) : '';

View File

@@ -146,5 +146,7 @@ export function useScriptExecution(id: number) {
queryKey: scriptKeys.execution(id),
queryFn: () => apiClient.get<ScriptExecution>(`/api/scripts/executions/${id}`),
enabled: id > 0,
// Выполнение фоновое: пока статус `running`, поллим каждые 3с до завершения.
refetchInterval: (query) => (query.state.data?.status === 'running' ? 3000 : false),
});
}