format down result message
This commit is contained in:
@@ -1,36 +1,26 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
api "github.com/metrue/fx/docker-api"
|
||||
Message "github.com/metrue/fx/message"
|
||||
)
|
||||
|
||||
// Down stops the processes designated by a function
|
||||
func Down(containID, image string, msgChan chan<- string, doneChan chan<- bool) {
|
||||
checkErr := func(err error) bool {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
doneChan <- false
|
||||
return true
|
||||
}
|
||||
return false
|
||||
func Down(containerId string, image string, result chan<- Message.DownMsgMeta) {
|
||||
res := Message.DownMsgMeta{
|
||||
ContainerId: containerId,
|
||||
ContainerStatus: "",
|
||||
ImageStatus: "",
|
||||
}
|
||||
err := api.Remove(containerId)
|
||||
if err == nil {
|
||||
res.ContainerStatus = "stopped"
|
||||
}
|
||||
|
||||
err := api.Remove(containID)
|
||||
if checkErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("I am closed " + containID)
|
||||
msgChan <- fmt.Sprintf("Container[%s] Removed", containID)
|
||||
if err := api.ImageRemove(image); err != nil {
|
||||
log.Printf("cleanup docker image[%s] error: %s\n", image, err.Error())
|
||||
msgChan <- fmt.Sprintf("Image[%s] Removing Failed", image)
|
||||
res.ImageStatus = "not removed"
|
||||
} else {
|
||||
msgChan <- fmt.Sprintf("Image[%s] Removed", image)
|
||||
res.ImageStatus = "removed"
|
||||
}
|
||||
|
||||
doneChan <- true
|
||||
result <- res
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ func NewMessage(msgType string, msgBody string) {
|
||||
}
|
||||
|
||||
type DownMsgMeta struct {
|
||||
containerId string
|
||||
containerStatus string
|
||||
imageStatus string
|
||||
ContainerId string
|
||||
ContainerStatus string
|
||||
ImageStatus string
|
||||
}
|
||||
|
||||
//----------------------------
|
||||
@@ -22,11 +22,11 @@ type DownMsgMeta struct {
|
||||
//----------------------------
|
||||
func CreateDownMessage(downs []DownMsgMeta) (msg string) {
|
||||
msgPrefix := `------------------------------------------------------
|
||||
ID ServiceStatus ResourceStatus`
|
||||
ID ServiceStatus ResourceStatus`
|
||||
|
||||
msg += msgPrefix
|
||||
for _, down := range downs {
|
||||
msg += fmt.Sprintf("\n%s\t\t%s\t\t\t%s", down.containerId, down.containerStatus, down.imageStatus)
|
||||
msg += fmt.Sprintf("\n%s\t\t%s\t\t\t%s", down.ContainerId, down.ContainerStatus, down.ImageStatus)
|
||||
}
|
||||
msgSuffix := "\n------------------------------------------------------"
|
||||
msg += msgSuffix
|
||||
|
||||
@@ -4,20 +4,20 @@ import "testing"
|
||||
|
||||
func TestCreateDownMessage(t *testing.T) {
|
||||
a := DownMsgMeta{
|
||||
containerId: "id1",
|
||||
containerStatus: "stopped",
|
||||
imageStatus: "removed",
|
||||
ContainerId: "id1",
|
||||
ContainerStatus: "stopped",
|
||||
ImageStatus: "removed",
|
||||
}
|
||||
b := DownMsgMeta{
|
||||
containerId: "id2",
|
||||
containerStatus: "stopped",
|
||||
imageStatus: "removed",
|
||||
ContainerId: "id2",
|
||||
ContainerStatus: "stopped",
|
||||
ImageStatus: "removed",
|
||||
}
|
||||
|
||||
downs := []DownMsgMeta{a, b}
|
||||
msg := CreateDownMessage(downs)
|
||||
expectMsg := `------------------------------------------------------
|
||||
ID ServiceStatus ResourceStatus
|
||||
ID ServiceStatus ResourceStatus
|
||||
id1 stopped removed
|
||||
id2 stopped removed
|
||||
------------------------------------------------------`
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/metrue/fx/config"
|
||||
"github.com/metrue/fx/env"
|
||||
"github.com/metrue/fx/handlers"
|
||||
Message "github.com/metrue/fx/message"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
@@ -96,9 +97,6 @@ func down(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
doneCh := make(chan bool)
|
||||
msgCh := make(chan string)
|
||||
|
||||
mt, message, err := c.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println("read: ", err)
|
||||
@@ -109,39 +107,26 @@ func down(w http.ResponseWriter, r *http.Request) {
|
||||
ids = strings.Split(msg, " ")
|
||||
}
|
||||
containers := handlers.List(ids...)
|
||||
|
||||
count := len(containers)
|
||||
downResultCh := make(chan Message.DownMsgMeta, count)
|
||||
for _, container := range containers {
|
||||
ids = append(ids, container.ID)
|
||||
go handlers.Down(container.ID[:10], container.Image, msgCh, doneCh)
|
||||
go handlers.Down(container.ID[:10], container.Image, downResultCh)
|
||||
}
|
||||
|
||||
numSuccess := 0
|
||||
numFail := 0
|
||||
defer func() {
|
||||
close(doneCh)
|
||||
close(msgCh)
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case newDone := <-doneCh:
|
||||
if newDone {
|
||||
numSuccess++
|
||||
} else {
|
||||
numFail++
|
||||
}
|
||||
|
||||
if numSuccess+numFail == count {
|
||||
res := fmt.Sprintf("Succed: %d", numSuccess)
|
||||
c.WriteMessage(mt, []byte(res))
|
||||
res = fmt.Sprintf("Failed: %d", numFail)
|
||||
c.WriteMessage(mt, []byte(res))
|
||||
closeConn(c, "0")
|
||||
return
|
||||
}
|
||||
case newMsg := <-msgCh:
|
||||
c.WriteMessage(mt, []byte(newMsg))
|
||||
// collect down result
|
||||
var downs []Message.DownMsgMeta
|
||||
for downResult := range downResultCh {
|
||||
downs = append(downs, downResult)
|
||||
if len(downs) == count {
|
||||
close(downResultCh)
|
||||
}
|
||||
}
|
||||
|
||||
msg := Message.CreateDownMessage(downs)
|
||||
c.WriteMessage(mt, []byte(msg))
|
||||
closeConn(c, "0")
|
||||
}
|
||||
|
||||
func closeConn(c *websocket.Conn, msg string) {
|
||||
|
||||
Reference in New Issue
Block a user