Continue-As-New - Rust SDK
This page answers the following questions for Rust developers:
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 Rust 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 passed as a parameter or stored in the Workflow struct.
Inside your Workflow, call ctx.continue_as_new() and propagate its result:
use std::time::Duration;
use temporalio_macros::{workflow, workflow_methods};
use temporalio_sdk::{ActivityOptions, ContinueAsNewOptions, WorkflowContext, WorkflowResult};
use crate::activities::MyActivities;
#[workflow]
#[derive(Default)]
pub struct GreetingWorkflow;
#[workflow_methods]
impl GreetingWorkflow {
#[run(name = "greeting-workflow-1")]
pub async fn run(ctx: &mut WorkflowContext<Self>, name: String) -> WorkflowResult<String> {
let greeting = ctx
.execute_activity(
MyActivities::greet,
name.clone(),
ActivityOptions::start_to_close_timeout(Duration::from_secs(30)),
)
.await?;
println!("{}", greeting);
if name == "Ziggy" {
Ok(greeting)
} else {
ctx.continue_as_new("New Name".to_string(), ContinueAsNewOptions::default())?;
}
}
}
The ctx.continue_as_new() method accepts the input to pass to the next Workflow Run.
When is it right to Continue-as-New using the Rust SDK?
Use Continue-as-New when your Workflow might encounter degraded performance or Event History Limits.
Temporal tracks your Workflow's progress against these limits to let you know when you should Continue-as-New. Call ctx.continue_as_new_suggested() to check if it's time.
How to test Continue-as-New using the Rust 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, if you have an internal value like test_continue_as_new == True, this sample takes a variable called max_history_length and that can be set to a small value. A helper method in the Workflow impl checks it each time it considers using Continue-as-New:
use serde::{Deserialize, Serialize};
use temporalio_macros::{workflow, workflow_methods};
use temporalio_sdk::{WorkflowContext, WorkflowResult};
#[derive(Serialize, Deserialize)]
pub struct GreetingInput {
pub name: String,
pub max_history_length: u32,
}
...
#[workflow_methods]
impl GreetingWorkflow {
#[run]
pub async fn run(
ctx: &mut WorkflowContext<Self>,
input: GreetingInput,
) -> WorkflowResult<String> {
// your Workflow code here
if Self::should_continue_as_new(ctx, input.max_history_length) {
// Continue as new
}
}
fn should_continue_as_new(
ctx: &WorkflowContext<Self>,
max_history_length: u32,
) -> bool {
if ctx.continue_as_new_suggested() {
return true;
}
// For testing
if max_history_length > 0 && ctx.history_length() > max_history_length {
return true;
}
false
}
}
Best practices
- Pass all necessary state: When continuing as new, include all state the next run needs.
- Use meaningful iteration markers: Include iteration numbers or timestamps to track progress.
- Test your state passing: Ensure parameters serialize and deserialize correctly.
- Don't continue-as-new too frequently: It's better to have some Event History than to continue-as-new on every execution.
- Consider batch sizes: Find the right balance between batch size and number of continues as new.