mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-03-22 06:45:03 +01: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:
@@ -8,6 +8,10 @@ type jobErrorContextKey string
|
||||
|
||||
const jobErrorContextKeyVal = jobErrorContextKey("job.error")
|
||||
|
||||
type jobCancelCtx string
|
||||
|
||||
const JobCancelCtxVal = jobCancelCtx("job.cancel")
|
||||
|
||||
// JobError returns the job error for current context if any
|
||||
func JobError(ctx context.Context) error {
|
||||
val := ctx.Value(jobErrorContextKeyVal)
|
||||
@@ -28,3 +32,35 @@ func WithJobErrorContainer(ctx context.Context) context.Context {
|
||||
container := map[string]error{}
|
||||
return context.WithValue(ctx, jobErrorContextKeyVal, container)
|
||||
}
|
||||
|
||||
func WithJobCancelContext(ctx context.Context, cancelContext context.Context) context.Context {
|
||||
return context.WithValue(ctx, JobCancelCtxVal, cancelContext)
|
||||
}
|
||||
|
||||
func JobCancelContext(ctx context.Context) context.Context {
|
||||
val := ctx.Value(JobCancelCtxVal)
|
||||
if val != nil {
|
||||
if container, ok := val.(context.Context); ok {
|
||||
return container
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EarlyCancelContext returns a new context based on ctx that is canceled when the first of the provided contexts is canceled.
|
||||
func EarlyCancelContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
val := JobCancelContext(ctx)
|
||||
if val != nil {
|
||||
context, cancel := context.WithCancel(ctx)
|
||||
go func() {
|
||||
defer cancel()
|
||||
select {
|
||||
case <-context.Done():
|
||||
case <-ctx.Done():
|
||||
case <-val.Done():
|
||||
}
|
||||
}()
|
||||
return context, cancel
|
||||
}
|
||||
return ctx, func() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user