Skip to content
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

feat: allow remote cancelation of builds #290

Merged
merged 1 commit into from
Jun 23, 2024
Merged
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
3 changes: 3 additions & 0 deletions pkg/buildx/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,9 @@ func rewriteFriendlyErrors(err error) error {
simplified := re.ReplaceAllString(err.Error(), "")
return errors.New(simplified + ". Please check if the files exist in the context.")
}
if strings.Contains(err.Error(), "code = Canceled desc = grpc: the client connection is closing") {
return errors.New("build canceled")
}
return err
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/protobuf/types/known/timestamppb"
)

type Machine struct {
Expand Down Expand Up @@ -114,14 +115,19 @@ func (m *Machine) ReportHealth() error {

client := api.NewBuildClient()
for {
err := m.doReportHealth(context.Background(), client, builderPlatform)
cancelAt, err := m.doReportHealth(context.Background(), client, builderPlatform)
if err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
fmt.Printf("error reporting health: %s", err.Error())
client = api.NewBuildClient()
}

// If canceling the build was requested, release the machine to interrupt the build step.
if cancelAt != nil && time.Now().After(cancelAt.AsTime()) {
_ = m.Release()
}
select {
case <-time.After(5 * time.Second):
case <-m.reportHealthDone:
Expand All @@ -130,12 +136,15 @@ func (m *Machine) ReportHealth() error {
}
}

func (m *Machine) doReportHealth(ctx context.Context, client cliv1connect.BuildServiceClient, builderPlatform cliv1.BuilderPlatform) error {
func (m *Machine) doReportHealth(ctx context.Context, client cliv1connect.BuildServiceClient, builderPlatform cliv1.BuilderPlatform) (*timestamppb.Timestamp, error) {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
req := cliv1.ReportBuildHealthRequest{BuildId: m.BuildID, Platform: builderPlatform}
_, err := client.ReportBuildHealth(ctx, api.WithAuthentication(connect.NewRequest(&req), m.Token))
return err
res, err := client.ReportBuildHealth(ctx, api.WithAuthentication(connect.NewRequest(&req), m.Token))
if err != nil {
return nil, err
}
return res.Msg.GetCancelsAt(), nil
}

func (m *Machine) Release() error {
Expand Down
Loading
Loading