@@ -40,6 +40,7 @@ var imageBytes []byte
4040
4141// global system state.
4242type SystemState struct {
43+ Status string
4344 QueueSize int
4445 NodesLimit int
4546 Timestamp string
@@ -79,7 +80,7 @@ type Progress struct {
7980 Complete bool
8081}
8182
82- type server struct {
83+ type Server struct {
8384 progress map [string ]Progress
8485 progressMutex sync.RWMutex
8586 queue chan Task
@@ -90,12 +91,16 @@ type server struct {
9091 image image.Image
9192 nodesLimit int
9293
93- timestampMutex sync.Mutex
94- lastTimestampTime time.Time
95- lastTimestamp string
94+ lastUpdated LastUpdated
9695}
9796
98- func (h * server ) runTask (id int , task Task ) error {
97+ type LastUpdated struct {
98+ mutex sync.Mutex
99+ timestamp time.Time
100+ checkedAt time.Time
101+ }
102+
103+ func (h * Server ) runTask (id int , task Task ) error {
99104 uuid := task .Uuid
100105 fmt .Println ("worker" , id , "started job" , uuid )
101106 start := time .Now ()
@@ -188,7 +193,7 @@ func (h *server) runTask(id int, task Task) error {
188193 return nil
189194}
190195
191- func (h * server ) worker (id int , queue chan Task ) {
196+ func (h * Server ) worker (id int , queue chan Task ) {
192197 for task := range queue {
193198 h .progressMutex .Lock ()
194199 h .progress [task .Uuid ] = Progress {}
@@ -203,7 +208,7 @@ func (h *server) worker(id int, queue chan Task) {
203208 }
204209}
205210
206- func (h * server ) StartWorkers () {
211+ func (h * Server ) StartWorkers () {
207212 h .queue = make (chan Task , 512 )
208213 h .progress = make (map [string ]Progress )
209214
@@ -318,7 +323,7 @@ func parseInput(body io.Reader) (orb.Geometry, string, string, json.RawMessage,
318323
319324// check the filesystem for the result JSON
320325// if it's not started yet, return the position in the queue
321- func (h * server ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
326+ func (h * Server ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
322327 w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
323328 if r .Method == "POST" {
324329 geom , sanitized_name , sanitized_type , sanitized_region , err := parseInput (r .Body )
@@ -351,22 +356,29 @@ func (h *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
351356 if r .URL .Path == "/api" || r .URL .Path == "/api/" {
352357 l := len (h .queue )
353358
354- var timestamp string
355- h .timestampMutex .Lock ()
359+ h .lastUpdated .mutex .Lock ()
356360
357- if time .Since (h .lastTimestampTime ).Seconds () > 10 {
361+ if time .Since (h .lastUpdated . checkedAt ).Seconds () > 10 {
358362 cmd := exec .Command (h .exec , "query" , h .data , "timestamp" )
359363 timestampRaw , _ := cmd .Output ()
360- timestamp = strings .TrimSpace (string (timestampRaw ))
361- h . lastTimestampTime = time . Now ()
362- h . lastTimestamp = timestamp
363- } else {
364- timestamp = h . lastTimestamp
364+ timestamp , err := time . Parse ( time . RFC3339 , strings .TrimSpace (string (timestampRaw ) ))
365+ if err == nil {
366+ h . lastUpdated . timestamp = timestamp
367+ h . lastUpdated . checkedAt = time . Now ()
368+ }
365369 }
366- h .timestampMutex .Unlock ()
370+
371+ timestamp := h .lastUpdated .timestamp
372+ h .lastUpdated .mutex .Unlock ()
367373
368374 w .Header ().Set ("Content-Type" , "application/json" )
369- json .NewEncoder (w ).Encode (SystemState {l , h .nodesLimit , timestamp })
375+
376+ status := "ok"
377+ if time .Now ().Sub (timestamp ).Minutes () > 15 {
378+ status = "warn"
379+ }
380+
381+ json .NewEncoder (w ).Encode (SystemState {status , l , h .nodesLimit , timestamp .Format (time .RFC3339 )})
370382 } else if r .URL .Path == "/api/nodes.png" {
371383 w .Header ().Set ("Content-Type" , "image/png" )
372384 w .Write (imageBytes )
@@ -456,7 +468,7 @@ func main() {
456468 return
457469 }
458470
459- srv := server {
471+ srv := Server {
460472 filesDir : filesDir ,
461473 tmpDir : tmpDir ,
462474 exec : exec ,
0 commit comments