2023-09-06 10:28:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"github.com/gorilla/mux"
|
2023-09-06 12:27:36 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-09-06 16:30:36 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-09-06 10:28:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
jobStatus struct {
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
Status status `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
status string
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusCreated = "created"
|
|
|
|
statusStarted = "started"
|
|
|
|
statusError = "error"
|
|
|
|
statusFinished = "finished"
|
|
|
|
)
|
|
|
|
|
2023-09-06 12:27:36 +00:00
|
|
|
//revive:disable-next-line:get-return // Is not a getter
|
2023-09-06 10:28:03 +00:00
|
|
|
func getJobStatus(res http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
uid, err := uuid.FromString(vars["uid"])
|
|
|
|
if err != nil {
|
|
|
|
http.Error(res, "UUID had unexpected format!", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 12:27:36 +00:00
|
|
|
status, err := loadStatusByUUID(uid)
|
|
|
|
if err != nil {
|
|
|
|
serverErrorf(res, err, "reading status file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if encErr := json.NewEncoder(res).Encode(status); encErr != nil {
|
|
|
|
serverErrorf(res, encErr, "serializing status file")
|
2023-09-06 10:28:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadStatusByUUID(uid uuid.UUID) (*jobStatus, error) {
|
|
|
|
statusFile := pathFromUUID(uid, filenameStatus)
|
|
|
|
|
|
|
|
status := jobStatus{}
|
|
|
|
// #nosec G304
|
2023-09-06 12:27:36 +00:00
|
|
|
f, err := os.Open(statusFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "opening status file")
|
|
|
|
}
|
2023-09-06 16:30:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing status file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2023-09-06 12:27:36 +00:00
|
|
|
|
|
|
|
if err = json.NewDecoder(f).Decode(&status); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decoding status file")
|
2023-09-06 10:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *jobStatus) UpdateStatus(st status) {
|
|
|
|
s.Status = st
|
|
|
|
s.UpdatedAt = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s jobStatus) Save() error {
|
|
|
|
uid, _ := uuid.FromString(s.UUID) // #nosec G104
|
|
|
|
f, err := os.Create(pathFromUUID(uid, filenameStatusTemp))
|
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "creating status file")
|
2023-09-06 10:28:03 +00:00
|
|
|
}
|
2023-09-06 16:30:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing status file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2023-09-06 10:28:03 +00:00
|
|
|
|
|
|
|
if err = json.NewEncoder(f).Encode(s); err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "encoding status")
|
2023-09-06 10:28:03 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(
|
|
|
|
os.Rename(
|
|
|
|
pathFromUUID(uid, filenameStatusTemp),
|
|
|
|
pathFromUUID(uid, filenameStatus),
|
|
|
|
),
|
|
|
|
"moving status file in place",
|
2023-09-06 10:28:03 +00:00
|
|
|
)
|
|
|
|
}
|