Skip to content

Commit

Permalink
add Jobs to status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
geek authored and jwreagor committed Sep 20, 2017
1 parent 8df9b48 commit a66e0fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
25 changes: 21 additions & 4 deletions telemetry/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ import (
type Status struct {
Version string
jobs []*jobs.Job
Services []*jobStatusResponse
Jobs []*jobStatusResponse
Services []*serviceStatusResponse
Watches []string
}

type jobStatusResponse struct {
Name string
Status string
}

type serviceStatusResponse struct {
Name string
Address string
Port int
Expand Down Expand Up @@ -51,6 +57,11 @@ func (sh StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
service.Status = status
}
}
for _, jobStatus := range sh.telem.Status.Jobs {
if jobStatus.Name == job.Name {
jobStatus.Status = status
}
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand All @@ -61,15 +72,21 @@ func (sh StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (t *Telemetry) MonitorJobs(jobs []*jobs.Job) {
if t != nil {
for _, job := range jobs {
t.Status.jobs = append(t.Status.jobs, job)
if job.Service != nil && job.Service.Port != 0 {
service := &jobStatusResponse{
serviceResponse := &serviceStatusResponse{
Name: job.Name,
Address: job.Service.IPAddress,
Port: job.Service.Port,
Status: fmt.Sprintf("%s", job.GetStatus()),
}
t.Status.jobs = append(t.Status.jobs, job)
t.Status.Services = append(t.Status.Services, service)
t.Status.Services = append(t.Status.Services, serviceResponse)
} else {
jobResponse := &jobStatusResponse{
Name: job.Name,
Status: fmt.Sprintf("%s", job.GetStatus()),
}
t.Status.Jobs = append(t.Status.Jobs, jobResponse)
}
}
}
Expand Down
33 changes: 25 additions & 8 deletions telemetry/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ func TestStatusServerGet(t *testing.T) {

jobCfgs, err := jobs.NewConfigs(
tests.DecodeRawToSlice(
`[{name: "myjob1", exec: "sleep 10"},
{name: "myjob2", exec: "sleep 10",
port: 80, interfaces: ["inet", "lo0"],
health: { exec: "true", interval: 1, ttl: 2}}]`),
`[
{ name: "myjob1", exec: "sleep 10" },
{
name: "myjob2",
exec: "sleep 10",
port: 80,
interfaces: ["inet", "lo0"],
health: { exec: "true", interval: 1, ttl: 10 }
},
{
name: "myjob3",
exec: "sleep 10",
health: { exec: "true", interval: 1, ttl: 10 }
}
]`),
noop)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -88,9 +99,15 @@ func TestStatusServerGet(t *testing.T) {
t.Fatal(err)
}

assert.Equal(t, out.Watches, []string{"watch1", "watch2"},
// expected, actual
assert.Equal(t, []string{"watch1", "watch2"}, out.Watches,
"unexpected value for 'watches'")
assert.Equal(t, len(out.Services), 1, "unexpected count of services")
assert.Equal(t, out.Services[0].Port, 80, "unexpected job port")
assert.Equal(t, out.Services[0].Status, "unknown", "unexpected job status")
assert.Equal(t, 1, len(out.Services), "unexpected count of services")
assert.Equal(t, 80, out.Services[0].Port, "unexpected job port")
assert.Equal(t, "unknown", out.Services[0].Status, "unexpected job status")
assert.Equal(t, 2, len(out.Jobs), "unexpected count of services")
assert.Equal(t, "myjob1", out.Jobs[0].Name)
assert.Equal(t, "unknown", out.Jobs[0].Status, "unexpected job status")
assert.Equal(t, "myjob3", out.Jobs[1].Name)
assert.Equal(t, "unknown", out.Jobs[1].Status, "unexpected job status")
}

0 comments on commit a66e0fe

Please sign in to comment.