Skip to content

Commit 740276f

Browse files
committed
handle "stop" event
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 40bca10 commit 740276f

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

cmd/convert.go renamed to cmd/compatibility/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package compatibility
1818

1919
import (
2020
"fmt"
@@ -43,7 +43,7 @@ func getStringFlags() []string {
4343
}
4444
}
4545

46-
func convert(args []string) []string {
46+
func Convert(args []string) []string {
4747
var rootFlags []string
4848
command := []string{compose.PluginName}
4949
l := len(args)

cmd/convert_test.go renamed to cmd/compatibility/convert_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package compatibility
1818

1919
import (
2020
"testing"
@@ -71,7 +71,7 @@ func Test_convert(t *testing.T) {
7171
}
7272
for _, tt := range tests {
7373
t.Run(tt.name, func(t *testing.T) {
74-
got := convert(tt.args)
74+
got := Convert(tt.args)
7575
assert.DeepEqual(t, tt.want, got)
7676
})
7777
}

cmd/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/cli/cli/command"
2626
"github.com/spf13/cobra"
2727

28+
"github.com/docker/compose/v2/cmd/compatibility"
2829
commands "github.com/docker/compose/v2/cmd/compose"
2930
"github.com/docker/compose/v2/internal"
3031
"github.com/docker/compose/v2/pkg/api"
@@ -68,7 +69,7 @@ func pluginMain() {
6869

6970
func main() {
7071
if commands.RunningAsStandalone() {
71-
os.Args = append([]string{"docker"}, convert(os.Args[1:])...)
72+
os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
7273
}
7374
pluginMain()
7475
}

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ const (
436436
ContainerEventLog = iota
437437
// ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container
438438
ContainerEventAttach
439+
// ContainerEventStopped is a ContainerEvent of type stopped.
440+
ContainerEventStopped
439441
// ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
440442
ContainerEventExit
441443
// UserCancel user cancelled compose up, we are stopping containers

pkg/compose/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string
7979
}
8080
containers[container] = struct{}{}
8181
p.consumer.Register(container)
82-
case api.ContainerEventExit:
82+
case api.ContainerEventExit, api.ContainerEventStopped:
8383
if !event.Restarting {
8484
delete(containers, container)
8585
}

pkg/compose/start.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ func (s *composeService) watchContainers(ctx context.Context, projectName string
111111
}
112112
name := getContainerNameWithoutProject(container)
113113

114+
if event.Status == "stop" {
115+
listener(api.ContainerEvent{
116+
Type: api.ContainerEventStopped,
117+
Container: name,
118+
Service: container.Labels[api.ServiceLabel],
119+
})
120+
121+
delete(watched, container.ID)
122+
if len(watched) == 0 {
123+
// all project containers stopped, we're done
124+
stop()
125+
}
126+
return nil
127+
}
128+
114129
if event.Status == "die" {
115130
restarted := watched[container.ID]
116131
watched[container.ID] = restarted + 1

0 commit comments

Comments
 (0)