feat(generation): resume manual summary jobs
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tradarr/backend/internal/httputil"
|
||||
@ -22,27 +21,45 @@ func (h *Handler) ListSummaries(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) GenerateSummary(c *gin.Context) {
|
||||
if h.generationUseCase == nil {
|
||||
if h.generationJobs == nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "generation unavailable"})
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Hour)
|
||||
defer cancel()
|
||||
result, err := h.generationUseCase.Generate(ctx, c.GetString("userID"))
|
||||
job, err := h.generationJobs.Start(c.Request.Context(), c.GetString("userID"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
||||
return
|
||||
}
|
||||
switch result.Outcome.Status {
|
||||
case "succeeded", "partial":
|
||||
c.JSON(http.StatusCreated, gin.H{"data": result})
|
||||
case "no_results":
|
||||
httputil.OK(c, result)
|
||||
case "failed":
|
||||
c.JSON(http.StatusBadGateway, gin.H{"data": result})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid generation outcome"})
|
||||
c.Header("Location", "/api/summaries/generation-jobs/"+job.ID)
|
||||
c.JSON(http.StatusAccepted, gin.H{"data": job})
|
||||
}
|
||||
func (h *Handler) RecentGenerationJob(c *gin.Context) {
|
||||
job, err := h.repo.RecentGenerationJob(c.Request.Context(), c.GetString("userID"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
||||
return
|
||||
}
|
||||
httputil.OK(c, job)
|
||||
}
|
||||
|
||||
var generationJobUUID = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
|
||||
func (h *Handler) GetGenerationJob(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if !generationJobUUID.MatchString(id) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "generation not found"})
|
||||
return
|
||||
}
|
||||
job, err := h.repo.GetGenerationJob(c.Request.Context(), c.GetString("userID"), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
||||
return
|
||||
}
|
||||
if job == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "generation not found"})
|
||||
return
|
||||
}
|
||||
httputil.OK(c, job)
|
||||
}
|
||||
func (h *Handler) ListGenerations(c *gin.Context) {
|
||||
limit, err := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
||||
|
||||
Reference in New Issue
Block a user