mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
fix(sandbox): start recovered Docker workers before exec
This commit is contained in:
parent
8f91643b78
commit
c93052f03f
2 changed files with 107 additions and 7 deletions
|
|
@ -468,13 +468,13 @@ func (b *DockerBackend) ensureTask(ctx context.Context, taskID string, scopeDoma
|
|||
b.removeContainer(ctx, tc.proxyID)
|
||||
tc.proxyID = ""
|
||||
}
|
||||
workerOK := tc.workerID != "" && b.startContainer(ctx, tc.workerID) == nil
|
||||
if !workerOK && tc.workerID != "" {
|
||||
b.removeContainer(ctx, tc.workerID)
|
||||
tc.workerID = ""
|
||||
}
|
||||
if proxyOK && workerOK {
|
||||
return tc, nil
|
||||
if proxyOK {
|
||||
if err := b.ensureWorkerStarted(ctx, tc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tc.workerID != "" {
|
||||
return tc, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -547,6 +547,13 @@ func (b *DockerBackend) ensureTask(ctx context.Context, taskID string, scopeDoma
|
|||
tc.scopeHash = sh
|
||||
}
|
||||
|
||||
// Recover does not know the proxy's scope. After reconciling the proxy,
|
||||
// start any tracked worker before reusing it, including workers found stopped
|
||||
// after a Docker restart.
|
||||
if err := b.ensureWorkerStarted(ctx, tc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create worker container strictly matching CONTRACT.md §4.
|
||||
if tc.workerID == "" {
|
||||
b.removeContainer(ctx, workerName)
|
||||
|
|
@ -605,6 +612,21 @@ func (b *DockerBackend) ensureTask(ctx context.Context, taskID string, scopeDoma
|
|||
return tc, nil
|
||||
}
|
||||
|
||||
// ensureWorkerStarted runs under the task lifecycle lock. Clear a stale ID only
|
||||
// after Docker confirms removal or absence so cleanup can be retried on failure.
|
||||
func (b *DockerBackend) ensureWorkerStarted(ctx context.Context, tc *taskContainers) error {
|
||||
if tc.workerID == "" {
|
||||
return nil
|
||||
}
|
||||
if startErr := b.startContainer(ctx, tc.workerID); startErr != nil {
|
||||
if removeErr := b.removeContainer(ctx, tc.workerID); removeErr != nil {
|
||||
return errors.Join(startErr, removeErr)
|
||||
}
|
||||
tc.workerID = ""
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exec runs req.Argv directly inside the task's worker container via /usr/local/bin/artex-exec-wrap
|
||||
// (zero shell interpolation, isolated process group, SIGTERM -> 5s -> SIGKILL on timeout).
|
||||
func (b *DockerBackend) Exec(ctx context.Context, taskID string, req ExecRequest) (ExecResponse, error) {
|
||||
|
|
|
|||
78
sandbox/docker_recovery_test.go
Normal file
78
sandbox/docker_recovery_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package sandbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDockerRecoveredWorkerStartsAfterProxyReconciliation(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
root := t.TempDir()
|
||||
b := NewDockerBackend(DockerConfig{WorkspaceRoot: root, AuditRoot: root})
|
||||
labels := func(component string) map[string]string {
|
||||
return map[string]string{LabelManagedBy: ManagedByRunner, LabelTask: "t1", LabelComponent: component}
|
||||
}
|
||||
var proxyStarted, workerStarted atomic.Bool
|
||||
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "GET /containers/json":
|
||||
_ = json.NewEncoder(w).Encode([]any{
|
||||
map[string]any{"Id": "old-proxy", "Labels": labels(ComponentProxy), "State": "exited"},
|
||||
map[string]any{"Id": "old-worker", "Labels": labels(ComponentWorker), "State": "exited"},
|
||||
})
|
||||
case "GET /networks/artex-task-t1":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"Internal": true, "Labels": labels(ComponentWorker)})
|
||||
case "GET /networks/egress-net":
|
||||
_, _ = w.Write([]byte("{}"))
|
||||
case "DELETE /containers/artex-egress-t1", "POST /networks/egress-net/connect":
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case "POST /containers/create":
|
||||
if name := r.URL.Query().Get("name"); name != "artex-egress-t1" {
|
||||
t.Errorf("unexpected container creation: %s", name)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"Id": "new-proxy"})
|
||||
case "POST /containers/new-proxy/start":
|
||||
proxyStarted.Store(true)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case "POST /containers/old-worker/start":
|
||||
if !proxyStarted.Load() {
|
||||
t.Error("worker started before the proxy was reconciled")
|
||||
}
|
||||
workerStarted.Store(true)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case "POST /containers/old-worker/exec":
|
||||
if !workerStarted.Load() {
|
||||
http.Error(w, "worker is not running", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"Id": "exec-1"})
|
||||
case "POST /exec/exec-1/start":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
case "GET /exec/exec-1/json":
|
||||
_, _ = w.Write([]byte("{\"ExitCode\":0,\"Running\":false}"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(fake.Close)
|
||||
transport := b.http.Transport.(*http.Transport)
|
||||
transport.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
var d net.Dialer
|
||||
return d.DialContext(ctx, "tcp", fake.Listener.Addr().String())
|
||||
}
|
||||
t.Cleanup(transport.CloseIdleConnections)
|
||||
if n, err := b.Recover(ctx); err != nil || n != 1 {
|
||||
t.Fatalf("Recover = %d, %v, want 1 task", n, err)
|
||||
}
|
||||
res, err := b.Exec(ctx, "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 1000})
|
||||
if err != nil || res.ExitCode != 0 {
|
||||
t.Fatalf("Exec after recovery = %+v, %v", res, err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue