85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
// backend\tasks_status.go
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type CleanupTaskState struct {
|
|
Running bool `json:"running"`
|
|
Text string `json:"text,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
type TasksStatusResponse struct {
|
|
GenerateAssets AssetsTaskState `json:"generateAssets"`
|
|
Cleanup CleanupTaskState `json:"cleanup"`
|
|
AnyRunning bool `json:"anyRunning"`
|
|
}
|
|
|
|
var (
|
|
cleanupTaskMu sync.Mutex
|
|
cleanupTaskState CleanupTaskState
|
|
)
|
|
|
|
func snapshotCleanupTaskState() CleanupTaskState {
|
|
cleanupTaskMu.Lock()
|
|
st := cleanupTaskState
|
|
cleanupTaskMu.Unlock()
|
|
return st
|
|
}
|
|
|
|
func setCleanupTaskRunning(text string) {
|
|
cleanupTaskMu.Lock()
|
|
cleanupTaskState = CleanupTaskState{
|
|
Running: true,
|
|
Text: strings.TrimSpace(text),
|
|
Error: "",
|
|
}
|
|
cleanupTaskMu.Unlock()
|
|
}
|
|
|
|
func setCleanupTaskDone(text string) {
|
|
cleanupTaskMu.Lock()
|
|
cleanupTaskState = CleanupTaskState{
|
|
Running: false,
|
|
Text: strings.TrimSpace(text),
|
|
Error: "",
|
|
}
|
|
cleanupTaskMu.Unlock()
|
|
}
|
|
|
|
func setCleanupTaskError(errText string) {
|
|
cleanupTaskMu.Lock()
|
|
cleanupTaskState = CleanupTaskState{
|
|
Running: false,
|
|
Text: "",
|
|
Error: strings.TrimSpace(errText),
|
|
}
|
|
cleanupTaskMu.Unlock()
|
|
}
|
|
|
|
func tasksStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Nur GET erlaubt", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
assets := getGenerateAssetsTaskStatus()
|
|
cleanup := snapshotCleanupTaskState()
|
|
|
|
resp := TasksStatusResponse{
|
|
GenerateAssets: assets,
|
|
Cleanup: cleanup,
|
|
AnyRunning: assets.Running || cleanup.Running,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
_ = json.NewEncoder(w).Encode(resp)
|
|
}
|