Skip to content

Commit 021a4f0

Browse files
committed
Merge branch 'master' into 'master'
refactor See merge request !82
2 parents cb01060 + 9b9814d commit 021a4f0

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.29
1+
0.7.29a

cluster/calcium/run_and_wait.go

+20-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package calcium
33
import (
44
"bufio"
55
"fmt"
6-
"sync"
76

87
log "github.com/Sirupsen/logrus"
98
enginetypes "github.com/docker/docker/api/types"
@@ -27,14 +26,12 @@ func (c *calcium) RunAndWait(specs types.Specs, opts *types.DeployOptions) (chan
2726
}
2827

2928
go func() {
30-
wg := &sync.WaitGroup{}
3129
defer log.Info("[RunAndWait] Finish run and wait for containers")
3230
defer close(ch)
33-
defer wg.Wait()
3431
logsOpts := enginetypes.ContainerLogsOptions{Follow: true, ShowStdout: true, ShowStderr: true}
3532

33+
ids := map[string]*types.Node{}
3634
for message := range createChan {
37-
wg.Add(1)
3835
if message.ContainerID == "" {
3936
log.Errorf("[RunAndWait] Can't find container id %s", err.Error())
4037
continue
@@ -46,9 +43,9 @@ func (c *calcium) RunAndWait(specs types.Specs, opts *types.DeployOptions) (chan
4643
continue
4744
}
4845

49-
go func(node *types.Node, message *types.CreateContainerMessage) {
50-
defer wg.Done()
51-
resp, err := node.Engine.ContainerLogs(context.Background(), message.ContainerID, logsOpts)
46+
ids[message.ContainerID] = node
47+
go func(node *types.Node, containerID string) {
48+
resp, err := node.Engine.ContainerLogs(context.Background(), containerID, logsOpts)
5249
if err != nil {
5350
log.Errorf("[RunAndWait] Failed to get logs, %s", err.Error())
5451
return
@@ -58,31 +55,30 @@ func (c *calcium) RunAndWait(specs types.Specs, opts *types.DeployOptions) (chan
5855
scanner := bufio.NewScanner(stream)
5956
for scanner.Scan() {
6057
data := scanner.Bytes()
61-
ch <- &types.RunAndWaitMessage{ContainerID: message.ContainerID, Data: data}
62-
log.Debugf("[RunAndWait] %s %s", message.ContainerID[:12], data)
58+
ch <- &types.RunAndWaitMessage{ContainerID: containerID, Data: data}
59+
log.Debugf("[RunAndWait] %s %s", containerID[:12], data)
6360
}
6461

6562
if err := scanner.Err(); err != nil {
6663
log.Errorf("[RunAndWait] Parse log failed, %s", err.Error())
6764
return
6865
}
66+
}(node, message.ContainerID)
67+
}
6968

70-
container, err := c.GetContainer(message.ContainerID)
71-
if err != nil {
72-
log.Errorf("[RunAndWait] Container not found, %s", err.Error())
73-
return
74-
}
75-
76-
containerJSON, err := container.Inspect()
77-
defer func() { go c.removeOneContainer(container, containerJSON) }()
78-
exitData := []byte(fmt.Sprintf("[exitcode] %d", containerJSON.State.ExitCode))
79-
if err != nil {
80-
exitData = []byte(fmt.Sprintf("[exitcode]unknown %s", err.Error()))
81-
}
82-
ch <- &types.RunAndWaitMessage{ContainerID: message.ContainerID, Data: exitData}
83-
log.Infof("[RunAndWait] Container %s finished, remove", message.ContainerID)
84-
}(node, message)
69+
rmids := []string{}
70+
for id, node := range ids {
71+
rmids = append(rmids, id)
72+
code, err := node.Engine.ContainerWait(context.Background(), id)
73+
exitData := []byte(fmt.Sprintf("[exitcode] %d", code))
74+
if err != nil {
75+
log.Errorf("%s run failed, %s", id[:12], err.Error())
76+
exitData = []byte(fmt.Sprintf("[exitcode]unknown %s", err.Error()))
77+
}
78+
ch <- &types.RunAndWaitMessage{ContainerID: id, Data: exitData}
79+
log.Infof("[RunAndWait] Container %s finished, remove", id[:12])
8580
}
81+
go c.RemoveContainer(rmids)
8682
}()
8783

8884
return ch, nil

0 commit comments

Comments
 (0)