You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2019/12/18 12:34:51 UTC

[camel-k] 06/13: fix(build): Fix build routine execution

This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit e5b2c1ed9cf8060822bef8e35dfd18681a05cef4
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Tue Dec 17 12:18:35 2019 +0100

    fix(build): Fix build routine execution
---
 pkg/controller/build/schedule_routine.go | 71 ++++++++++++++++++--------------
 1 file changed, 40 insertions(+), 31 deletions(-)

diff --git a/pkg/controller/build/schedule_routine.go b/pkg/controller/build/schedule_routine.go
index 6535687..f4abe59 100644
--- a/pkg/controller/build/schedule_routine.go
+++ b/pkg/controller/build/schedule_routine.go
@@ -91,42 +91,51 @@ func (action *scheduleRoutineAction) Handle(ctx context.Context, build *v1alpha1
 	}
 
 	// Start the build asynchronously to avoid blocking the reconcile loop
-	go func() {
-		defer action.routines.Delete(build.Name)
+	action.routines.Store(build.Name, true)
 
-		status := v1alpha1.BuildStatus{
-			Phase:     v1alpha1.BuildPhaseRunning,
-			StartedAt: metav1.Now(),
-		}
-		if err := action.updateBuildStatus(ctx, build, status); err != nil {
-			return
-		}
+	go action.runBuild(ctx, build)
 
-		for i, task := range build.Spec.Tasks {
-			if task.Builder == nil {
-				status := v1alpha1.BuildStatus{
-					// Error the build directly as we know recovery won't work over ill-defined tasks
-					Phase: v1alpha1.BuildPhaseError,
-					Error: fmt.Sprintf("task cannot be executed using the routine strategy: %s", task.GetName()),
-				}
-				if err := action.updateBuildStatus(ctx, build, status); err != nil {
-					break
-				}
-			} else {
-				status := action.builder.Run(*task.Builder)
-				if i == len(build.Spec.Tasks)-1 {
-					status.Duration = metav1.Now().Sub(build.Status.StartedAt.Time).String()
-				}
-				if err := action.updateBuildStatus(ctx, build, status); err != nil {
-					break
-				}
+	return nil, nil
+}
+
+func (action *scheduleRoutineAction) runBuild(ctx context.Context, build *v1alpha1.Build) {
+	defer action.routines.Delete(build.Name)
+
+	status := v1alpha1.BuildStatus{
+		Phase:     v1alpha1.BuildPhaseRunning,
+		StartedAt: metav1.Now(),
+	}
+	if err := action.updateBuildStatus(ctx, build, status); err != nil {
+		return
+	}
+
+	for i, task := range build.Spec.Tasks {
+		if task.Builder == nil {
+			status := v1alpha1.BuildStatus{
+				// Error the build directly as we know recovery won't work over ill-defined tasks
+				Phase: v1alpha1.BuildPhaseError,
+				Error: fmt.Sprintf("task cannot be executed using the routine strategy: %s",
+					task.GetName()),
+				Duration: metav1.Now().Sub(build.Status.StartedAt.Time).String(),
 			}
+			_ = action.updateBuildStatus(ctx, build, status)
+			break
 		}
-	}()
-
-	action.routines.Store(build.Name, true)
 
-	return nil, nil
+		status := action.builder.Run(*task.Builder)
+		lastTask := i == len(build.Spec.Tasks)-1
+		taskFailed := status.Phase == v1alpha1.BuildPhaseFailed
+		if lastTask || taskFailed {
+			status.Duration = metav1.Now().Sub(build.Status.StartedAt.Time).String()
+		}
+		if lastTask && !taskFailed {
+			status.Phase = v1alpha1.BuildPhaseSucceeded
+		}
+		err := action.updateBuildStatus(ctx, build, status)
+		if err != nil || taskFailed {
+			break
+		}
+	}
 }
 
 func (action *scheduleRoutineAction) updateBuildStatus(ctx context.Context, build *v1alpha1.Build, status v1alpha1.BuildStatus) error {