Skip to content

Commit

Permalink
Merge pull request #45 from sttts/verbose-errors
Browse files Browse the repository at this point in the history
Make bamboo verbose on haproxy reload errors
  • Loading branch information
Jing Dong committed Oct 20, 2014
2 parents 942eda8 + 6c0e875 commit 58e198c
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions services/event_bus/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,43 @@ type Handlers struct {

func (h *Handlers) MarathonEventHandler(event MarathonEvent) {
log.Printf("%s => %s\n", event.EventType, event.Timestamp)
handleHAPUpdate(h.Conf, h.Zookeeper)
queueUpdate(h)
h.Conf.StatsD.Increment(1.0, "reload.marathon", 1)
}

func (h *Handlers) ServiceEventHandler(event ServiceEvent) {
log.Println("Domain mapping: Stated changed")
handleHAPUpdate(h.Conf, h.Zookeeper)
queueUpdate(h)
h.Conf.StatsD.Increment(1.0, "reload.domain", 1)
}

var execSem = make(chan int, 1)
var updateChan = make(chan *Handlers, 1)

func init() {
go func () {
log.Println("Starting update loop")
for {
h := <-updateChan
handleHAPUpdate(h.Conf, h.Zookeeper)
}
} ()
}

var queueUpdateSem = make(chan int, 1)

func queueUpdate(h *Handlers) {
queueUpdateSem <- 1

select {
case _ = <-updateChan:
log.Println("Found pending update request. Don't start another one.")
default:
log.Println("Queuing an haproxy update.")
}
updateChan <- h

<-queueUpdateSem
}

func handleHAPUpdate(conf *configuration.Configuration, conn *zk.Conn) bool {
currentContent, _ := ioutil.ReadFile(conf.HAProxy.OutputPath)
Expand All @@ -61,22 +87,25 @@ func handleHAPUpdate(conf *configuration.Configuration, conn *zk.Conn) bool {
err := ioutil.WriteFile(conf.HAProxy.OutputPath, []byte(newContent), 0666)
if err != nil { log.Fatalf("Failed to write template on path: %s", err) }

execSem <- 1
execCommand(conf.HAProxy.ReloadCommand)
<-execSem

log.Println("HAProxy: Configuration updated")
err = execCommand(conf.HAProxy.ReloadCommand)
if err != nil {
log.Fatalf("HAProxy: update failed\n")
} else {
log.Println("HAProxy: Configuration updated")
}
return true
} else {
log.Println("HAProxy: Same content, no need to reload")
return false
}
}

func execCommand(cmd string) {
_, err := exec.Command("sh", "-c", cmd).Output()
func execCommand(cmd string) error {
log.Printf("Exec cmd: %s \n", cmd)
output, err := exec.Command("sh", "-c", cmd).CombinedOutput()
if err != nil {
log.Println(err.Error())
log.Println("Output:\n" + string(output[:]))
}
log.Printf("Exec cmd: %s \n", cmd)
return err
}

0 comments on commit 58e198c

Please sign in to comment.