Skip to content

Commit

Permalink
feat: allow remote cancelation of builds
Browse files Browse the repository at this point in the history
Builds can now have a timeout to cancel long running steps

Signed-off-by: Chris Goller <[email protected]>
  • Loading branch information
goller committed Jun 23, 2024
1 parent 20b637d commit 0d61e9b
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 258 deletions.
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()

Check failure on line 129 in pkg/machine/machine.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `m.Release` is not checked (errcheck)
}
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

0 comments on commit 0d61e9b

Please sign in to comment.