handle "stop" event

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-12-08 17:59:48 +01:00 committed by Nicolas De loof
parent 40bca10250
commit 740276f550
6 changed files with 24 additions and 6 deletions

View File

@ -14,7 +14,7 @@
limitations under the License.
*/
package main
package compatibility
import (
"fmt"
@ -43,7 +43,7 @@ func getStringFlags() []string {
}
}
func convert(args []string) []string {
func Convert(args []string) []string {
var rootFlags []string
command := []string{compose.PluginName}
l := len(args)

View File

@ -14,7 +14,7 @@
limitations under the License.
*/
package main
package compatibility
import (
"testing"
@ -71,7 +71,7 @@ func Test_convert(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := convert(tt.args)
got := Convert(tt.args)
assert.DeepEqual(t, tt.want, got)
})
}

View File

@ -25,6 +25,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
"github.com/docker/compose/v2/cmd/compatibility"
commands "github.com/docker/compose/v2/cmd/compose"
"github.com/docker/compose/v2/internal"
"github.com/docker/compose/v2/pkg/api"
@ -68,7 +69,7 @@ func pluginMain() {
func main() {
if commands.RunningAsStandalone() {
os.Args = append([]string{"docker"}, convert(os.Args[1:])...)
os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
}
pluginMain()
}

View File

@ -436,6 +436,8 @@ const (
ContainerEventLog = iota
// ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container
ContainerEventAttach
// ContainerEventStopped is a ContainerEvent of type stopped.
ContainerEventStopped
// ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
ContainerEventExit
// UserCancel user cancelled compose up, we are stopping containers

View File

@ -79,7 +79,7 @@ func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string
}
containers[container] = struct{}{}
p.consumer.Register(container)
case api.ContainerEventExit:
case api.ContainerEventExit, api.ContainerEventStopped:
if !event.Restarting {
delete(containers, container)
}

View File

@ -111,6 +111,21 @@ func (s *composeService) watchContainers(ctx context.Context, projectName string
}
name := getContainerNameWithoutProject(container)
if event.Status == "stop" {
listener(api.ContainerEvent{
Type: api.ContainerEventStopped,
Container: name,
Service: container.Labels[api.ServiceLabel],
})
delete(watched, container.ID)
if len(watched) == 0 {
// all project containers stopped, we're done
stop()
}
return nil
}
if event.Status == "die" {
restarted := watched[container.ID]
watched[container.ID] = restarted + 1