[go: nahoru, domu]

Skip to content

Commit

Permalink
implements basic stats
Browse files Browse the repository at this point in the history
  • Loading branch information
mhausenblas committed Jun 13, 2016
1 parent c368816 commit 85b0f6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
50 changes: 37 additions & 13 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package main

import (
// "encoding/json"
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
// "io/ioutil"
marathon "github.com/gambol99/go-marathon"
"math/rand"
"net/http"
"strconv"
"sync/atomic"
)

// API nouns
type NOUN_Stats struct{}
type NOUN_Rampage struct{}

// JSON payloads
type StatsResult struct {
TasksKilled uint64 `json:"gone"`
}

type RampageResult struct {
Success bool `json:"success"`
TaskList []string `json:"goners"`
}

// Handles /stats API calls
func (n NOUN_Stats) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"handle": "/stats"}).Info("Reporting on runtime statistics ...")
Expand All @@ -26,7 +37,11 @@ func (n NOUN_Stats) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
log.WithFields(log.Fields{"handle": "/stats"}).Info("... from beginning of time")
}
fmt.Fprint(w, "not yet implemented")
sr := &StatsResult{}
sr.TasksKilled = atomic.LoadUint64(&overallTasksKilled)
jsonsr, _ := json.Marshal(sr)
w.Header().Set("Content-Type", "application/javascript")
fmt.Fprint(w, string(jsonsr))
}

// Handles /rampage API calls
Expand Down Expand Up @@ -60,19 +75,21 @@ func (n NOUN_Rampage) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// to be killed and randomly kill off a few of them
func killTasks(w http.ResponseWriter, r *http.Request) {
if client, ok := getClient(); ok {
nonFrameworkApps := 0
apps, err := client.Applications(nil)
if err != nil {
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Failed to list apps")
http.Error(w, "Failed to list apps", 500)
return
}
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Found ", len(apps.Apps), " applications running")
b := ""
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Found overall ", len(apps.Apps), " applications running")
candidates := []string{}
rr := &RampageResult{}
for _, app := range apps.Apps {
log.WithFields(log.Fields{"handle": "/rampage"}).Debug("APP ", app.ID)
details, _ := client.Application(app.ID)
if !isFramework(details) {
nonFrameworkApps++
if details.Tasks != nil && len(details.Tasks) > 0 {
for _, task := range details.Tasks {
log.WithFields(log.Fields{"handle": "/rampage"}).Debug("TASK ", task.ID)
Expand All @@ -81,33 +98,36 @@ func killTasks(w http.ResponseWriter, r *http.Request) {
}
}
}

if len(candidates) > 0 {
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Found ", len(candidates), " non-framework tasks in ", nonFrameworkApps, " apps to kill")
// pick one random task to be killed
candidate := candidates[rand.Intn(len(candidates))]
ok := killTask(client, candidate)
if ok {
b += fmt.Sprintf("Killed task %s", candidate)
} else {
b += fmt.Sprintf("Failed to kill task %s", candidate)
rr.Success = killTask(client, candidate)
if rr.Success {
rr.TaskList = []string{candidate}
log.WithFields(log.Fields{"handle": "/rampage"}).Info("Killed tasks ", rr.TaskList)
}
} else {
b = fmt.Sprintf("No task found to kill")
rr.Success = false
}
fmt.Fprint(w, b)
jsonrr, _ := json.Marshal(rr)
w.Header().Set("Content-Type", "application/javascript")
fmt.Fprint(w, string(jsonrr))
} else {
http.Error(w, "Can't connect to Marathon", 500)
}
}

// killTask kills a certain task
// killTask kills a certain task and increments
// the overall count if successful
func killTask(c marathon.Marathon, taskID string) bool {
_, err := c.KillTask(taskID, nil)
if err != nil {
log.WithFields(log.Fields{"handle": "/rampage"}).Debug("Not able to kill task ", taskID)
return false
} else {
log.WithFields(log.Fields{"handle": "/rampage"}).Debug("Killed task ", taskID)
go incTasksKilled()
return true
}
}
Expand Down Expand Up @@ -135,3 +155,7 @@ func getClient() (marathon.Marathon, bool) {
}
return client, true
}

func incTasksKilled() {
atomic.AddUint64(&overallTasksKilled, 1)
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ const (
)

var (
mux *http.ServeMux
destructionLevel DestructionLevel = DL_BASIC
marathonURL string
mux *http.ServeMux
marathonURL string
destructionLevel DestructionLevel = DL_BASIC
overallTasksKilled uint64
)

func init() {
Expand Down

0 comments on commit 85b0f6b

Please sign in to comment.