Skip to content

Commit

Permalink
fix cancel build to cancel pending stages
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Feb 4, 2024
1 parent 908bf73 commit 77c0440
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 3 additions & 1 deletion core/handler/server/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package server

import (
"context"
"errors"
"net/http"
"strconv"

"github.com/99nil/gopkg/ctr"
"github.com/99nil/gopkg/sse"
"gorm.io/gorm"

"github.com/zc2638/ink/core/handler/wrapper"
storageV1 "github.com/zc2638/ink/pkg/api/storage/v1"
Expand Down Expand Up @@ -62,7 +64,7 @@ func logInfo() http.HandlerFunc {
}
logS := new(storageV1.Log)
logS.SetID(stepS.ID)
if err := db.Where(logS).First(logS).Error; err != nil {
if err := db.Where(logS).First(logS).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
wrapper.InternalError(w, err)
return
}
Expand Down
38 changes: 38 additions & 0 deletions core/service/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"slices"

"github.com/99nil/gopkg/sets"

"github.com/zc2638/ink/core/scheduler"
"gorm.io/gorm"

Expand Down Expand Up @@ -272,6 +274,42 @@ func (s *srv) Cancel(ctx context.Context, namespace, name string, number uint64)
return errors.New("already done")
}

var stages []storageV1.Stage
if err := db.Where(&storageV1.Stage{
BoxID: buildS.BoxID,
BuildID: buildS.ID,
Phase: v1.PhasePending.String(),
}).Find(&stages).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("find pending stages failed: %v", err)
}

stageIDSet := sets.New[uint64]()
for _, v := range stages {
stageIDSet.Add(v.ID)
}
stageIds := stageIDSet.List()

if len(stageIds) > 0 {
err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&storageV1.Stage{}).
Where("id in (?)", stageIds).
Where(&storageV1.Stage{Phase: v1.PhasePending.String()}).
Update("phase", v1.PhaseCanceled).Error; err != nil {
return fmt.Errorf("cancel pending stages failed: %v", err)
}
if err := tx.Model(&storageV1.Step{}).
Where("stage_id in (?)", stageIds).
Where(&storageV1.Step{Phase: v1.PhasePending.String()}).
Update("phase", v1.PhaseCanceled).Error; err != nil {
return fmt.Errorf("cancel pending steps failed: %v", err)
}
return nil
})
if err != nil {
return err
}
}

sched := scheduler.FromContext(ctx)
return sched.Cancel(ctx, int64(build.ID))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/zc2638/ink
go 1.21.0

require (
github.com/99nil/gopkg v0.0.0-20240131094835-25a1aa9ff089
github.com/99nil/gopkg v0.0.0-20240202180301-972815b49f29
github.com/docker/distribution v2.8.2+incompatible
github.com/docker/docker v24.0.5+incompatible
github.com/docker/go-units v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
github.com/99nil/gopkg v0.0.0-20240131094835-25a1aa9ff089 h1:9xHZRHLMtBq2R8GLL4MqAtKlwrssBDaZpNbp3QLE6pk=
github.com/99nil/gopkg v0.0.0-20240131094835-25a1aa9ff089/go.mod h1:yQy7Xa2r1VHYfgqfCuDE6VGDmBt8xJgDVTZ2Bw3xTW0=
github.com/99nil/gopkg v0.0.0-20240202180301-972815b49f29 h1:x6ZHT+3qjWgk3ZwHsAnMc7SXupNlWUNqvrJYVjIi+kQ=
github.com/99nil/gopkg v0.0.0-20240202180301-972815b49f29/go.mod h1:yQy7Xa2r1VHYfgqfCuDE6VGDmBt8xJgDVTZ2Bw3xTW0=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20210715213245-6c3934b029d8/go.mod h1:CzsSbkDixRphAF5hS6wbMKq0eI6ccJRb7/A0M6JBnwg=
github.com/Azure/azure-pipeline-go v0.2.3/go.mod h1:x841ezTBIMG6O3lAcl8ATHnsOPVl2bqk7S3ta6S6u4k=
github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
Expand Down

0 comments on commit 77c0440

Please sign in to comment.