Refactor error handling and improve code formatting in runners.go. Replace fmt.Errorf with errors.New for better error management. Clean up whitespace and enhance readability in various API response structures.

This commit is contained in:
2025-11-24 21:49:17 -06:00
parent 4ac05d50a1
commit 3217bbfe4d

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"log"
@@ -295,10 +296,10 @@ func (s *Server) handleUpdateTaskStep(w http.ResponseWriter, r *http.Request) {
if err == nil {
// Broadcast step update to frontend
s.broadcastTaskUpdate(jobID, taskID, "step_update", map[string]interface{}{
"step_id": stepID,
"step_name": req.StepName,
"status": req.Status,
"duration_ms": req.DurationMs,
"step_id": stepID,
"step_name": req.StepName,
"status": req.Status,
"duration_ms": req.DurationMs,
"error_message": req.ErrorMessage,
})
}
@@ -338,7 +339,7 @@ func (s *Server) handleDownloadJobContext(w http.ResponseWriter, r *http.Request
// Set appropriate headers for tar file
w.Header().Set("Content-Type", "application/x-tar")
w.Header().Set("Content-Disposition", "attachment; filename=context.tar")
// Stream the file to the response
io.Copy(w, file)
}
@@ -1153,8 +1154,8 @@ func (s *Server) updateJobStatusFromTasks(jobID int64) {
}
// Broadcast job update via WebSocket
s.broadcastJobUpdate(jobID, "job_update", map[string]interface{}{
"status": jobStatus,
"progress": progress,
"status": jobStatus,
"progress": progress,
"completed_at": now,
})
}
@@ -1184,7 +1185,7 @@ func (s *Server) updateJobStatusFromTasks(jobID int64) {
// Broadcast that a new task was added
log.Printf("Broadcasting task_added for job %d: video generation task %d", jobID, videoTaskID)
s.broadcastTaskUpdate(jobID, videoTaskID, "task_added", map[string]interface{}{
"task_id": videoTaskID,
"task_id": videoTaskID,
"task_type": types.TaskTypeVideoGeneration,
})
// Update job status to ensure it's marked as running (has pending video task)
@@ -1790,7 +1791,7 @@ func (s *Server) assignTaskToRunner(runnerID int64, taskID int64) error {
types.TaskStatusPending, taskID,
)
s.logTaskEvent(taskID, nil, types.LogLevelError, errMsg, "")
return fmt.Errorf(errMsg)
return errors.New(errMsg)
}
// Note: Task is already assigned in database by the atomic update in distributeTasksToRunners