/
service_queue_stats.go
61 lines (51 loc) · 1.75 KB
/
service_queue_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package rest
import (
"context"
"net/http"
"time"
"github.com/evergreen-ci/gimlet"
"github.com/mongodb/amboy"
"github.com/mongodb/grip"
)
type status struct {
Status string `bson:"string" json:"string" yaml:"string"`
QueueRunning bool `bson:"queue_running" json:"queue_running" yaml:"queue_running"`
PendingJobs int `bson:"pending_jobs,omitempty" json:"pending_jobs,omitempty" yaml:"pending_jobs,omitempty"`
SupportedJobTypes []string `bson:"supported_job_types" json:"supported_job_types" yaml:"supported_job_types"`
}
func (s *QueueService) getStatus(ctx context.Context) status {
output := status{
SupportedJobTypes: s.registeredTypes,
}
if s.queue != nil && s.queue.Info().Started {
output.Status = "ok"
output.QueueRunning = true
output.PendingJobs = s.queue.Stats(ctx).Pending
} else {
output.Status = "degraded"
}
return output
}
// Status defines an http.HandlerFunc that returns health check and
// current staus status information for the entire service.
func (s *QueueService) Status(w http.ResponseWriter, r *http.Request) {
gimlet.WriteJSON(w, s.getStatus(r.Context()))
}
// WaitAll blocks waiting for all pending jobs in the queue to
// stop. Has a default timeout of 10 seconds, and returns 408 (request
// timeout) when the timeout succeeds.
func (s *QueueService) WaitAll(w http.ResponseWriter, r *http.Request) {
timeout, err := parseTimeout(r)
if err != nil {
grip.Infof("problem parsing timeout for wait-all operation: %v", err)
}
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
ok := amboy.WaitInterval(ctx, s.queue, 100*time.Millisecond)
st := s.getStatus(ctx)
if !ok {
gimlet.WriteJSONResponse(w, http.StatusRequestTimeout, st)
return
}
gimlet.WriteJSON(w, st)
}