mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-03-20 11:56:47 +08:00
feat: Support graceful job step cancellation (#69)
* for gh-act-runner * act-cli support as well * respecting always() and cancelled() of steps * setup-job, bug report, gh cli and watch wait call is cancelled early
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/actions-oss/act-cli/pkg/container"
|
||||
"github.com/actions-oss/act-cli/pkg/exprparser"
|
||||
"github.com/actions-oss/act-cli/pkg/model"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type step interface {
|
||||
@@ -84,6 +85,9 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
return err
|
||||
}
|
||||
|
||||
cctx := common.JobCancelContext(ctx)
|
||||
rc.Cancelled = cctx != nil && cctx.Err() != nil
|
||||
|
||||
runStep, err := isStepEnabled(ctx, ifExpression, step, stage)
|
||||
if err != nil {
|
||||
stepResult.Conclusion = model.StepStatusFailure
|
||||
@@ -139,9 +143,13 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
Mode: 0o666,
|
||||
})(ctx)
|
||||
|
||||
timeoutctx, cancelTimeOut := evaluateStepTimeout(ctx, rc.ExprEval, stepModel)
|
||||
stepCtx, cancelStepCtx := context.WithCancel(ctx)
|
||||
defer cancelStepCtx()
|
||||
var cancelTimeOut context.CancelFunc
|
||||
stepCtx, cancelTimeOut = evaluateStepTimeout(stepCtx, rc.ExprEval, stepModel)
|
||||
defer cancelTimeOut()
|
||||
err = executor(timeoutctx)
|
||||
monitorJobCancellation(ctx, stepCtx, cctx, rc, logger, ifExpression, step, stage, cancelStepCtx)
|
||||
err = executor(stepCtx)
|
||||
|
||||
if err == nil {
|
||||
logger.WithField("stepResult", stepResult.Outcome).Infof(" \u2705 Success - %s %s", stage, stepString)
|
||||
@@ -189,6 +197,24 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
}
|
||||
}
|
||||
|
||||
func monitorJobCancellation(ctx context.Context, stepCtx context.Context, jobCancellationCtx context.Context, rc *RunContext, logger logrus.FieldLogger, ifExpression string, step step, stage stepStage, cancelStepCtx context.CancelFunc) {
|
||||
if !rc.Cancelled && jobCancellationCtx != nil {
|
||||
go func() {
|
||||
select {
|
||||
case <-jobCancellationCtx.Done():
|
||||
rc.Cancelled = true
|
||||
logger.Infof("Reevaluate condition %v due to cancellation", ifExpression)
|
||||
keepStepRunning, err := isStepEnabled(ctx, ifExpression, step, stage)
|
||||
logger.Infof("Result condition keepStepRunning=%v", keepStepRunning)
|
||||
if !keepStepRunning || err != nil {
|
||||
cancelStepCtx()
|
||||
}
|
||||
case <-stepCtx.Done():
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func evaluateStepTimeout(ctx context.Context, exprEval ExpressionEvaluator, stepModel *model.Step) (context.Context, context.CancelFunc) {
|
||||
timeout := exprEval.Interpolate(ctx, stepModel.TimeoutMinutes)
|
||||
if timeout != "" {
|
||||
|
||||
Reference in New Issue
Block a user