This commit is contained in:
2025-08-31 17:01:07 -04:00
parent 66b114f374
commit e3e6a4460b
11 changed files with 600 additions and 101 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/RyanCopley/skybridge/faas/internal/domain"
"github.com/RyanCopley/skybridge/faas/internal/repository"
"github.com/RyanCopley/skybridge/faas/internal/runtime"
"github.com/google/uuid"
)
@ -42,13 +43,13 @@ 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,
@ -112,8 +113,53 @@ func (s *executionService) executeSync(ctx context.Context, execution *domain.Fu
defer cancel()
}
// Execute function
result, err := backend.Execute(execCtx, function, execution.Input)
// Define log streaming callback
logCallback := func(logs []string) error {
s.logger.Info("Log streaming callback called",
zap.String("execution_id", execution.ID.String()),
zap.Int("log_count", len(logs)),
zap.Strings("logs_preview", logs))
// Update execution with current logs using background context
// to ensure updates continue even after HTTP request completes
// Create a copy of the execution to avoid race conditions
execCopy := *execution
execCopy.Logs = logs
_, err := s.executionRepo.Update(context.Background(), execution.ID, &execCopy)
if err == nil {
// Only update the original if database update succeeds
execution.Logs = logs
s.logger.Info("Successfully updated execution with logs in database",
zap.String("execution_id", execution.ID.String()),
zap.Int("log_count", len(logs)))
} else {
s.logger.Error("Failed to update execution with logs in database",
zap.String("execution_id", execution.ID.String()),
zap.Error(err))
}
return err
}
// Check if backend supports log streaming
type logStreamingBackend interface {
ExecuteWithLogStreaming(ctx context.Context, function *domain.FunctionDefinition, input json.RawMessage, logCallback runtime.LogStreamCallback) (*domain.ExecutionResult, error)
}
var result *domain.ExecutionResult
if lsBackend, ok := backend.(logStreamingBackend); ok {
s.logger.Info("Backend supports log streaming, using ExecuteWithLogStreaming",
zap.String("execution_id", execution.ID.String()),
zap.String("function_id", function.ID.String()))
// Execute function with log streaming
result, err = lsBackend.ExecuteWithLogStreaming(execCtx, function, execution.Input, logCallback)
} else {
s.logger.Info("Backend does not support log streaming, using regular Execute",
zap.String("execution_id", execution.ID.String()),
zap.String("function_id", function.ID.String()))
// Fallback to regular execute
result, err = backend.Execute(execCtx, function, execution.Input)
}
if err != nil {
// Check if this was a timeout error
if execCtx.Err() == context.DeadlineExceeded {
@ -194,8 +240,53 @@ func (s *executionService) executeAsync(ctx context.Context, execution *domain.F
defer cancel()
}
// Execute function
result, err := backend.Execute(execCtx, function, execution.Input)
// Define log streaming callback
logCallback := func(logs []string) error {
s.logger.Info("Log streaming callback called",
zap.String("execution_id", execution.ID.String()),
zap.Int("log_count", len(logs)),
zap.Strings("logs_preview", logs))
// Update execution with current logs using background context
// to ensure updates continue even after HTTP request completes
// Create a copy of the execution to avoid race conditions
execCopy := *execution
execCopy.Logs = logs
_, err := s.executionRepo.Update(context.Background(), execution.ID, &execCopy)
if err == nil {
// Only update the original if database update succeeds
execution.Logs = logs
s.logger.Info("Successfully updated execution with logs in database",
zap.String("execution_id", execution.ID.String()),
zap.Int("log_count", len(logs)))
} else {
s.logger.Error("Failed to update execution with logs in database",
zap.String("execution_id", execution.ID.String()),
zap.Error(err))
}
return err
}
// Check if backend supports log streaming
type logStreamingBackend interface {
ExecuteWithLogStreaming(ctx context.Context, function *domain.FunctionDefinition, input json.RawMessage, logCallback runtime.LogStreamCallback) (*domain.ExecutionResult, error)
}
var result *domain.ExecutionResult
if lsBackend, ok := backend.(logStreamingBackend); ok {
s.logger.Info("Backend supports log streaming, using ExecuteWithLogStreaming",
zap.String("execution_id", execution.ID.String()),
zap.String("function_id", function.ID.String()))
// Execute function with log streaming
result, err = lsBackend.ExecuteWithLogStreaming(execCtx, function, execution.Input, logCallback)
} else {
s.logger.Info("Backend does not support log streaming, using regular Execute",
zap.String("execution_id", execution.ID.String()),
zap.String("function_id", function.ID.String()))
// Fallback to regular execute
result, err = backend.Execute(execCtx, function, execution.Input)
}
if err != nil {
// Check if this was a timeout error
if execCtx.Err() == context.DeadlineExceeded {
@ -329,17 +420,35 @@ func (s *executionService) Cancel(ctx context.Context, id uuid.UUID, userID stri
}
func (s *executionService) GetLogs(ctx context.Context, id uuid.UUID) ([]string, error) {
s.logger.Debug("GetLogs called in execution service",
zap.String("execution_id", id.String()))
// Get execution with logs from database
execution, err := s.executionRepo.GetByID(ctx, id)
if err != nil {
s.logger.Error("Failed to get execution from database in GetLogs",
zap.String("execution_id", id.String()),
zap.Error(err))
return nil, fmt.Errorf("execution not found: %w", err)
}
s.logger.Info("Retrieved execution from database",
zap.String("execution_id", id.String()),
zap.String("status", string(execution.Status)),
zap.Int("log_count", len(execution.Logs)),
zap.Bool("logs_nil", execution.Logs == nil))
// Return logs from execution record
if execution.Logs == nil {
s.logger.Debug("Execution has nil logs, returning empty slice",
zap.String("execution_id", id.String()))
return []string{}, nil
}
s.logger.Debug("Returning logs from execution",
zap.String("execution_id", id.String()),
zap.Int("log_count", len(execution.Logs)))
return execution.Logs, nil
}