*: add run cancel action

and remove unused change phase to finished
This commit is contained in:
Simone Gotti 2019-05-15 14:42:50 +02:00
parent 9cafc36a0d
commit bda7a3eb8b
2 changed files with 25 additions and 5 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/sorintlab/agola/internal/services/common" "github.com/sorintlab/agola/internal/services/common"
rsapi "github.com/sorintlab/agola/internal/services/runservice/api" 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/sorintlab/agola/internal/util"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -104,6 +105,7 @@ type RunActionType string
const ( const (
RunActionTypeRestart RunActionType = "restart" RunActionTypeRestart RunActionType = "restart"
RunActionTypeCancel RunActionType = "cancel"
RunActionTypeStop RunActionType = "stop" RunActionTypeStop RunActionType = "stop"
) )
@ -140,6 +142,17 @@ func (h *ActionHandler) RunAction(ctx context.Context, req *RunActionsRequest) (
return nil, ErrFromRemote(resp, err) 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: case RunActionTypeStop:
rsreq := &rsapi.RunActionsRequest{ rsreq := &rsapi.RunActionsRequest{
ActionType: rsapi.RunActionTypeStop, ActionType: rsapi.RunActionTypeStop,

View File

@ -76,18 +76,25 @@ func (h *ActionHandler) ChangeRunPhase(ctx context.Context, req *RunChangePhaseR
switch req.Phase { switch req.Phase {
case types.RunPhaseRunning: case types.RunPhaseRunning:
if r.Phase != types.RunPhaseQueued { 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) r.ChangePhase(types.RunPhaseRunning)
runEvent, err = common.NewRunEvent(ctx, h.e, r.ID, r.Phase, r.Result) runEvent, err = common.NewRunEvent(ctx, h.e, r.ID, r.Phase, r.Result)
if err != nil { if err != nil {
return err return err
} }
case types.RunPhaseFinished: case types.RunPhaseCancelled:
if r.Phase != types.RunPhaseRunning { if r.Phase != types.RunPhaseQueued {
return errors.Errorf("run %s is not running 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.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) _, err = store.AtomicPutRun(ctx, h.e, r, runEvent, cgt)