Skip to content

Add a status field to /api response #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var imageBytes []byte

// global system state.
type SystemState struct {
Status string
QueueSize int
NodesLimit int
Timestamp string
Expand Down Expand Up @@ -77,7 +78,7 @@ type Progress struct {
Complete bool
}

type server struct {
type Server struct {
progress map[string]Progress
progressMutex sync.RWMutex
queue chan Task
Expand All @@ -88,12 +89,16 @@ type server struct {
image image.Image
nodesLimit int

timestampMutex sync.Mutex
lastTimestampTime time.Time
lastTimestamp string
lastUpdated LastUpdated
}

func (h *server) runTask(id int, task Task) error {
type LastUpdated struct {
mutex sync.Mutex
timestamp time.Time
checkedAt time.Time
}

func (h *Server) runTask(id int, task Task) error {
uuid := task.Uuid
fmt.Println("worker", id, "started job", uuid)
start := time.Now()
Expand Down Expand Up @@ -186,7 +191,7 @@ func (h *server) runTask(id int, task Task) error {
return nil
}

func (h *server) worker(id int, queue chan Task) {
func (h *Server) worker(id int, queue chan Task) {
for task := range queue {
h.progressMutex.Lock()
h.progress[task.Uuid] = Progress{}
Expand All @@ -201,7 +206,7 @@ func (h *server) worker(id int, queue chan Task) {
}
}

func (h *server) StartWorkers() {
func (h *Server) StartWorkers() {
h.queue = make(chan Task, 512)
h.progress = make(map[string]Progress)

Expand Down Expand Up @@ -253,7 +258,7 @@ func GetPixel(image image.Image, z int, x int, y int) float64 {

// check the filesystem for the result JSON
// if it's not started yet, return the position in the queue
func (h *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "POST" {
decoder := json.NewDecoder(r.Body)
Expand Down Expand Up @@ -311,22 +316,29 @@ func (h *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api" || r.URL.Path == "/api/" {
l := len(h.queue)

var timestamp string
h.timestampMutex.Lock()
h.lastUpdated.mutex.Lock()

if time.Since(h.lastTimestampTime).Seconds() > 10 {
if time.Since(h.lastUpdated.checkedAt).Seconds() > 10 {
cmd := exec.Command(h.exec, "query", h.data, "timestamp")
timestampRaw, _ := cmd.Output()
timestamp = strings.TrimSpace(string(timestampRaw))
h.lastTimestampTime = time.Now()
h.lastTimestamp = timestamp
} else {
timestamp = h.lastTimestamp
timestamp, err := time.Parse(time.RFC3339, strings.TrimSpace(string(timestampRaw)))
if err == nil {
h.lastUpdated.timestamp = timestamp
h.lastUpdated.checkedAt = time.Now()
}
}
h.timestampMutex.Unlock()

timestamp := h.lastUpdated.timestamp
h.lastUpdated.mutex.Unlock()

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(SystemState{l, h.nodesLimit, timestamp})

status := "ok"
if time.Now().Sub(timestamp).Minutes() > 15 {
status = "warn"
}

json.NewEncoder(w).Encode(SystemState{status, l, h.nodesLimit, timestamp.Format(time.RFC3339)})
} else if r.URL.Path == "/api/nodes.png" {
w.Header().Set("Content-Type", "image/png")
w.Write(imageBytes)
Expand Down Expand Up @@ -416,7 +428,7 @@ func main() {
return
}

srv := server{
srv := Server{
filesDir: filesDir,
tmpDir: tmpDir,
exec: exec,
Expand Down