mirror of
https://github.com/hanzoai/runtime.git
synced 2026-10-30 18:23:13 +00:00
- Add main Dockerfile for production builds - Add Dockerfile.runtime for secure code execution - Add Dockerfile.runtime.fixed with improved configuration - Include .dockerignore for build optimization - Add integration documentation and publishing guides - Update API and CLI components for better runtime support - Enhance sandbox and container management features
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
// Copyright 2025 Hanzo Industries Inc.
|
|
// SPDX-License-Identifier: AGPL-3.0
|
|
|
|
package docker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hanzoai/runner/pkg/models/enums"
|
|
"github.com/docker/docker/api/types/container"
|
|
)
|
|
|
|
func (d *DockerClient) Stop(ctx context.Context, containerId string) error {
|
|
d.cache.SetSandboxState(ctx, containerId, enums.SandboxStateStopping)
|
|
|
|
err := d.apiClient.ContainerStop(ctx, containerId, container.StopOptions{
|
|
Signal: "SIGKILL",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = d.waitForContainerStopped(ctx, containerId, 10*time.Second)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.cache.SetSandboxState(ctx, containerId, enums.SandboxStateStopped)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *DockerClient) waitForContainerStopped(ctx context.Context, containerId string, timeout time.Duration) error {
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-timeoutCtx.Done():
|
|
return fmt.Errorf("timeout waiting for container %s to stop", containerId)
|
|
case <-ticker.C:
|
|
c, err := d.ContainerInspect(ctx, containerId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !c.State.Running {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
}
|