Skip to content

Commit

Permalink
Merge pull request #47 from blp1526/goroutine
Browse files Browse the repository at this point in the history
Use goroutine for currentServerUp
  • Loading branch information
blp1526 authored Dec 31, 2017
2 parents fd0706d + 5abe55a commit 461de8c
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/isac.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ MAINLOOP:
case termbox.KeyArrowDown, termbox.KeyCtrlN:
i.currentRowDown()
case termbox.KeyCtrlU:
message := i.currentServerUp()
i.draw(message)
i.currentServerUp()
case termbox.KeyCtrlR:
i.refresh()
case termbox.KeyBackspace2, termbox.KeyCtrlH:
Expand Down Expand Up @@ -239,29 +238,36 @@ func (i *Isac) currentNo() int {
return i.row.Current + 1 - i.row.HeadersSize()
}

func (i *Isac) currentServerUp() (message string) {
func (i *Isac) currentServerUp() {
s := i.serverByCurrentRow[i.row.Current]

if s.ID == "" {
return "[ERROR] Current row has no Server"
i.draw("[ERROR] Current row has no Server")
return
}

if s.Instance.Status == "up" {
return fmt.Sprintf("[WARNING] Server.Name %v is already up", s.Name)
i.draw(fmt.Sprintf("[WARNING] Server.Name %v is already up", s.Name))
return
}

url := i.client.URL(s.Zone.Name, []string{"server", s.ID, "power"})
statusCode, _, err := i.client.Request("PUT", url, nil)
go func() {
url := i.client.URL(s.Zone.Name, []string{"server", s.ID, "power"})
statusCode, _, err := i.client.Request("PUT", url, nil)

if err != nil {
return fmt.Sprintf("[ERROR] %v", err)
}
if err != nil {
i.draw(fmt.Sprintf("[ERROR] %v", err))
return
}

if statusCode != 200 {
return fmt.Sprintf("[ERROR] Request Method: PUT, Request URL: %v, Status Code: %v", url, statusCode)
}
if statusCode != 200 {
i.draw(fmt.Sprintf("[ERROR] Request Method: PUT, Server.Name: %v, Status Code: %v", s.Name, statusCode))
return
}

return fmt.Sprintf("Server.Name %v is booting, wait few seconds, and refresh", s.Name)
i.draw(fmt.Sprintf("Server.Name %v is booting, wait few seconds, and refresh", s.Name))
return
}()
}

func (i *Isac) refresh() {
Expand Down

0 comments on commit 461de8c

Please sign in to comment.