next ai job handler

This commit is contained in:
Erik Winter 2024-01-20 13:12:13 +01:00
parent 6b8ec34b8e
commit afb1cea172
1 changed files with 25 additions and 1 deletions

View File

@ -1,7 +1,10 @@
package handler
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
@ -29,12 +32,14 @@ func (jobAPI *JobAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
jobAPI.Add(w, r)
case r.Method == http.MethodGet && subPath == "":
jobAPI.List(w, r)
case r.Method == http.MethodGet && subPath == "next-ai":
jobAPI.NextAI(w, r)
case r.Method == http.MethodDelete && subPath != "":
jobAPI.Delete(w, r, subPath)
case r.Method == http.MethodDelete && subPath == "":
jobAPI.DeleteAll(w, r)
default:
Error(w, http.StatusNotFound, "unregistered path", nil, logger)
Error(w, http.StatusNotFound, "unregistered path", fmt.Errorf("method %q with subpath %q was not registered in /movie", r.Method, subPath), logger)
}
}
@ -73,6 +78,25 @@ func (jobAPI *JobAPI) List(w http.ResponseWriter, r *http.Request) {
}
}
func (jobAPI *JobAPI) NextAI(w http.ResponseWriter, r *http.Request) {
logger := jobAPI.logger.With("method", "nextai")
j, err := jobAPI.jq.Next(job.TypeAI)
switch {
case errors.Is(err, sql.ErrNoRows):
logger.Info("no ai jobs found")
w.WriteHeader(http.StatusNoContent)
case err != nil:
Error(w, http.StatusInternalServerError, "could not get next ai job", err, logger)
return
}
if err := json.NewEncoder(w).Encode(j); err != nil {
Error(w, http.StatusInternalServerError, "could not encode job", err, logger)
return
}
}
func (jobAPI *JobAPI) Delete(w http.ResponseWriter, r *http.Request, id string) {
logger := jobAPI.logger.With("method", "delete")