Skip to content

Commit 6495e59

Browse files
Cleanup and correct language of workflow initializers (#101)
1 parent 0e7a312 commit 6495e59

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

references/dotnet/advanced-features.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ var worker = new TemporalWorker(
9696

9797
## Workflow Init Attribute
9898

99-
Use `[WorkflowInit]` on a constructor to run initialization code when a workflow is first created.
99+
You should always put state initialization logic in the constructor of your workflow class, so that it happens before signals/updates arrive.
100100

101-
**Purpose:** Execute some setup code before signal/update happens or run is invoked.
101+
Normally, your constructor must have no arguments. However, if you add the `[WorkflowInit]` attribute, then your constructor instead receives the same workflow arguments that `[WorkflowRun]` receives:
102102

103103
```csharp
104104
[Workflow]
@@ -122,7 +122,7 @@ public class MyWorkflow
122122
}
123123
```
124124

125-
Constructor and `[WorkflowRun]` method must have the same parameters with the same types. You cannot make blocking calls (activities, sleeps, etc.) from the constructor.
125+
Constructor (with `[WorkflowInit]`) and `[WorkflowRun]` method must have the same parameters with the same types. You cannot make blocking calls (activities, sleeps, etc.) from the constructor.
126126

127127
## Workflow Failure Exception Types
128128

references/java/advanced-features.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ worker.registerActivitiesImplementations(new MyActivitiesImpl());
116116
factory.start();
117117
```
118118

119+
## Workflow Init Annotation
120+
121+
You should always put state initialization logic in the constructor of your workflow class, so that it happens before signals/updates arrive.
122+
123+
Normally, your constructor must have no arguments. However, if you add the `@WorkflowInit` annotation, then your constructor instead receives the same workflow arguments that `run` receives:
124+
125+
```java
126+
public class MyWorkflowImpl implements MyWorkflow {
127+
private final int foo;
128+
129+
@WorkflowInit
130+
public MyWorkflowImpl(MyInput input) {
131+
foo = 1234;
132+
}
133+
134+
@Override
135+
public ClusterManagerResult run(ClusterManagerInput input) {
136+
// this.foo is already initialized
137+
}
138+
}
139+
```
140+
141+
Constructor (with `@WorkflowInit`) and `run` method must have the same parameters with the same types. You cannot make blocking calls (activities, sleeps, etc.) from the constructor.
142+
119143
## Workflow Failure Exception Types
120144

121145
Control which exceptions cause workflow failures vs workflow task failures.

references/python/advanced-features.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ worker = Worker(
116116

117117
## Workflow Init Decorator
118118

119-
Use `@workflow.init` to run initialization code when a workflow is first created.
119+
You should always put state initialization logic in the `__init__` of your workflow class, so that it happens before signals/updates arrive.
120120

121-
**Purpose:** Execute some setup code before signal/update happens or run is invoked.
121+
Normally, your `__init__` must have no arguments. However, if you add the `@workflow.init` decorator, then your `__init__` instead receives the same workflow arguments that `@workflow.run` receives:
122122

123123
```python
124124
@workflow.defn
@@ -130,11 +130,13 @@ class MyWorkflow:
130130
self._items: list[str] = []
131131

132132
@workflow.run
133-
async def run(self) -> str:
133+
async def run(self, initial_value: str) -> str:
134134
# self._value and self._items are already initialized
135135
return self._value
136136
```
137137

138+
`__init__` (with `@workflow.init`) and `@workflow.run` must have the same parameters with the same types. You cannot make blocking calls (activities, sleeps, etc.) from the `__init__`.
139+
138140
## Workflow Failure Exception Types
139141

140142
Control which exceptions cause workflow task failures vs workflow failures.

0 commit comments

Comments
 (0)