feat: funciones infra — Docker, deploy, build y health check
Funciones impuras para gestión de contenedores: docker_build_image, docker_compose_up/down, docker_volume_create/list/remove, generate_dockerfile, write_dockerfile, go_build_binary, health_check_http, deploy_app y stop_app. Todas con tests unitarios donde aplica. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HealthCheckHTTP hace polling HTTP GET a url hasta recibir status 200
|
||||
// o hasta que pasen timeoutSecs segundos. Entre intentos espera intervalMs milisegundos.
|
||||
func HealthCheckHTTP(url string, timeoutSecs, intervalMs int) error {
|
||||
deadline := time.Now().Add(time.Duration(timeoutSecs) * time.Second)
|
||||
interval := time.Duration(intervalMs) * time.Millisecond
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: time.Duration(intervalMs)*time.Millisecond + 500*time.Millisecond,
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
for time.Now().Before(deadline) {
|
||||
resp, err := client.Get(url)
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
lastErr = fmt.Errorf("status %d", resp.StatusCode)
|
||||
} else {
|
||||
lastErr = err
|
||||
}
|
||||
time.Sleep(interval)
|
||||
}
|
||||
|
||||
if lastErr != nil {
|
||||
return fmt.Errorf("health check %s timeout after %ds: %w", url, timeoutSecs, lastErr)
|
||||
}
|
||||
return fmt.Errorf("health check %s timeout after %ds", url, timeoutSecs)
|
||||
}
|
||||
Reference in New Issue
Block a user