Skip to content

Commit

Permalink
use struct
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanos committed Oct 3, 2024
1 parent 509449f commit 3565530
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
12 changes: 6 additions & 6 deletions early-return/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (
"go.temporal.io/sdk/activity"
)

func InitTransaction(ctx context.Context, transactionId, fromAccount, toAccount string, amount float64) error {
func InitTransaction(ctx context.Context, tx Transaction) error {
logger := activity.GetLogger(ctx)
if fromAccount == "" {
if tx.FromAccount == "" {
return errors.New("invalid fromAccount")
}
if toAccount == "" {
if tx.ToAccount == "" {
return errors.New("invalid toAccount")
}
if amount == 0 {
if tx.Amount == 0 {
return errors.New("invalid amount")
}
logger.Info("Transaction initialized")
return nil
}

func CancelTransaction(ctx context.Context, transactionId string) {
func CancelTransaction(ctx context.Context, tx Transaction) {
logger := activity.GetLogger(ctx)
logger.Info("Transaction cancelled")
}

func CompleteTransaction(ctx context.Context, transactionId string) {
func CompleteTransaction(ctx context.Context, tx Transaction) {
logger := activity.GetLogger(ctx)
logger.Info("Transaction completed")
}
6 changes: 3 additions & 3 deletions early-return/starter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func main() {
WaitForStage: client.WorkflowUpdateStageCompleted,
})

txId := uuid.New()
tx := earlyreturn.Transaction{ID: uuid.New(), FromAccount: "Bob", ToAccount: "Alice", Amount: 100.0}
workflowOptions := client.StartWorkflowOptions{
ID: "early-return-workflow-ID-" + txId,
ID: "early-return-workflow-ID-" + tx.ID,
TaskQueue: earlyreturn.TaskQueueName,
WithStartOperation: updateOperation,
}
we, err := c.ExecuteWorkflow(ctxWithTimeout, workflowOptions, earlyreturn.Workflow, txId, "bob", "alice", 100.0)
we, err := c.ExecuteWorkflow(ctxWithTimeout, workflowOptions, earlyreturn.Workflow, tx)
if err != nil {
log.Fatalln("Error executing workflow:", err)
}
Expand Down
17 changes: 11 additions & 6 deletions early-return/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ var (
earlyReturnTimeout = 5 * time.Second
)

type Transaction struct {
ID string
FromAccount string
ToAccount string
Amount float64
}

// Workflow processes a transaction in two phases. First, the transaction is initialized, and if successful,
// it proceeds to completion. However, if initialization fails - due to validation errors or transient
// issues (e.g., network connectivity problems) - the transaction is cancelled.
//
// By utilizing Update-with-Start, the client can initiate the workflow and immediately receive the result of
// the initialization in a single round trip, even before the transaction processing completes. The remainder
// of the transaction is then processed asynchronously.
func Workflow(ctx workflow.Context, transactionId, fromAccount, toAccount string, amount float64) error {
func Workflow(ctx workflow.Context, tx Transaction) error {
var initErr error
var initDone bool
logger := workflow.GetLogger(ctx)
Expand Down Expand Up @@ -54,9 +61,7 @@ func Workflow(ctx workflow.Context, transactionId, fromAccount, toAccount string
activityOptions := workflow.WithLocalActivityOptions(ctx, workflow.LocalActivityOptions{
ScheduleToCloseTimeout: activityTimeout,
})
initErr = workflow.ExecuteLocalActivity(
activityOptions, InitTransaction, transactionId, fromAccount, toAccount, amount,
).Get(ctx, nil)
initErr = workflow.ExecuteLocalActivity(activityOptions, InitTransaction, tx).Get(ctx, nil)
initDone = true

// Phase 2: Complete or cancel the transaction asychronously.
Expand All @@ -67,11 +72,11 @@ func Workflow(ctx workflow.Context, transactionId, fromAccount, toAccount string
logger.Info("cancelling transaction due to error: %v", initErr)

// Transaction failed to be initialized or not quickly enough; cancel the transaction.
return workflow.ExecuteActivity(activityCtx, CancelTransaction, transactionId).Get(ctx, nil)
return workflow.ExecuteActivity(activityCtx, CancelTransaction, tx).Get(ctx, nil)
}

logger.Info("completing transaction")

// Transaction was initialized successfully; complete the transaction.
return workflow.ExecuteActivity(activityCtx, CompleteTransaction, transactionId).Get(ctx, nil)
return workflow.ExecuteActivity(activityCtx, CompleteTransaction, tx).Get(ctx, nil)
}

0 comments on commit 3565530

Please sign in to comment.