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

DImuthuUpe pushed a commit to branch workflow-pause-on-auth
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/workflow-pause-on-auth by this 
push:
     new 9bba89042b Cleaning up workflow execution status saving
9bba89042b is described below

commit 9bba89042baa879379123b52412ed9ee25f13d24
Author: DImuthuUpe <[email protected]>
AuthorDate: Sat Nov 7 00:46:17 2026 -0500

    Cleaning up workflow execution status saving
---
 api/process/service/launch.go                      |   9 --
 internal/app/app.go                                |   2 +-
 internal/orchestration/engine.go                   |  27 ++----
 internal/orchestration/workflows/outcome.go        |  82 ++++++++++++++++
 internal/orchestration/workflows/outcome_test.go   | 105 +++++++++++++++++++++
 internal/orchestration/workflows/workflows.go      |  25 ++---
 internal/orchestration/workflows/workflows_test.go |   2 +-
 7 files changed, 211 insertions(+), 41 deletions(-)

diff --git a/api/process/service/launch.go b/api/process/service/launch.go
index 22d3897e72..f2ed4bf40c 100644
--- a/api/process/service/launch.go
+++ b/api/process/service/launch.go
@@ -60,7 +60,6 @@ type LaunchService struct {
        scpDataStorageService  *datasev.SCPDataStorageService
        dataProductService     *datasev.DataProductService
        statusService          *StatusService
-       processWorkflowService *WorkflowService
 
        dataStagingTasks   *repository.DataStagingTaskRepository
        jobSubmissionTasks *repository.JobSubmissionTaskRepository
@@ -82,7 +81,6 @@ func NewLaunchService(
        scpDataStorageService *datasev.SCPDataStorageService,
        dataProductService *datasev.DataProductService,
        statusService *StatusService,
-       processWorkflowService *WorkflowService,
 
        dataStagingTasks *repository.DataStagingTaskRepository,
        jobSubmissionTasks *repository.JobSubmissionTaskRepository,
@@ -107,7 +105,6 @@ func NewLaunchService(
                monitoringTasks:        monitoringTasks,
                interactiveTasks:       interactiveTasks,
                executionEngine:        executionEngine,
-               processWorkflowService: processWorkflowService,
        }
 }
 
@@ -279,12 +276,6 @@ func (s *LaunchService) launchBatchProcess(ctx 
context.Context, process *dto.Res
                return err
        }
 
-       _, err = s.processWorkflowService.Record(ctx, process.ProcessID, 
workflowId, procmodel.ProcessWorkflowTypeBatchJobSubmission, 
procmodel.ProcessWorkflowStatusTypeRunning)
-
-       if err != nil {
-               slog.Error("Failed to record process workflow", "processId", 
process.ProcessID, "error", err)
-               return err
-       }
        slog.Info("Launched batch job submission", "processId", 
process.ProcessID, "workflowId", workflowId)
 
        if _, err := s.statusService.Record(ctx, process.ProcessID, 
procmodel.ProcessStatusTypeSubmitted, nil); err != nil {
diff --git a/internal/app/app.go b/internal/app/app.go
index 37e1b598af..9e2bf0d534 100644
--- a/internal/app/app.go
+++ b/internal/app/app.go
@@ -265,7 +265,7 @@ func NewServices(cfg config.Config, db *gorm.DB, repos 
*Repositories, workflowBa
 
                Launch: processsvc.NewLaunchService(db, processSvs, 
batchDeploymentSvc,
                        templateSvc, slurmClusterSvc, slurmClusterConfigSvc,
-                       sshCredentialSvc, scpDataStorageSvc, dataProductSvc, 
statusSvc, processWorkflowSvc, stagingTasks, submissionTasks,
+                       sshCredentialSvc, scpDataStorageSvc, dataProductSvc, 
statusSvc, stagingTasks, submissionTasks,
                        monitoringTasks, commandTasks, executionEngine),
 
                ExecutionEngine: executionEngine,
diff --git a/internal/orchestration/engine.go b/internal/orchestration/engine.go
index 234a77c20e..f75dd30a20 100644
--- a/internal/orchestration/engine.go
+++ b/internal/orchestration/engine.go
@@ -51,12 +51,11 @@ import (
 // ExecutionEngine runs the workflows that carry out a process, and starts 
them when a
 // process is launched or the scheduler reports its job has finished.
 type ExecutionEngine struct {
-       workflows        *workflows.Workflows
-       orchestrator     *worker.WorkflowOrchestrator
-       processes        *processrepo.ProcessRepository
-       batchStatus      *processrepo.BatchJobStatusRepository
-       processStatus    *processrepo.StatusRepository
-       processWorkflows *processrepo.WorkflowRepository
+       workflows     *workflows.Workflows
+       orchestrator  *worker.WorkflowOrchestrator
+       processes     *processrepo.ProcessRepository
+       batchStatus   *processrepo.BatchJobStatusRepository
+       processStatus *processrepo.StatusRepository
 }
 
 // NewExecutionEngine returns the workflow set scheduling acts.
@@ -93,11 +92,10 @@ func NewExecutionEngine(dataStagingTasks 
*processrepo.DataStagingTaskRepository,
                        dataStagingTasks, jobSubmissionTasks, 
jobMonitoringTasks, processStatus,
                        processWorkflows,
                ),
-               orchestrator:     orchestrator,
-               processes:        processes,
-               batchStatus:      batchStatus,
-               processStatus:    processStatus,
-               processWorkflows: processWorkflows,
+               orchestrator:  orchestrator,
+               processes:     processes,
+               batchStatus:   batchStatus,
+               processStatus: processStatus,
        }
 }
 
@@ -180,13 +178,6 @@ func (w *ExecutionEngine) HandleBatchJobEmailResponse(ctx 
context.Context, email
                        return nil
                }
 
-               // Update the process workflow repository to indicate that the 
job completion workflow is running
-               _, err = w.processWorkflows.Record(ctx, process.ID, workflowId, 
model.ProcessWorkflowTypeJobCompletion, model.ProcessWorkflowStatusTypeRunning)
-               if err != nil {
-                       slog.Error("Failed to record process workflow", 
"processID", process.ID, "error", err)
-                       return nil
-               }
-
        }
 
        if parsed.Status == model.BatchJobStatusBegin {
diff --git a/internal/orchestration/workflows/outcome.go 
b/internal/orchestration/workflows/outcome.go
new file mode 100644
index 0000000000..2d6d8a59c4
--- /dev/null
+++ b/internal/orchestration/workflows/outcome.go
@@ -0,0 +1,82 @@
+/**
+*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+ */
+
+package workflows
+
+import (
+       "context"
+       "log/slog"
+
+       workflow "github.com/cschleiden/go-workflows/workflow"
+
+       model "github.com/apache/airavata/api/process/model"
+)
+
+// recordingOutcome runs a workflow body between two records of it in the 
process's
+// workflow history: RUNNING before the body starts, then SUCCEEDED or FAILED 
by what
+// the body returns. A workflow fails exactly when its function returns an 
error, so
+// this is where every failure path of the body passes.
+//
+// RUNNING is recorded here rather than by whoever started the workflow: a 
caller
+// writing it after CreateWorkflowInstance could land after a fast workflow's 
final
+// status and leave the record RUNNING for good.
+//
+// A panic in the body is not seen here — the workflow engine recovers it 
above this
+// function — so a panicking workflow stays RUNNING.
+func (w *Workflows) recordingOutcome(ctx workflow.Context, processID string,
+       workflowType model.ProcessWorkflowType, body func() error) error {
+
+       w.recordStatus(ctx, processID, workflowType, 
model.ProcessWorkflowStatusTypeRunning)
+
+       err := body()
+
+       status := model.ProcessWorkflowStatusTypeSucceeded
+       if err != nil {
+               status = model.ProcessWorkflowStatusTypeFailed
+       }
+       // Disconnected, so the outcome is still recorded when the body ended 
because the
+       // workflow was cancelled.
+       w.recordStatus(workflow.NewDisconnectedContext(ctx), processID, 
workflowType, status)
+       return err
+}
+
+// recordStatus records the running workflow's status through 
RecordWorkflowStatus. A
+// failure to record is logged and otherwise ignored: the history describes 
the work,
+// and losing an entry of it is no reason to fail the work itself.
+func (w *Workflows) recordStatus(ctx workflow.Context, processID string,
+       workflowType model.ProcessWorkflowType, status 
model.ProcessWorkflowStatusType) {
+
+       workflowID := workflow.WorkflowInstance(ctx).InstanceID
+       if _, err := workflow.ExecuteActivity[any](ctx, 
workflow.DefaultActivityOptions,
+               w.RecordWorkflowStatus, processID, workflowID, workflowType, 
status).Get(ctx); err != nil {
+               slog.Error("Failed to record process workflow", "processId", 
processID,
+                       "workflowId", workflowID, "status", status, "error", 
err)
+       }
+}
+
+// RecordWorkflowStatus is the activity behind recordStatus. The write is an 
activity
+// rather than workflow code so that a replay reads its result from history 
instead of
+// writing again.
+func (w *Workflows) RecordWorkflowStatus(ctx context.Context, processID, 
workflowID string,
+       workflowType model.ProcessWorkflowType, status 
model.ProcessWorkflowStatusType) error {
+
+       _, err := w.processWorkflows.Record(ctx, processID, workflowID, 
workflowType, status)
+       return err
+}
diff --git a/internal/orchestration/workflows/outcome_test.go 
b/internal/orchestration/workflows/outcome_test.go
new file mode 100644
index 0000000000..27c65b4738
--- /dev/null
+++ b/internal/orchestration/workflows/outcome_test.go
@@ -0,0 +1,105 @@
+/**
+*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+ */
+
+package workflows
+
+import (
+       "context"
+       "errors"
+       "testing"
+
+       "github.com/cschleiden/go-workflows/tester"
+       workflow "github.com/cschleiden/go-workflows/workflow"
+       "github.com/glebarez/sqlite"
+       "gorm.io/gorm"
+       "gorm.io/gorm/logger"
+
+       model "github.com/apache/airavata/api/process/model"
+       processrepo "github.com/apache/airavata/api/process/repository"
+       "github.com/apache/airavata/internal/db"
+)
+
+// runRecorded runs a workflow whose body returns bodyErr under 
recordingOutcome, with
+// the real RecordWorkflowStatus activity writing to an in-memory database, 
and returns
+// the workflow's error and the record it left.
+func runRecorded(t *testing.T, bodyErr error) (error, model.ProcessWorkflow) {
+       t.Helper()
+       gdb, err := 
gorm.Open(sqlite.Open("file::memory:?_pragma=foreign_keys(1)"), &gorm.Config{
+               Logger: logger.Default.LogMode(logger.Silent),
+       })
+       if err != nil {
+               t.Fatalf("open sqlite: %v", err)
+       }
+       if err := db.AutoMigrate(gdb); err != nil {
+               t.Fatalf("automigrate: %v", err)
+       }
+       proc := &model.Process{}
+       if err := gdb.Create(proc).Error; err != nil {
+               t.Fatalf("create process: %v", err)
+       }
+
+       w := New(nil, nil, nil, nil, nil, nil, nil, 
processrepo.NewWorkflowRepository(gdb))
+       wf := func(ctx workflow.Context, processID string) error {
+               return w.recordingOutcome(ctx, processID, 
model.ProcessWorkflowTypeBatchJobSubmission,
+                       func() error { return bodyErr })
+       }
+
+       wft := tester.NewWorkflowTester[any](wf)
+       if err := wft.Registry().RegisterActivity(w.RecordWorkflowStatus); err 
!= nil {
+               t.Fatalf("register activity: %v", err)
+       }
+       wft.Execute(context.Background(), proc.ID)
+       if !wft.WorkflowFinished() {
+               t.Fatal("workflow did not finish")
+       }
+       _, wfErr := wft.WorkflowResult()
+
+       var rows []model.ProcessWorkflow
+       if err := gdb.Where("process_id = ?", proc.ID).Find(&rows).Error; err 
!= nil {
+               t.Fatalf("find process workflows: %v", err)
+       }
+       if len(rows) != 1 {
+               t.Fatalf("process workflows = %d, want one record updated in 
place", len(rows))
+       }
+       return wfErr, rows[0]
+}
+
+func TestRecordingOutcomeMarksSuccess(t *testing.T) {
+       wfErr, rec := runRecorded(t, nil)
+       if wfErr != nil {
+               t.Fatalf("workflow error = %v, want nil", wfErr)
+       }
+       if rec.Status == nil || *rec.Status != 
model.ProcessWorkflowStatusTypeSucceeded {
+               t.Errorf("status = %v, want SUCCEEDED", rec.Status)
+       }
+       if rec.WorkflowID == "" {
+               t.Error("record names no workflow instance")
+       }
+}
+
+func TestRecordingOutcomeMarksFailure(t *testing.T) {
+       wfErr, rec := runRecorded(t, errors.New("staging failed"))
+       if wfErr == nil {
+               t.Fatal("workflow error = nil, want the body's error passed 
through")
+       }
+       if rec.Status == nil || *rec.Status != 
model.ProcessWorkflowStatusTypeFailed {
+               t.Errorf("status = %v, want FAILED", rec.Status)
+       }
+}
diff --git a/internal/orchestration/workflows/workflows.go 
b/internal/orchestration/workflows/workflows.go
index f6482b7fbd..842b7386bf 100644
--- a/internal/orchestration/workflows/workflows.go
+++ b/internal/orchestration/workflows/workflows.go
@@ -88,7 +88,7 @@ func (w *Workflows) Register(r Registrar) error {
                        return err
                }
        }
-       for _, a := range []workflow.Activity{w.data.CopyData, 
w.batch.SubmitBatchJob, w.monitoring.MonitorBatchJob, w.CompleteProcess} {
+       for _, a := range []workflow.Activity{w.data.CopyData, 
w.batch.SubmitBatchJob, w.monitoring.MonitorBatchJob, w.CompleteProcess, 
w.RecordWorkflowStatus} {
                if err := r.RegisterActivity(a); err != nil {
                        return err
                }
@@ -99,6 +99,12 @@ func (w *Workflows) Register(r Registrar) error {
 // HandleBatchJobCompletion runs once the scheduler reports a job finished: it 
checks
 // the job's outcome, stages its outputs back and marks the process complete.
 func (w *Workflows) HandleBatchJobCompletion(ctx workflow.Context, processID 
string) error {
+       return w.recordingOutcome(ctx, processID, 
model.ProcessWorkflowTypeJobCompletion, func() error {
+               return w.handleBatchJobCompletion(ctx, processID)
+       })
+}
+
+func (w *Workflows) handleBatchJobCompletion(ctx workflow.Context, processID 
string) error {
        // Implement the logic for batch job completion workflow here
 
        ctxInt := context.Background()
@@ -170,18 +176,18 @@ func (w *Workflows) HandleBatchJobCompletion(ctx 
workflow.Context, processID str
                return err
        }
 
-       // record workflow completion
-       if _, err := w.processWorkflows.Record(ctxInt, processID, 
workflow.WorkflowInstance(ctx).InstanceID, 
model.ProcessWorkflowTypeJobCompletion, 
model.ProcessWorkflowStatusTypeSucceeded); err != nil {
-               slog.Error("Failed to record process workflow", "processId", 
processID, "error", err)
-               return err
-       }
-
        return nil
 }
 
 // HandleBatchJobSubmission stages a process's inputs onto its cluster and 
submits the
 // job. This is an async call and will not block the caller.
 func (w *Workflows) HandleBatchJobSubmission(ctx workflow.Context, processID 
string) error {
+       return w.recordingOutcome(ctx, processID, 
model.ProcessWorkflowTypeBatchJobSubmission, func() error {
+               return w.handleBatchJobSubmission(ctx, processID)
+       })
+}
+
+func (w *Workflows) handleBatchJobSubmission(ctx workflow.Context, processID 
string) error {
 
        ctxInt := context.Background()
 
@@ -244,11 +250,6 @@ func (w *Workflows) HandleBatchJobSubmission(ctx 
workflow.Context, processID str
                return err
        }
 
-       if _, err := w.processWorkflows.Record(ctxInt, processID, 
workflow.WorkflowInstance(ctx).InstanceID, 
model.ProcessWorkflowTypeBatchJobSubmission, 
model.ProcessWorkflowStatusTypeSucceeded); err != nil {
-               slog.Error("Failed to record process workflow", "processId", 
processID, "error", err)
-               return err
-       }
-
        return nil
 }
 
diff --git a/internal/orchestration/workflows/workflows_test.go 
b/internal/orchestration/workflows/workflows_test.go
index 573c36e720..291e6b4254 100644
--- a/internal/orchestration/workflows/workflows_test.go
+++ b/internal/orchestration/workflows/workflows_test.go
@@ -39,7 +39,7 @@ func TestRegister(t *testing.T) {
                        t.Errorf("workflow %s: %v", name, err)
                }
        }
-       for _, name := range []string{"CopyData", "SubmitBatchJob", 
"MonitorBatchJob", "CompleteProcess"} {
+       for _, name := range []string{"CopyData", "SubmitBatchJob", 
"MonitorBatchJob", "CompleteProcess", "RecordWorkflowStatus"} {
                if _, err := r.GetActivity(name); err != nil {
                        t.Errorf("activity %s: %v", name, err)
                }

Reply via email to