Batch create AI jobs efficiently
All checks were successful
CI / test (push) Successful in 22s
Build and Deploy / build-and-deploy (push) Successful in 25s

This commit is contained in:
Grendgi
2026-06-09 11:49:12 +03:00
parent 6254bf0810
commit da144ecefe
2 changed files with 92 additions and 26 deletions

View File

@@ -135,6 +135,8 @@ type createBatchResponse struct {
Jobs []*model.Job `json:"jobs"`
}
const maxCreateBatchJobs = 1000
func (s *Server) handleCreateBatch(w http.ResponseWriter, r *http.Request) {
var req createBatchRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -145,17 +147,21 @@ func (s *Server) handleCreateBatch(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "jobs is required")
return
}
if len(req.Jobs) > maxCreateBatchJobs {
writeError(w, http.StatusBadRequest, fmt.Sprintf("jobs limit is %d", maxCreateBatchJobs))
return
}
ctx, cancel := contextWithTimeout(r, 20*time.Second)
defer cancel()
out := createBatchResponse{Jobs: make([]*model.Job, 0, len(req.Jobs))}
items := make([]model.CreateJob, 0, len(req.Jobs))
for _, item := range req.Jobs {
if item.OwnerService == "" {
if strings.TrimSpace(item.OwnerService) == "" {
item.OwnerService = req.OwnerService
}
if item.TaskType == "" {
if strings.TrimSpace(item.TaskType) == "" {
item.TaskType = req.TaskType
}
if item.ModelProfile == "" {
if strings.TrimSpace(item.ModelProfile) == "" {
item.ModelProfile = req.ModelProfile
}
if item.Priority == 0 {
@@ -164,18 +170,18 @@ func (s *Server) handleCreateBatch(w http.ResponseWriter, r *http.Request) {
if item.MaxAttempts == 0 {
item.MaxAttempts = req.MaxAttempts
}
job, err := s.store.CreateJob(ctx, item)
if err != nil {
status := http.StatusInternalServerError
if isValidationError(err) {
status = http.StatusBadRequest
}
writeError(w, status, err.Error())
return
}
out.Jobs = append(out.Jobs, job)
items = append(items, item)
}
writeJSON(w, http.StatusCreated, out)
jobs, err := s.store.CreateJobs(ctx, items)
if err != nil {
status := http.StatusInternalServerError
if isValidationError(err) {
status = http.StatusBadRequest
}
writeError(w, status, err.Error())
return
}
writeJSON(w, http.StatusCreated, createBatchResponse{Jobs: jobs})
}
func (s *Server) handleRetryJobs(w http.ResponseWriter, r *http.Request) {