Skip to content

Commit

Permalink
New Update-With-Start API
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Nov 27, 2024
1 parent c69dc0b commit 896625e
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions early-return/starter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -20,38 +21,39 @@ 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)

startWorkflowOp, err := c.NewStartWorkflowOperation(client.StartWorkflowOptions{

Check failure on line 26 in early-return/starter/main.go

View workflow job for this annotation

GitHub Actions / build-and-test

c.NewStartWorkflowOperation undefined (type client.Client has no field or method NewStartWorkflowOperation)
ID: "early-return-workflow-ID-" + tx.ID,
WorkflowIDConflictPolicy: enumspb.WORKFLOW_ID_CONFLICT_POLICY_FAIL,
TaskQueue: earlyreturn.TaskQueueName,
}, earlyreturn.Workflow, tx)
if err != nil {
log.Fatalln("Error executing workflow:", err)
// E.g. workflow arguments do not match workflow definition.
log.Fatalln("Error creating start workflow operation:", err)
}

log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())

updateHandle, err := updateOperation.Get(ctxWithTimeout)
updateHandle, err := c.UpdateWithStartWorkflow(ctxWithTimeout, client.UpdateWorkflowOptions{

Check failure on line 36 in early-return/starter/main.go

View workflow job for this annotation

GitHub Actions / build-and-test

c.UpdateWithStartWorkflow undefined (type client.Client has no field or method UpdateWithStartWorkflow) (compile)
UpdateName: earlyreturn.UpdateName,
WaitForStage: client.WorkflowUpdateStageCompleted,
}, startWorkflowOp)
if err != nil {
log.Fatalln("Error obtaining update handle:", err)
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.

// NOTE: If the error is retryable, a retry attempt must use a unique workflow ID.
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.

}

0 comments on commit 896625e

Please sign in to comment.