Skip to content
Snippets Groups Projects
Commit dfcab3f0 authored by Christian Pointner's avatar Christian Pointner
Browse files

api now allows to wait for job states

parent 521f06d4
No related branches found
No related tags found
No related merge requests found
......@@ -25,11 +25,11 @@
package v1
import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
"context"
"github.com/gorilla/mux"
"gitlab.servus.at/autoradio/tank/importer"
......@@ -67,6 +67,16 @@ func (api *API) CreateFileForGroup() http.Handler {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "source-uri is invalid: " + err.Error()})
return
}
q := r.URL.Query()
waitFor := q.Get("wait-for")
switch waitFor {
case "running":
case "done":
case "":
default:
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "unable to wait for unknown state: " + waitFor})
return
}
group := vars["group-id"]
file := &store.File{}
......@@ -84,11 +94,26 @@ func (api *API) CreateFileForGroup() http.Handler {
sendError(w, err)
return
}
if err = job.Start(context.Background(), 3 * time.Hour); err != nil { // TODO: hardcoded value
if err = job.Start(context.Background(), 3*time.Hour); err != nil { // TODO: hardcoded value
// shall we remove the file here... thinking...
sendError(w, err)
return
}
if waitFor == "" {
sendWebResponse(w, http.StatusCreated, file)
return
}
switch waitFor {
case "running":
<-job.Running()
case "done":
<-job.Done()
}
if file, err = api.store.GetFile(group, file.ID); err != nil {
sendError(w, err)
return
}
sendWebResponse(w, http.StatusCreated, file)
})
}
......
......@@ -50,12 +50,31 @@ func (api *API) ReadImportOfFile() http.Handler {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
return
}
q := r.URL.Query()
waitFor := q.Get("wait-for")
switch waitFor {
case "running":
case "done":
case "":
default:
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "unable to wait for unknown state: " + waitFor})
return
}
job, err := api.importer.GetJob(vars["group-id"], id)
if err != nil {
// TODO: return import info from store if err == ErrNotFound
sendError(w, err)
return
}
switch waitFor {
case "running":
<-job.Running()
case "done":
<-job.Done()
}
sendWebResponse(w, http.StatusOK, job)
})
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment