diff --git a/early-return/starter/main.go b/early-return/starter/main.go index ad54eb5d..f2c86d4e 100644 --- a/early-return/starter/main.go +++ b/early-return/starter/main.go @@ -7,6 +7,7 @@ import ( "github.com/pborman/uuid" "github.com/temporalio/samples-go/early-return" + enumspb "go.temporal.io/api/enums/v1" "go.temporal.io/sdk/client" ) @@ -20,31 +21,34 @@ func main() { ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - updateOperation := client.NewUpdateWithStartWorkflowOperation( - client.UpdateWorkflowOptions{ - UpdateName: earlyreturn.UpdateName, - WaitForStage: client.WorkflowUpdateStageCompleted, - }) - tx := earlyreturn.Transaction{ID: uuid.New(), SourceAccount: "Bob", TargetAccount: "Alice", Amount: 100} - workflowOptions := client.StartWorkflowOptions{ - ID: "early-return-workflow-ID-" + tx.ID, - TaskQueue: earlyreturn.TaskQueueName, - WithStartOperation: updateOperation, - } - we, err := c.ExecuteWorkflow(ctxWithTimeout, workflowOptions, earlyreturn.Workflow, tx) - if err != nil { - log.Fatalln("Error executing workflow:", err) - } - log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID()) + startWorkflowOp := c.NewWithStartWorkflowOperation(client.StartWorkflowOptions{ + ID: "early-return-workflow-ID-" + tx.ID, + // WorkflowIDConflictPolicy is required when using + // UpdateWithStartWorkflow. Here we use FAIL, because we do not expect + // this workflow ID to exist already, and so we want an error if it + // does. + WorkflowIDConflictPolicy: enumspb.WORKFLOW_ID_CONFLICT_POLICY_FAIL, + TaskQueue: earlyreturn.TaskQueueName, + }, earlyreturn.Workflow, tx) - updateHandle, err := updateOperation.Get(ctxWithTimeout) + updateHandle, err := c.UpdateWithStartWorkflow(ctxWithTimeout, client.UpdateWithStartWorkflowOptions{ + UpdateOptions: client.UpdateWorkflowOptions{ + UpdateName: earlyreturn.UpdateName, + WaitForStage: client.WorkflowUpdateStageCompleted, + }, + StartWorkflowOperation: startWorkflowOp, + }) if err != nil { - log.Fatalln("Error obtaining update handle:", err) + // For example, a client-side validation error (e.g. missing conflict + // policy or invalid workflow argument types in the start operation), or + // a server-side failure (e.g. failed to start workflow, or exceeded + // limit on concurrent update per workflow execution). + log.Fatalln("Error issuing update-with-start:", err) } - - err = updateHandle.Get(ctxWithTimeout, nil) + var earlyReturnResult any + err = updateHandle.Get(ctxWithTimeout, &earlyReturnResult) if err != nil { // The workflow will continue running, cancelling the transaction. @@ -52,6 +56,11 @@ func main() { log.Fatalln("Error obtaining update result:", err) } - log.Println("Transaction initialized successfully") + workflowRun, err := startWorkflowOp.Get(ctxWithTimeout) + if err != nil { + log.Fatalln("Error obtaining workflow run:", err) + } + log.Println("Transaction initialized successfully, with runID:", workflowRun.GetRunID()) // The workflow will continue running, completing the transaction. + } diff --git a/early-return/workflow.go b/early-return/workflow.go index 2ea47762..80e401d9 100644 --- a/early-return/workflow.go +++ b/early-return/workflow.go @@ -36,6 +36,8 @@ func Workflow(ctx workflow.Context, tx Transaction) error { return run(ctx, tx) } +// TODO: workflow should return final Transaction summary, and early return +// should return some sort of transaction conformation. func run(ctx workflow.Context, tx Transaction) error { logger := workflow.GetLogger(ctx)