7aa7790931
Process spawn/wait/kill functions for subprocess management with output capture, timeout, and process group cleanup. DagRun and DagStepResult types for SQLite execution persistence. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
50 lines
1.8 KiB
Markdown
50 lines
1.8 KiB
Markdown
---
|
|
name: process_wait
|
|
kind: function
|
|
lang: go
|
|
domain: infra
|
|
version: "1.0.0"
|
|
purity: impure
|
|
signature: "func ProcessWait(handle *ProcessHandle, timeoutSec int) (ProcessResult, error)"
|
|
description: "Espera a que un subproceso termine y recopila su salida. Lee stdout y stderr completos en goroutines para evitar deadlocks en pipes. Si timeoutSec > 0 y el proceso no termina en ese tiempo, llama a ProcessKill y marca el resultado con Killed=true. Retorna el exit code, salida completa y duracion total."
|
|
tags: [process, subprocess, wait, timeout, exec, infra]
|
|
uses_functions: [process_kill_go_infra]
|
|
uses_types: [process_handle_go_infra, process_result_go_infra]
|
|
returns: [process_result_go_infra]
|
|
returns_optional: false
|
|
error_type: "error_go_core"
|
|
imports: [fmt, io, time]
|
|
params:
|
|
- name: handle
|
|
desc: "handle del proceso lanzado por ProcessSpawn"
|
|
- name: timeoutSec
|
|
desc: "segundos maximos de espera; 0 o negativo espera indefinidamente"
|
|
output: "resultado con exit code, stdout, stderr, duracion en ms y flag de killed"
|
|
tested: true
|
|
tests:
|
|
- "spawn and wait echo"
|
|
- "spawn with timeout kills"
|
|
- "spawn with env"
|
|
- "spawn script"
|
|
- "spawn with working dir"
|
|
test_file_path: "functions/infra/process_spawn_test.go"
|
|
file_path: "functions/infra/process_wait.go"
|
|
---
|
|
|
|
## Ejemplo
|
|
|
|
```go
|
|
h, err := ProcessSpawn("sleep 60", "", nil, "")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
res, err := ProcessWait(h, 5) // timeout de 5 segundos
|
|
if res.Killed {
|
|
fmt.Println("proceso terminado por timeout")
|
|
}
|
|
```
|
|
|
|
## Notas
|
|
|
|
Funcion impura: bloquea esperando I/O y posiblemente llama a ProcessKill. Lee stdout y stderr en goroutines separadas antes de llamar a cmd.Wait() para evitar el deadlock clasico donde cmd.Wait() bloquea porque los pipes estan llenos y nadie los lee. El exit code -1 indica que ProcessState no estaba disponible (proceso matado antes de registrar estado).
|