Skip to content

controller: finish remote controller when all session finished #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion controller/local/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
func NewLocalBuildxController(ctx context.Context, dockerCli command.Cli, logger progress.SubLogger) control.BuildxController {
return &localController{
dockerCli: dockerCli,
ref: "local",
processes: processes.NewManager(),
}
}
Expand All @@ -48,6 +47,7 @@ func (b *localController) Build(ctx context.Context, options controllerapi.Build
}
defer b.buildOnGoing.Store(false)

b.ref = "local"
resp, res, buildErr := cbuild.RunBuild(ctx, b.dockerCli, options, in, progress, true)
// NOTE: RunBuild can return *build.ResultHandle even on error.
if res != nil {
Expand Down Expand Up @@ -121,6 +121,7 @@ func (b *localController) Kill(context.Context) error {
}

func (b *localController) Close() error {
b.ref = ""
b.cancelRunningProcesses()
if b.buildConfig.resultCtx != nil {
b.buildConfig.resultCtx.Done()
Expand All @@ -130,6 +131,9 @@ func (b *localController) Close() error {
}

func (b *localController) List(ctx context.Context) (res []string, _ error) {
if b.ref == "" {
return nil, nil
}
return []string{b.ref}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions controller/remote/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ func serveCmd(dockerCli command.Cli) *cobra.Command {
return cbuild.RunBuild(ctx, dockerCli, *options, stdin, progress, true)
})
defer b.Close()
sessionsDoneCh := make(chan struct{})
go func() {
for {
if !b.isUsed() {
sessionsDoneCh <- struct{}{}
break
}
time.Sleep(10 * time.Second)
}
}()

// serve server
addr := filepath.Join(root, defaultSocketFilename)
Expand Down Expand Up @@ -187,6 +197,9 @@ func serveCmd(dockerCli command.Cli) *cobra.Command {
signal.Notify(sigCh, syscall.SIGINT)
signal.Notify(sigCh, syscall.SIGTERM)
select {
case <-sessionsDoneCh:
logrus.Infof("all sessions finished")
return nil
case err := <-errCh:
logrus.Errorf("got error %s, exiting", err)
return err
Expand Down
Loading