Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/scripts/run-design-pattern.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ for _ in $(seq 1 60); do
done
temporal operator cluster health --address "localhost:$port" >/dev/null 2>&1 || fail "Temporal dev server didn't become healthy"

# Samples load their connection settings from the environment configuration, so
# point them at this run's dev server.
export TEMPORAL_ADDRESS="localhost:$port"

cd "$workdir"
if [ "${SKIP_SETUP:-}" != "1" ]; then
echo "== $name: setup"
Expand Down
97 changes: 96 additions & 1 deletion .github/workflows/run-design-patterns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ on:
- "*-sandbox/**/design-patterns/**"
- "*-sandbox/package.json"
- "*-sandbox/package-lock.json"
- "go-sandbox/go.mod"
- "go-sandbox/go.sum"
- "typescript-sandbox/src/mocha/saga.test.ts"
- ".github/scripts/run-design-pattern.sh"
- ".github/workflows/run-design-patterns.yml"
push:
Expand Down Expand Up @@ -39,6 +42,11 @@ jobs:
cache-dependency-path: typescript-sandbox/package-lock.json
- name: Install Temporal CLI
uses: temporalio/setup-temporal@1059a504f87e7fa2f385e3fa40d1aa7e62f1c6ca # v0
- name: Unit tests
working-directory: typescript-sandbox
run: |
npm ci
npx mocha --exit --node-option no-experimental-strip-types --require ts-node/register --require source-map-support/register src/mocha/saga.test.ts
- name: Run each manifest
run: |
status=0
Expand All @@ -49,9 +57,96 @@ jobs:
done
exit $status

go:
name: Go
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
with:
go-version-file: go-sandbox/go.mod
cache-dependency-path: go-sandbox/go.sum
- name: Install Temporal CLI
uses: temporalio/setup-temporal@1059a504f87e7fa2f385e3fa40d1aa7e62f1c6ca # v0
- name: Unit tests
working-directory: go-sandbox
run: go test ./design-patterns/...
- name: Run each manifest
run: |
status=0
for manifest in $(find go-sandbox -path '*/design-patterns/*/runner.json' -print | sort); do
echo "::group::$manifest"
.github/scripts/run-design-pattern.sh "$manifest" || status=1
echo "::endgroup::"
done
exit $status

python:
name: Python
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: "3.13"
- run: python -m pip install uv
- name: Install Temporal CLI
uses: temporalio/setup-temporal@1059a504f87e7fa2f385e3fa40d1aa7e62f1c6ca # v0
- name: Unit tests
run: |
status=0
for project in $(find python-sandbox -path '*/.venv' -prune -o -path '*/design-patterns/*/pyproject.toml' -print | sort); do
dir="$(dirname "$project")"
echo "::group::$dir"
(cd "$dir" && uv run --frozen pytest -q) || status=1
echo "::endgroup::"
done
exit $status
- name: Run each manifest
run: |
status=0
for manifest in $(find python-sandbox -path '*/.venv' -prune -o -path '*/design-patterns/*/runner.json' -print | sort); do
echo "::group::$manifest"
.github/scripts/run-design-pattern.sh "$manifest" || status=1
echo "::endgroup::"
done
exit $status

java:
name: Java
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1
with:
distribution: temurin
java-version: "21"
cache: maven
cache-dependency-path: java-sandbox/design-patterns/*/pom.xml
- name: Install Temporal CLI
uses: temporalio/setup-temporal@1059a504f87e7fa2f385e3fa40d1aa7e62f1c6ca # v0
- name: Unit tests
run: |
status=0
for pom in java-sandbox/design-patterns/*/pom.xml; do
echo "::group::${pom%/pom.xml}"
mvn -B -f "$pom" test || status=1
echo "::endgroup::"
done
exit $status
- name: Run each manifest
run: |
status=0
for manifest in $(find java-sandbox -path '*/target' -prune -o -path '*/design-patterns/*/runner.json' -print | sort); do
echo "::group::$manifest"
.github/scripts/run-design-pattern.sh "$manifest" || status=1
echo "::endgroup::"
done
exit $status

report:
name: Report nightly failure
needs: [typescript]
needs: [typescript, go, python, java]
if: failure() && github.event_name == 'schedule'
runs-on: ubuntu-latest
permissions:
Expand Down
54 changes: 54 additions & 0 deletions go-sandbox/design-patterns/saga/activities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package saga

