Generating Temporal Go from the Saga model
EOModeller blog · post 006
In the last post I rebuilt the Saga pattern from Temporal's technical guide as a model: the order saga, the account-opening saga from the guide's code, its Workflow and Activities, the Workers and Task Queues, a Nexus service, and a sequence diagram of a run that fails. This post takes the same model, unchanged, and generates code from it.
One click, one Go package
A package with the Temporal profile now offers Generate Temporal Go code. For the Saga model it produces one Go package, sagapattern, in five files:
| File | What's in it |
|---|---|
types.go |
OpenAccount, Address and BankDetails, with JSON tags that keep the model's names |
workflows.go |
the openAccount Workflow's name, Task Queue, id, start options and its status Query |
activities.go |
the seven Activities as an interface to implement, and a stub that calls each one with its options |
nexus.go |
the Accounts Nexus service, its endpoint and its two operations |
workers.go |
the Saga worker and the Onboarding worker |
It's ordinary Go for the Temporal Go SDK: typed wrappers over the SDK, nothing to run alongside it. The same model always gives the same files, so they can be checked in and diffed when the model changes.
What the model decided
Almost every line traces back to something drawn or typed into the model.
Timeouts. In the model, adding the bank account may take 4 seconds per attempt and 8 overall. The generated stub calls it with exactly those options:
func (AccountActivitiesStub) AddBankAccountOptions() workflow.ActivityOptions {
return workflow.ActivityOptions{
StartToCloseTimeout: 4 * time.Second,
ScheduleToCloseTimeout: 8 * time.Second,
}
}
The Workflow id. The «Workflow» has the id pattern saga-{accountId}, so there's a function for it, and start options that use the Workflow's Task Queue:
func OpenAccountWorkflowID(accountID string) string {
return "saga-" + accountID
}
What each Worker runs. On the Workers and queues diagram, the Saga worker «registers» the Workflow and the Activities, and the Nexus endpoint routes to its queue. So the generated constructor asks for all three, and registers each Activity under the name the Workflow calls it by:
func NewSagaWorker(
c client.Client,
openAccountWorkflow OpenAccountWorkflowFunc,
accountActivities AccountActivities,
accountsHandlers AccountsHandlers,
) (worker.Worker, error)
That turns a deployment diagram into a compile-time check. Forget to implement one of the seven Activities and the Worker won't build. Delete the «registers» line to the Activities in the model and that parameter goes away.
Nexus. openAccount is an Async operation whose handler Workflow is the saga. The generated service starts it, and fills the Workflow id from the operation's input. The pattern's {accountId} names a field of OpenAccount, so a retried request starts the same Workflow, not a second one:
return temporalnexus.ExecuteUntypedWorkflow[nexus.NoValue](
ctx, opts,
OpenAccountWorkflowOptions(OpenAccountWorkflowID(in.AccountID)),
OpenAccountWorkflowName, in)
getAccountStatus is Sync, so it becomes a handler method to write. Callers in other Workflows get ExecuteAccountsOpenAccount, which uses the endpoint that «exposes» the service and the operation's one-hour timeout.
What it doesn't write
It doesn't write the Workflow itself. The activity diagram documents the saga; the code that runs it is the developer's. The generated package gives that code a signature to match, OpenAccountWorkflowFunc, and a stub that runs each Activity with the model's options. So the saga reads like the diagram:
acts := sagapattern.AccountActivitiesStub{}
if err := acts.AddAddress(ctx, params.AccountID, params.Address).Get(ctx, nil); err != nil {
return err
}
compensations = append(compensations, func(c workflow.Context) error {
return acts.ClearPostalAddresses(c, params.AccountID).Get(c, nil)
})
Each step that succeeds registers the compensation that undoes it, and a deferred function runs them newest first if a later step fails.
Running the failed run
The last post ended with a sequence diagram of a run that fails: three steps succeed, adding the bank account fails, then the client is removed and the addresses cleared, in that order. I wrote that run as a test, with Temporal's test environment and a fake bank that's always down. It passes, and the calls come out as:
createAccount, addAddress, addClient,
addBankAccount, addBankAccount, … (retried),
removeClient, clearPostalAddresses
The diagram shows one failure, but Temporal doesn't give up after one. It retried the step on its own until the 8-second budget from the model ran out, and only then did the failure reach the Workflow. Then the compensations ran, newest first, as the diagram says. That's the timeout from the first post doing its job: without it, Temporal would keep retrying.
What the generator noticed
Generating code is another check on the model. The export came back with one note: the onboarding team's Workflow, OnboardCustomerWorkflow, has no operations, so it takes no input. On the Nexus diagram it's just a box that calls the service. It's fine for a diagram about Nexus, but before writing the code you'd want to say what starting an onboarding takes.
Getting it
- In the app: select a package with the Temporal profile and choose Generate Temporal Go code, or select a «Workflow», «Activities» or «NexusService» to generate just that and the types it uses.
- From an AI assistant: the same files come through EOModeller's MCP server, as
export_schemawith the formattemporal-go.
The generated Go is checked against the Temporal Go SDK and the Nexus Go SDK: it's built, vetted and run in tests, including the saga above. .NET is next, then TypeScript, Java and Python.
References
- The Saga Pattern Made Easy: Automating the Saga Pattern with Temporal, Temporal technical guide, January 2024
- Temporal Go SDK and the Go SDK developer guide
- Temporal Nexus and the Nexus Go SDK
- Redrawing the Saga pattern for Temporal, the previous post
- Generate code from your model