Skip to content

Commit 6aecfa0

Browse files
Merge branch 'main' into python-workflow-init
2 parents 71c69c2 + c60ba2d commit 6aecfa0

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

docs/develop/typescript/message-passing.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ export async function greetingWorkflow(): Promise<string> {
129129
This allows you to use Activities, Child Workflows, durable [`workflow.sleep`](https://typescript.temporal.io/api/namespaces/workflow#sleep) Timers, [`workflow.condition`](https://typescript.temporal.io/api/namespaces/workflow#condition) conditions, and more.
130130
See [Async handlers](#async-handlers) and [Workflow message passing](/encyclopedia/workflow-message-passing) for guidelines on safely using async Signal and Update handlers.
131131

132+
- Delay calling [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) until the Workflow initialization needed by Signal (or Update) handlers has finished.
133+
This is safe because the SDK buffers messages when there are no registered handlers for them.
134+
Note that [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) will immediately invoke the handler of buffered Signals (or Updates) with matching types.
135+
This could lead to out-of-order processing of messages with different types.
136+
132137
- `setHandler` can take `SignalHandlerOptions` (such as `description` and `unfinishedPolicy`) as described in the API reference docs for [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler).
133138

134139

@@ -196,6 +201,10 @@ wf.setHandler(
196201
This includes the Update ID, which can be useful for deduplication when using Continue-As-New: see [Ensuring your messages are processed exactly once](/encyclopedia/workflow-message-passing#exactly-once-message-processing).
197202
- Update (and Signal) handlers can be `async`, letting them use Activities, Child Workflows, durable [`workflow.sleep`](https://typescript.temporal.io/api/namespaces/workflow#sleep) Timers, [`workflow.condition`](https://typescript.temporal.io/api/namespaces/workflow#condition) conditions, and more.
198203
See [Async handlers](#async-handlers) and [Workflow message passing](/encyclopedia/workflow-message-passing) for safe usage guidelines.
204+
- Delay calling [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) until the Workflow initialization needed by Update (or Signal) handlers has finished.
205+
This is safe because the SDK buffers messages when there are no registered handlers for them.
206+
Note that [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) will immediately invoke the handler of buffered Updates (or Signals) with matching types.
207+
This could lead to out-of-order processing of messages with different types.
199208

200209
## Send messages {#send-messages}
201210

docs/encyclopedia/application-message-passing.mdx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,44 @@ Every time the Workflow wakes up--generally, it wakes up when it needs to--it wi
220220

221221
This execution is on a single thread–while this means you don’t have to worry about parallelism, you do need to worry about concurrency if you have written Signal and Update handlers that can block. These can run interleaved with the main Workflow and with one another, resulting in potential race conditions. These methods should be made reentrant.
222222

223+
#### Initializing the Workflow first {#workflow-initializers}
224+
225+
Initialize your Workflow's state before handling messages.
226+
This prevents your handler from reading uninitialized instance variables.
227+
228+
To see why, refer to the [diagram](#message-handler-concurrency).
229+
It shows that your Workflow processes messages before the first run of your Workflow's main method.
230+
231+
The message handler runs first in several scenarios, such as:
232+
233+
- When using [Signal-with-Start](#signal-with-start).
234+
- When your Worker experiences delays, such as when the Task Queue it polls gets backlogged.
235+
- When messages arrive immediately after a Workflow continues as new but before it resumes.
236+
237+
For all languages except Go and TypeScript, use your constructor to set up state.
238+
Annotate your constructor as a Workflow Initializer and take the same arguments as your Workflow's main method.
239+
240+
Note that you can't make blocking calls from your constructor.
241+
If you need to block, make your Signal or Update handler [wait](#waiting) for an initialization flag.
242+
243+
In Go and TypeScript, register any message handlers only after completing initialization.
244+
223245
### Message handler patterns {#message-handler-patterns}
224246

225247
Here are several common patterns for write operations, Signal and Update handlers. They don't apply to pure read operations, i.e. Queries or [Update Validators](#update-validators):
226248

227-
- Synchronous, returning immediately.
228-
- Waiting for the Workflow to be ready to process them.
249+
- Returning immediately from a handler
250+
- Waiting for the Workflow to be ready to process them
229251
- Kicking off activities and other asynchronous tasks
230-
- Injecting work into the main Workflow.
231-
- Finishing handlers before the Workflow completes.
232-
- Ensuring your messages are processed exactly once.
252+
- Injecting work into the main Workflow
253+
- Finishing handlers before the Workflow completes
254+
- Ensuring your messages are processed exactly once
233255

234256
#### Synchronous handlers
235257

236258
Synchronous handlers don’t kick off any long-running operations or otherwise block. They're guaranteed to run atomically.
237259

238-
#### Waiting
260+
#### Waiting {#waiting}
239261

240262
A Signal or Update handler can block waiting for the Workflow to reach a certain state using a Wait Condition. See the links below to find out how to use this with your SDK.
241263

0 commit comments

Comments
 (0)