chore(build): Cancel Build routine on timeout
@@ -33,7 +33,7 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/builder"
- camelevent "github.com/apache/camel-k/pkg/event"
+ "github.com/apache/camel-k/pkg/event"
"github.com/apache/camel-k/pkg/util/patch"
)
@@ -100,15 +100,18 @@ func (action *scheduleRoutineAction) Handle(ctx context.Context, build *v1.Build
// Start the build asynchronously to avoid blocking the reconcile loop
action.routines.Store(build.Name, true)
- // FIXME: Use context.WithTimeout
- go action.runBuild(ctx, build)
+ go action.runBuild(build)
return nil, nil
}
-func (action *scheduleRoutineAction) runBuild(ctx context.Context, build *v1.Build) {
+func (action *scheduleRoutineAction) runBuild(build *v1.Build) {
defer action.routines.Delete(build.Name)
+ ctx := context.Background()
+ ctxWithTimeout, cancel := context.WithTimeout(ctx, build.Spec.Timeout.Duration)
+ defer cancel()
+
now := metav1.Now()
status := v1.BuildStatus{
Phase: v1.BuildPhaseRunning,
@@ -121,6 +124,12 @@ func (action *scheduleRoutineAction) runBuild(ctx context.Context, build *v1.Bui
buildDir := ""
for i, task := range build.Spec.Tasks {
+ select {
+ case <-ctxWithTimeout.Done():
+ status.Phase = v1.BuildPhaseInterrupted
+ break
+
+ default:
// Coordinate the build and context directories across the sequence of tasks
if t := task.Builder; t != nil {
if t.BuildDir == "" {
@@ -149,7 +158,7 @@ func (action *scheduleRoutineAction) runBuild(ctx context.Context, build *v1.Bui
}
// Execute the task
- status = action.builder.Build(build).Task(task).Do(ctx)
+ status = action.builder.Build(build).Task(task).Do(ctxWithTimeout)
lastTask := i == len(build.Spec.Tasks)-1
taskFailed := status.Phase == v1.BuildPhaseFailed || status.Phase == v1.BuildPhaseError
@@ -169,6 +178,7 @@ func (action *scheduleRoutineAction) runBuild(ctx context.Context, build *v1.Bui
break
}
}
+ }
duration := metav1.Now().Sub(build.Status.StartedAt.Time)
status.Duration = duration.String()
@@ -197,7 +207,7 @@ func (action *scheduleRoutineAction) updateBuildStatus(ctx context.Context, buil
if target.Status.Phase != build.Status.Phase {
action.L.Info("Build state transition", "phase", target.Status.Phase)
}
- camelevent.NotifyBuildUpdated(ctx, action.client, action.recorder, build, target)
+ event.NotifyBuildUpdated(ctx, action.client, action.recorder, build, target)
build.Status = target.Status
return nil
}
