Continue-As-New - Go SDK
This page answers the following questions for Go developers:
- What is Continue-As-New?
- How to Continue-As-New?
- When is it right to Continue-as-New?
- How to test Continue-as-New?
What is Continue-As-New?
Continue-As-New lets a Workflow Execution close successfully and creates a new Workflow Execution. You can think of it as a checkpoint when your Workflow gets too long or approaches certain scaling limits.
The new Workflow Execution is in the same chain; it keeps the same Workflow Id but gets a new Run Id and a fresh Event History. It also receives your Workflow's usual parameters.
How to Continue-As-New using the Go SDK
First, design your Workflow parameters so that you can pass in the "current state" when you Continue-As-New into the next Workflow run.
This state is typically set to None
for the original caller of the Workflow.
View the source code
in the context of the rest of the application code.
ClusterManagerInput struct {
State *ClusterManagerState
TestContinueAsNew bool
}
func newClusterManager(ctx workflow.Context, wfInput ClusterManagerInput) (*ClusterManager, error) {
The test hook in the above snippet is covered below.
Inside your Workflow, return the NewContinueAsNewError
error.
This stops the Workflow right away and starts a new one.
View the source code
in the context of the rest of the application code.
return ClusterManagerResult{}, workflow.NewContinueAsNewError(
ctx,
ClusterManagerWorkflow,
ClusterManagerInput{
State: &cm.state,
TestContinueAsNew: cm.testContinueAsNew,
},
)
Considerations for Workflows with Message Handlers
If you use Updates or Signals, don't call Continue-as-New from the handlers.
Instead, wait for your handlers to finish in your main Workflow before you return NewContinueAsNewError
.
See the AllHandlersFinished
example for guidance.
When is it right to Continue-as-New using the Go SDK?
Use Continue-as-New when your Workflow might hit Event History Limits.
Temporal tracks your Workflow's progress against these limits to let you know when you should Continue-as-New.
Call GetInfo(ctx).GetContinueAsNewSuggested()
to check if it's time.
How to test Continue-as-New using the Go SDK
Testing Workflows that naturally Continue-as-New may be time-consuming and resource-intensive. Instead, add a test hook to check your Workflow's Continue-as-New behavior faster in automated tests.
For example, when TestContinueAsNew == True
, this sample creates a test-only variable called maxHistoryLength
and sets it to a small value.
A helper method in the Workflow checks it each time it considers using Continue-as-New:
View the source code
in the context of the rest of the application code.
func (cm *ClusterManager) shouldContinueAsNew(ctx workflow.Context) bool {
if workflow.GetInfo(ctx).GetContinueAsNewSuggested() {
return true
}
if cm.maxHistoryLength > 0 && workflow.GetInfo(ctx).GetCurrentHistoryLength() > cm.maxHistoryLength {
return true
}
return false
}