Skip to main content

Introduction to Workflows

Temporal Workflows orchestrate your Activities.

They are resilient - they can run and keep running for years, even if the underlying infrastructure fails. If the application itself crashes, Temporal will automatically recreate its Workflow’s pre-failure state so it can continue right where it left off.

In our reimbursement example, if the network fails right after the withdrawal but before the deposit, Temporal recreates the Workflow Execution from its last known state and continues as if no failure ever occurred.

This Workflow orchestrates our two Activities.

import (
"fmt"
"time"

"go.temporal.io/sdk/workflow"
)

func ReimbursementWorkflow(ctx workflow.Context, userId string, amount float64) (string, error) {
options := workflow.ActivityOptions{
StartToCloseTimeout: time.Second * 5, // maximum time allowed for a single attempt of an Activity to execute
}
ctx = workflow.WithActivityOptions(ctx, options)

err := workflow.ExecuteActivity(ctx, WithdrawMoney, amount).Get(ctx, &withdrawSuccess)
if err != nil {
return "", err
}

err = workflow.ExecuteActivity(ctx, DepositMoney, amount).Get(ctx, &depositSuccess)
if err != nil {
return "", err
}
return fmt.Sprintf("reimbursement to %s successfully complete", userId), nil
}
4 / 9