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 66fe964
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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))
}

0 comments on commit 66fe964

Please sign in to comment.