Remove "sync" from the list of valid job types to align with the removal of JobTypeSync from the Worker service.
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"bookhoard/internal/database"
|
|
"bookhoard/internal/services"
|
|
)
|
|
|
|
type JobsHandler struct {
|
|
db *database.Queries
|
|
worker *services.Worker
|
|
}
|
|
|
|
func NewJobsHandler(db *database.Queries, worker *services.Worker) *JobsHandler {
|
|
return &JobsHandler{
|
|
db: db,
|
|
worker: worker,
|
|
}
|
|
}
|
|
|
|
// CreateJob creates a new job based on type
|
|
func (h *JobsHandler) CreateJob(c echo.Context) error {
|
|
var req struct {
|
|
Type string `json:"type"`
|
|
Params map[string]interface{} `json:"params"`
|
|
}
|
|
|
|
if err := c.Bind(&req); err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
"error": "Invalid request body",
|
|
})
|
|
}
|
|
|
|
// Validate job type
|
|
var jobType services.JobType
|
|
switch req.Type {
|
|
case "import", "convert", "thumbnails", "backup", "analytics":
|
|
jobType = services.JobType(req.Type)
|
|
default:
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
"error": "Invalid job type",
|
|
})
|
|
}
|
|
|
|
// Add database to params
|
|
req.Params["db"] = h.db
|
|
|
|
// Create job
|
|
job := &services.Job{
|
|
ID: uuid.New().String(),
|
|
Type: jobType,
|
|
Params: req.Params,
|
|
Status: services.JobStatusPending,
|
|
}
|
|
|
|
// Enqueue job
|
|
if err := h.worker.EnqueueJob(job); err != nil {
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
"error": "Failed to enqueue job",
|
|
})
|
|
}
|
|
|
|
return c.JSON(http.StatusAccepted, map[string]interface{}{
|
|
"message": "Job created",
|
|
"job_id": job.ID,
|
|
"type": req.Type,
|
|
"status": "pending",
|
|
})
|
|
}
|
|
|
|
// GetJobStatus returns the status of a specific job
|
|
func (h *JobsHandler) GetJobStatus(c echo.Context) error {
|
|
jobID := c.Param("jobId")
|
|
|
|
result, exists := h.worker.GetJobStatus(jobID)
|
|
if !exists {
|
|
return c.JSON(http.StatusNotFound, map[string]string{
|
|
"error": "Job not found",
|
|
})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, result)
|
|
}
|