Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Update-With-Start API #370

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions early-return/starter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"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,46 @@
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{

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

View workflow job for this annotation

GitHub Actions / build-and-test

c.NewWithStartWorkflowOperation undefined (type client.Client has no field or method NewWithStartWorkflowOperation)
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{

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)

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

View workflow job for this annotation

GitHub Actions / build-and-test

undefined: client.UpdateWithStartWorkflowOptions (compile)
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.

// 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.

}
2 changes: 2 additions & 0 deletions early-return/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading