-
This commit is contained in:
@ -83,12 +83,6 @@ func NewSimpleDockerRuntime(logger *zap.Logger) (*SimpleDockerRuntime, error) {
|
||||
func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.FunctionDefinition, input json.RawMessage) (*domain.ExecutionResult, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
s.logger.Info("🐳 TIMEOUT DEBUG: Executing function in Docker container",
|
||||
zap.String("function_id", function.ID.String()),
|
||||
zap.String("name", function.Name),
|
||||
zap.String("image", function.Image),
|
||||
zap.Duration("timeout", function.Timeout.Duration))
|
||||
|
||||
// Create container
|
||||
containerID, err := s.createContainer(ctx, function, input)
|
||||
if err != nil {
|
||||
@ -105,20 +99,12 @@ func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.Func
|
||||
var timeoutCtx context.Context
|
||||
var cancel context.CancelFunc
|
||||
if function.Timeout.Duration > 0 {
|
||||
s.logger.Info("⏰ TIMEOUT DEBUG: Creating timeout context for function execution",
|
||||
zap.Duration("timeout", function.Timeout.Duration),
|
||||
zap.String("container_id", containerID))
|
||||
timeoutCtx, cancel = context.WithTimeout(ctx, function.Timeout.Duration)
|
||||
defer cancel()
|
||||
} else {
|
||||
s.logger.Info("❌ TIMEOUT DEBUG: No timeout specified, using original context",
|
||||
zap.String("container_id", containerID))
|
||||
timeoutCtx = ctx
|
||||
}
|
||||
|
||||
s.logger.Debug("Starting container wait with timeout",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Duration("timeout", function.Timeout.Duration))
|
||||
|
||||
// Wait for container to finish with timeout
|
||||
statusCh, errCh := s.client.ContainerWait(timeoutCtx, containerID, container.WaitConditionNotRunning)
|
||||
@ -126,28 +112,16 @@ func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.Func
|
||||
var timedOut bool
|
||||
select {
|
||||
case err := <-errCh:
|
||||
s.logger.Debug("Container wait returned error",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Error(err))
|
||||
s.cleanupContainer(ctx, containerID)
|
||||
return nil, fmt.Errorf("error waiting for container: %w", err)
|
||||
case status := <-statusCh:
|
||||
s.logger.Debug("Container finished normally",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Int64("exit_code", status.StatusCode))
|
||||
case <-statusCh:
|
||||
// Container finished normally
|
||||
case <-timeoutCtx.Done():
|
||||
// Timeout occurred
|
||||
timedOut = true
|
||||
s.logger.Info("💥 TIMEOUT DEBUG: Function execution timed out, stopping container",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Duration("timeout", function.Timeout.Duration),
|
||||
zap.Error(timeoutCtx.Err()))
|
||||
|
||||
// Stop the container in the background - don't wait for it to complete
|
||||
go func() {
|
||||
s.logger.Debug("Attempting to stop timed out container in background",
|
||||
zap.String("container_id", containerID))
|
||||
// Use a very short timeout for stopping, then kill if needed
|
||||
if err := s.client.ContainerStop(context.Background(), containerID, container.StopOptions{
|
||||
Timeout: &[]int{1}[0], // Only 1 second grace period for stop
|
||||
@ -160,13 +134,7 @@ func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.Func
|
||||
s.logger.Error("Failed to kill timed out container",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Error(killErr))
|
||||
} else {
|
||||
s.logger.Debug("Successfully killed timed out container",
|
||||
zap.String("container_id", containerID))
|
||||
}
|
||||
} else {
|
||||
s.logger.Debug("Successfully stopped timed out container",
|
||||
zap.String("container_id", containerID))
|
||||
}
|
||||
}()
|
||||
}
|
||||
@ -176,8 +144,6 @@ func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.Func
|
||||
|
||||
// For timed-out containers, skip log retrieval and inspection to return quickly
|
||||
if timedOut {
|
||||
s.logger.Debug("Skipping log retrieval and inspection for timed-out container",
|
||||
zap.String("container_id", containerID))
|
||||
logs = []string{"Container execution timed out"}
|
||||
} else {
|
||||
// Get container logs
|
||||
@ -205,19 +171,11 @@ func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.Func
|
||||
|
||||
// Handle timeout case
|
||||
if timedOut {
|
||||
s.logger.Debug("Processing timeout result",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Duration("timeout", function.Timeout.Duration))
|
||||
result.Error = fmt.Sprintf("Function execution timed out after %v", function.Timeout.Duration)
|
||||
result.Output = json.RawMessage(`{"error": "Function execution timed out"}`)
|
||||
} else {
|
||||
s.logger.Debug("Processing normal execution result",
|
||||
zap.String("container_id", containerID))
|
||||
// Try to get output from container for successful executions
|
||||
if stats.State != nil {
|
||||
s.logger.Debug("Container state available",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Int("exit_code", stats.State.ExitCode))
|
||||
if stats.State.ExitCode == 0 {
|
||||
// Try to get output from container
|
||||
output, err := s.getContainerOutput(ctx, containerID)
|
||||
@ -225,28 +183,19 @@ func (s *SimpleDockerRuntime) Execute(ctx context.Context, function *domain.Func
|
||||
s.logger.Warn("Failed to get container output", zap.Error(err))
|
||||
result.Output = json.RawMessage(`{"error": "Failed to retrieve output"}`)
|
||||
} else {
|
||||
s.logger.Debug("Successfully retrieved container output",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Int("output_size", len(output)))
|
||||
result.Output = output
|
||||
}
|
||||
} else {
|
||||
s.logger.Debug("Container exited with non-zero code",
|
||||
zap.String("container_id", containerID),
|
||||
zap.Int("exit_code", stats.State.ExitCode))
|
||||
result.Error = fmt.Sprintf("Container exited with code %d", stats.State.ExitCode)
|
||||
result.Output = json.RawMessage(`{"error": "Container execution failed"}`)
|
||||
}
|
||||
} else {
|
||||
s.logger.Warn("Container state not available",
|
||||
zap.String("container_id", containerID))
|
||||
s.logger.Warn("Container state not available")
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup container - for timed-out containers, do this in background
|
||||
if timedOut {
|
||||
s.logger.Debug("Scheduling background cleanup for timed-out container",
|
||||
zap.String("container_id", containerID))
|
||||
go func() {
|
||||
s.cleanupContainer(context.Background(), containerID)
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user