Faas semi worfking

This commit is contained in:
2025-08-30 23:52:37 -04:00
parent 2778cbc512
commit 67bce24899
23 changed files with 1089 additions and 135 deletions

View File

@ -2,6 +2,7 @@ package services
import (
"context"
"encoding/json"
"fmt"
"time"
@ -34,7 +35,7 @@ func NewExecutionService(
}
func (s *executionService) Execute(ctx context.Context, req *domain.ExecuteFunctionRequest, userID string) (*domain.ExecuteFunctionResponse, error) {
s.logger.Info("Executing function",
s.logger.Info("Executing function",
zap.String("function_id", req.FunctionID.String()),
zap.String("user_id", userID),
zap.Bool("async", req.Async))
@ -45,20 +46,27 @@ func (s *executionService) Execute(ctx context.Context, req *domain.ExecuteFunct
return nil, fmt.Errorf("function not found: %w", err)
}
// Create execution record
// Create execution record
// Initialize input with empty JSON if nil or empty
input := req.Input
if input == nil || len(input) == 0 {
input = json.RawMessage(`{}`)
}
execution := &domain.FunctionExecution{
ID: uuid.New(),
FunctionID: req.FunctionID,
Status: domain.StatusPending,
Input: req.Input,
ExecutorID: userID,
CreatedAt: time.Now(),
ID: uuid.New(),
FunctionID: req.FunctionID,
Status: domain.StatusPending,
Input: input,
Output: json.RawMessage(`{}`), // Initialize with empty JSON object
ExecutorID: userID,
CreatedAt: time.Now(),
}
// Store execution
createdExecution, err := s.executionRepo.Create(ctx, execution)
if err != nil {
s.logger.Error("Failed to create execution record",
s.logger.Error("Failed to create execution record",
zap.String("function_id", req.FunctionID.String()),
zap.Error(err))
return nil, fmt.Errorf("failed to create execution record: %w", err)
@ -67,7 +75,7 @@ func (s *executionService) Execute(ctx context.Context, req *domain.ExecuteFunct
if req.Async {
// Start async execution
go s.executeAsync(context.Background(), createdExecution, function)
return &domain.ExecuteFunctionResponse{
ExecutionID: createdExecution.ID,
Status: domain.StatusPending,
@ -82,7 +90,7 @@ func (s *executionService) executeSync(ctx context.Context, execution *domain.Fu
// Update status to running
execution.Status = domain.StatusRunning
execution.StartedAt = &[]time.Time{time.Now()}[0]
if _, err := s.executionRepo.Update(ctx, execution.ID, execution); err != nil {
s.logger.Warn("Failed to update execution status to running", zap.Error(err))
}
@ -115,7 +123,12 @@ func (s *executionService) executeSync(ctx context.Context, execution *domain.Fu
// Update execution with results
execution.Status = domain.StatusCompleted
execution.Output = result.Output
// Handle empty output
if len(result.Output) == 0 {
execution.Output = json.RawMessage(`{}`)
} else {
execution.Output = result.Output
}
execution.Error = result.Error
execution.Duration = result.Duration
execution.MemoryUsed = result.MemoryUsed
@ -139,7 +152,7 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
// Update status to running
execution.Status = domain.StatusRunning
execution.StartedAt = &[]time.Time{time.Now()}[0]
if _, err := s.executionRepo.Update(ctx, execution.ID, execution); err != nil {
s.logger.Warn("Failed to update execution status to running", zap.Error(err))
}
@ -147,7 +160,7 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
// Get runtime backend
backend, err := s.runtimeService.GetBackend(ctx, string(function.Runtime))
if err != nil {
s.logger.Error("Failed to get runtime backend for async execution",
s.logger.Error("Failed to get runtime backend for async execution",
zap.String("execution_id", execution.ID.String()),
zap.Error(err))
execution.Status = domain.StatusFailed
@ -159,7 +172,7 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
// Execute function
result, err := backend.Execute(ctx, function, execution.Input)
if err != nil {
s.logger.Error("Async function execution failed",
s.logger.Error("Async function execution failed",
zap.String("execution_id", execution.ID.String()),
zap.Error(err))
execution.Status = domain.StatusFailed
@ -170,7 +183,12 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
// Update execution with results
execution.Status = domain.StatusCompleted
execution.Output = result.Output
// Handle empty output
if len(result.Output) == 0 {
execution.Output = json.RawMessage(`{}`)
} else {
execution.Output = result.Output
}
execution.Error = result.Error
execution.Duration = result.Duration
execution.MemoryUsed = result.MemoryUsed
@ -181,7 +199,7 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
s.updateExecutionComplete(ctx, execution)
s.logger.Info("Async function execution completed",
s.logger.Info("Async function execution completed",
zap.String("execution_id", execution.ID.String()),
zap.String("status", string(execution.Status)),
zap.Duration("duration", execution.Duration))
@ -189,9 +207,9 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
func (s *executionService) updateExecutionComplete(ctx context.Context, execution *domain.FunctionExecution) {
execution.CompletedAt = &[]time.Time{time.Now()}[0]
if _, err := s.executionRepo.Update(ctx, execution.ID, execution); err != nil {
s.logger.Error("Failed to update execution completion",
s.logger.Error("Failed to update execution completion",
zap.String("execution_id", execution.ID.String()),
zap.Error(err))
}
@ -228,7 +246,7 @@ func (s *executionService) GetByFunctionID(ctx context.Context, functionID uuid.
}
func (s *executionService) Cancel(ctx context.Context, id uuid.UUID, userID string) error {
s.logger.Info("Canceling execution",
s.logger.Info("Canceling execution",
zap.String("execution_id", id.String()),
zap.String("user_id", userID))
@ -256,7 +274,7 @@ func (s *executionService) Cancel(ctx context.Context, id uuid.UUID, userID stri
}
if err := backend.StopExecution(ctx, id); err != nil {
s.logger.Warn("Failed to stop execution in runtime",
s.logger.Warn("Failed to stop execution in runtime",
zap.String("execution_id", id.String()),
zap.Error(err))
}
@ -270,7 +288,7 @@ func (s *executionService) Cancel(ctx context.Context, id uuid.UUID, userID stri
return fmt.Errorf("failed to update execution status: %w", err)
}
s.logger.Info("Execution canceled successfully",
s.logger.Info("Execution canceled successfully",
zap.String("execution_id", id.String()))
return nil
@ -306,4 +324,4 @@ func (s *executionService) GetLogs(ctx context.Context, id uuid.UUID) ([]string,
func (s *executionService) GetRunningExecutions(ctx context.Context) ([]*domain.FunctionExecution, error) {
return s.executionRepo.GetRunningExecutions(ctx)
}
}

View File

@ -48,7 +48,11 @@ func NewRuntimeService(logger *zap.Logger, config *RuntimeConfig) RuntimeService
func (s *runtimeService) initializeDockerBackend() error {
// Use simple Docker backend for now
dockerBackend := docker.NewSimpleDockerRuntime(s.logger)
dockerBackend, err := docker.NewSimpleDockerRuntime(s.logger)
if err != nil {
s.logger.Error("Failed to create Docker runtime", zap.Error(err))
return err
}
s.mutex.Lock()
s.backends["docker"] = dockerBackend
@ -72,7 +76,7 @@ func (s *runtimeService) GetBackend(ctx context.Context, runtimeType string) (ru
// Check backend health
if err := backend.HealthCheck(ctx); err != nil {
s.logger.Warn("Runtime backend health check failed",
s.logger.Warn("Runtime backend health check failed",
zap.String("backend", backendType),
zap.Error(err))
return nil, fmt.Errorf("runtime backend '%s' is not healthy: %w", backendType, err)
@ -191,4 +195,4 @@ func (s *runtimeService) isRuntimeAvailable(ctx context.Context, runtimeType str
}
return true
}
}