Skip to content

Commit

Permalink
Merge pull request #1 from takyoshi/remember-previous-status-key
Browse files Browse the repository at this point in the history
Remember previous item key to invoke a trigger as the last status changed item
  • Loading branch information
fujiwara authored Oct 3, 2016
2 parents 135b9ad + 83e9a64 commit 8818755
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func kvApiProxy(w http.ResponseWriter, r *http.Request) {
func watchForTrigger(command string) {
var index int64
lastStatus := make(map[string]Status)
prevItem := make(map[Item]Status)
for {
resp, newIndex, err := callConsulAPI(
"/v1/kv/" + Namespace + "/?recurse&wait=55s&index=" + strconv.FormatInt(index, 10),
Expand All @@ -231,7 +232,20 @@ func watchForTrigger(command string) {
dec := json.NewDecoder(resp.Body)
dec.Decode(&kvps)

// find each current item of category
currentItem := make(map[string]Item)
for _, kv := range kvps {
item := kv.NewItem()
if !itemInNodes(&item) {
continue
}

current := compactItem(item)
_, exist := prevItem[current]
if exist && prevItem[current] != item.Status {
currentItem[item.Category] = item
}
}
for _, kv := range kvps {
item := kv.NewItem()
if !itemInNodes(&item) {
Expand All @@ -243,9 +257,11 @@ func watchForTrigger(command string) {
currentItem[item.Category] = item
}
}

// invoke trigger when a category status was changed
for category, item := range currentItem {
if _, exist := lastStatus[category]; !exist {
// at first initialze
// at first initialize
lastStatus[category] = item.Status
log.Printf("[info] %s: status %s", category, item.Status)
} else if lastStatus[category] != item.Status {
Expand All @@ -259,10 +275,27 @@ func watchForTrigger(command string) {
}
}
}

// update previous item status
for _, kv := range kvps {
item := kv.NewItem()
prev := compactItem(item)
prevItem[prev] = item.Status
}

time.Sleep(1 * time.Second)
}
}

// compactItem builds `Item` struct that has only `Category`, `Key`, and `Node` fields.
func compactItem(item Item) Item {
return Item{
Key: item.Key,
Category: item.Category,
Node: item.Node,
}
}

func invokePipe(command string, src io.Reader) error {
log.Println("[info] Invoking command:", command)
cmd := exec.Command("sh", "-c", command)
Expand Down

0 comments on commit 8818755

Please sign in to comment.