Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bamboo verbose on haproxy reload errors #45

Merged
merged 2 commits into from
Oct 20, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}