diff --git a/internal/services/gateway/action/run.go b/internal/services/gateway/action/run.go index 9df5227..e49a82b 100644 --- a/internal/services/gateway/action/run.go +++ b/internal/services/gateway/action/run.go @@ -21,6 +21,7 @@ import ( "github.com/sorintlab/agola/internal/services/common" rsapi "github.com/sorintlab/agola/internal/services/runservice/api" + rstypes "github.com/sorintlab/agola/internal/services/runservice/types" "github.com/sorintlab/agola/internal/util" "github.com/pkg/errors" @@ -104,6 +105,7 @@ type RunActionType string const ( RunActionTypeRestart RunActionType = "restart" + RunActionTypeCancel RunActionType = "cancel" RunActionTypeStop RunActionType = "stop" ) @@ -140,6 +142,17 @@ func (h *ActionHandler) RunAction(ctx context.Context, req *RunActionsRequest) ( return nil, ErrFromRemote(resp, err) } + case RunActionTypeCancel: + rsreq := &rsapi.RunActionsRequest{ + ActionType: rsapi.RunActionTypeChangePhase, + Phase: rstypes.RunPhaseCancelled, + } + + resp, err = h.runserviceClient.RunActions(ctx, req.RunID, rsreq) + if err != nil { + return nil, ErrFromRemote(resp, err) + } + case RunActionTypeStop: rsreq := &rsapi.RunActionsRequest{ ActionType: rsapi.RunActionTypeStop, diff --git a/internal/services/runservice/action/action.go b/internal/services/runservice/action/action.go index 670eb95..f67235c 100644 --- a/internal/services/runservice/action/action.go +++ b/internal/services/runservice/action/action.go @@ -76,18 +76,25 @@ func (h *ActionHandler) ChangeRunPhase(ctx context.Context, req *RunChangePhaseR switch req.Phase { case types.RunPhaseRunning: if r.Phase != types.RunPhaseQueued { - return errors.Errorf("run %s is not queued but in %q phase", r.ID, r.Phase) + return errors.Errorf("run %q is not queued but in %q phase", r.ID, r.Phase) } r.ChangePhase(types.RunPhaseRunning) runEvent, err = common.NewRunEvent(ctx, h.e, r.ID, r.Phase, r.Result) if err != nil { return err } - case types.RunPhaseFinished: - if r.Phase != types.RunPhaseRunning { - return errors.Errorf("run %s is not running but in %q phase", r.ID, r.Phase) + case types.RunPhaseCancelled: + if r.Phase != types.RunPhaseQueued { + return errors.Errorf("run %q is not queued but in %q phase", r.ID, r.Phase) } - r.Stop = true + r.ChangePhase(types.RunPhaseCancelled) + runEvent, err = common.NewRunEvent(ctx, h.e, r.ID, r.Phase, r.Result) + if err != nil { + return err + } + default: + return errors.Errorf("unsupport change phase %q", req.Phase) + } _, err = store.AtomicPutRun(ctx, h.e, r, runEvent, cgt)