Skip to content

Commit

Permalink
refactor(executor): clarify why use different contexts (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd authored Apr 30, 2022
1 parent 29cfaa0 commit 27554a6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
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

0 comments on commit 27554a6

Please sign in to comment.