import (
"context"
"fmt"

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

// Forward steps. Each one would call a separate service in a real application.

func CreateAccount(ctx context.Context, req OpenAccountRequest) error {
activity.GetLogger(ctx).Info(fmt.Sprintf("Created account %s", req.AccountID))
return nil
}

func AddAddress(ctx context.Context, req OpenAccountRequest) error {
activity.GetLogger(ctx).Info(fmt.Sprintf("Added address %q to account %s", req.Address, req.AccountID))
return nil
}

func AddClient(ctx context.Context, req OpenAccountRequest) error {
activity.GetLogger(ctx).Info(fmt.Sprintf("Added client %s to account %s", req.ClientEmail, req.AccountID))
return nil
}

func AddBankAccount(ctx context.Context, req OpenAccountRequest) error {
// Fail on purpose so the Workflow runs its compensations.
// Non-retryable, so the failure reaches the Workflow without retry delays.
return temporal.NewNonRetryableApplicationError(
fmt.Sprintf("Bank rejected account %s", req.BankAccountID),
"BankAccountRejected",
nil,
)
}

// Compensations. Each one is idempotent and safe to run when its forward step
// never ran, because the Workflow registers it before running that step.

func ClearPostalAddresses(ctx context.Context, req OpenAccountRequest) error {
activity.GetLogger(ctx).Info(fmt.Sprintf("Compensation: cleared postal addresses for account %s", req.AccountID))
return nil
}

func RemoveClient(ctx context.Context, req OpenAccountRequest) error {
activity.GetLogger(ctx).Info(fmt.Sprintf("Compensation: removed client %s from account %s", req.ClientEmail, req.AccountID))
return nil
}

func DisconnectBankAccounts(ctx context.Context, req OpenAccountRequest) error {
activity.GetLogger(ctx).Info(fmt.Sprintf("Compensation: disconnected bank accounts from account %s", req.AccountID))
return nil
}
24 changes: 24 additions & 0 deletions go-sandbox/design-patterns/saga/runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"pattern": "saga",
"language": "go",
"workdir": "go-sandbox",
"files": [
"design-patterns/saga/workflow.go",
"design-patterns/saga/activities.go",
"design-patterns/saga/shared.go",
"design-patterns/saga/worker/main.go",
"design-patterns/saga/starter/main.go"
],
"setup": "go mod download && go build -o /dev/null ./design-patterns/saga/...",
"worker": "go run ./design-patterns/saga/worker",
"starter": "go run ./design-patterns/saga/starter",
"expect": {
"status": "FAILED",
"output": [
"Compensation: disconnected bank accounts",
"Compensation: removed client",
"Compensation: cleared postal addresses",
"Workflow failed after compensating: Bank rejected account"
]
}
}
34 changes: 34 additions & 0 deletions go-sandbox/design-patterns/saga/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package saga

import (
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/contrib/envconfig"
)

// TaskQueue is the Task Queue the Saga Worker polls.
const TaskQueue = "design-patterns-saga"

// OpenAccountRequest carries everything each step and compensation needs.
type OpenAccountRequest struct {
AccountID string `json:"accountId"`
Address string `json:"address"`
ClientEmail string `json:"clientEmail"`
BankAccountID string `json:"bankAccountId"`
}

// ClientOptions loads connection settings from the environment configuration
// (TEMPORAL_ADDRESS, TEMPORAL_PROFILE, or the TOML config file), and falls back
// to a local dev server when nothing is configured.
func ClientOptions() (client.Options, error) {
opts, err := envconfig.LoadDefaultClientOptions()
if err != nil {
return opts, err
}
if opts.HostPort == "" {
opts.HostPort = client.DefaultHostPort
}
if opts.Namespace == "" {
opts.Namespace = client.DefaultNamespace
}
return opts, nil
}
56 changes: 56 additions & 0 deletions go-sandbox/design-patterns/saga/starter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"math/rand"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/temporal"

"my-org/quickstart/design-patterns/saga"
)

func main() {
opts, err := saga.ClientOptions()
if err != nil {
log.Fatalln("Unable to load client options", err)
}
c, err := client.Dial(opts)
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

accountID := fmt.Sprintf("acct-%06d", rand.Intn(1_000_000))
req := saga.OpenAccountRequest{
AccountID: accountID,
Address: "1 Main St",
ClientEmail: "client@example.com",
BankAccountID: fmt.Sprintf("bank-%06d", rand.Intn(1_000_000)),
}

run, err := c.ExecuteWorkflow(context.Background(), client.StartWorkflowOptions{
ID: "open-account-" + accountID,
TaskQueue: saga.TaskQueue,
}, saga.OpenAccount, req)
if err != nil {
log.Fatalln("Unable to start Workflow", err)
}
fmt.Println("Started Workflow", run.GetID())

var result string
if err := run.Get(context.Background(), &result); err != nil {
// This sample fails on purpose, so report the failure instead of crashing.
// The cause chain is WorkflowExecutionError -> ActivityError -> ApplicationError.
var appErr *temporal.ApplicationError
if errors.As(err, &appErr) {
fmt.Println("Workflow failed after compensating:", appErr.Message())
return
}
log.Fatalln("Workflow failed", err)
}
fmt.Println(result)
}
36 changes: 36 additions & 0 deletions go-sandbox/design-patterns/saga/worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"log"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"

"my-org/quickstart/design-patterns/saga"
)

func main() {
opts, err := saga.ClientOptions()
if err != nil {
log.Fatalln("Unable to load client options", err)
}
c, err := client.Dial(opts)
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

w := worker.New(c, saga.TaskQueue, worker.Options{})
w.RegisterWorkflow(saga.OpenAccount)
w.RegisterActivity(saga.CreateAccount)
w.RegisterActivity(saga.AddAddress)
w.RegisterActivity(saga.AddClient)
w.RegisterActivity(saga.AddBankAccount)
w.RegisterActivity(saga.ClearPostalAddresses)
w.RegisterActivity(saga.RemoveClient)
w.RegisterActivity(saga.DisconnectBankAccounts)

if err := w.Run(worker.InterruptCh()); err != nil {
log.Fatalln("Unable to start Worker", err)
}
}
Loading
Loading