Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(executor): clarify why use different contexts #304

Merged
merged 4 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cmd/vela-worker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,21 @@ func (w *Worker) exec(index int) error {
t = time.Duration(item.Repo.GetTimeout()) * time.Minute
}

// create a background context
// create a build context (from a background context
// so that other builds can't inadvertently cancel this build)
buildCtx, done := context.WithCancel(context.Background())
defer done()

// add to the background context with a timeout
// built in for ensuring a build doesn't run forever
ctx, timeout := context.WithTimeout(buildCtx, t)
timeoutCtx, timeout := context.WithTimeout(buildCtx, t)
defer timeout()

defer func() {
logger.Info("destroying build")
// destroy the build with the executor

// destroy the build with the executor (pass a background
// context to guarantee all build resources are destroyed).
err = _executor.DestroyBuild(context.Background())
if err != nil {
logger.Errorf("unable to destroy build: %v", err)
Expand All @@ -115,15 +118,15 @@ func (w *Worker) exec(index int) error {

logger.Info("creating build")
// create the build with the executor
err = _executor.CreateBuild(ctx)
err = _executor.CreateBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to create build: %v", err)
return nil
}

logger.Info("planning build")
// plan the build with the executor
err = _executor.PlanBuild(ctx)
err = _executor.PlanBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to plan build: %v", err)
return nil
Expand All @@ -141,15 +144,15 @@ func (w *Worker) exec(index int) error {

logger.Info("assembling build")
// assemble the build with the executor
err = _executor.AssembleBuild(ctx)
err = _executor.AssembleBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to assemble build: %v", err)
return nil
}

logger.Info("executing build")
// execute the build with the executor
err = _executor.ExecBuild(ctx)
err = _executor.ExecBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to execute build: %v", err)
return nil
Expand Down
2 changes: 2 additions & 0 deletions cmd/vela-worker/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (w *Worker) operate(ctx context.Context) error {
return nil
default:
// exec operator subprocess to poll and execute builds
// (do not pass the context to avoid errors in one
// executor+build inadvertently canceling other builds)
// nolint: contextcheck // ignore passing context
err = w.exec(id)
if err != nil {
Expand Down