Skip to main content

Continue as new

Continue-As-New enables a Workflow Execution to close successfully and create a new Workflow Execution in a single atomic operation if the number of Events in the Event History is becoming too large.
The Workflow Execution spawned from the use of Continue-As-New has the same Workflow Id, a new Run Id, and a fresh Event History and is passed all the appropriate parameters. ZIO-Temporal allows you to use Continue-As-New in various ways.

Defining a stub​

Let's start with some basic imports that will be required for the whole demonstration:

import zio._
import zio.temporal._
import zio.temporal.workflow._
import java.util.UUID

Then define workflow interfaces:

@workflowInterface
trait LongRunningWorkflow {
@workflowMethod
def watchFiles(paths: List[String]): Unit
}

In order to Continue-As-New, it's required to define a ZWorkflowContinueAsNewStub and run it using ZWorkflowContinueAsNewStub.execute:

class LongRunningWorkflowImpl extends LongRunningWorkflow {
private val logger = ZWorkflow.makeLogger

private val nextRun = ZWorkflow.newContinueAsNewStub[LongRunningWorkflow]()

override def watchFiles(paths: List[String]): Unit = {
logger.info(s"Watching files=$paths")
// Do stuff
ZWorkflowContinueAsNewStub.execute(
nextRun.watchFiles(paths)
)
}
}
  • To create a Continue-As-New stub, you must use ZWorkflow.newContinueAsNewStub[<Type>] method.
  • Reminder: you must always wrap the Continue-As-New invocation into ZWorkflowContinueAsNewStub.execute method.
    • nextRun.watchFiles(paths) invocation would be re-written into an untyped Temporal's Continue-As-New call
    • A direct method invocation will throw an exception

You can provide additional configuration for the next run using ZContinueAsNewOptions:

val continueAsNewOptions = ZContinueAsNewOptions.default
.withWorkflowRunTimeout(5.minutes)
.withTaskQueue("<other-task-queue>")
// continueAsNewOptions: ZContinueAsNewOptions = ZContinueAsNewOptions(
// workflowRunTimeout = Some(value = PT5M),
// taskQueue = Some(value = "<other-task-queue>"),
// workflowTaskTimeout = None,
// memo = Map(),
// searchAttributes = None,
// contextPropagators = List(),
// versioningIntent = None,
// javaOptionsCustomization = zio.temporal.workflow.ZContinueAsNewOptions$$$Lambda$9022/0x00007f228db8f150@10ecff3d
// )