Skip to content

Commit 5ddde91

Browse files
authored
docs: clarify the idempotency requirements (#823)
1 parent b536dce commit 5ddde91

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

docs/docs/custom_ops/custom_targets.mdx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ This method should be implemented to:
114114
- Update configuration when a target spec changes
115115
- Clean up resources when a target is removed (`current` is `None`)
116116

117-
:::note Best practice: Keep all actions idempotent
118-
119-
Ideally this method should be idempotent, i.e. when calling this with the same arguments multiple times, the effect should remain the same.
120-
For example, if the target is a directory, it should be a no-op if we try to create it (`previous` is `None`) when the directory already exists, and also a no-op if we try to delete it (`current` is `None`) when the directory does not exist.
121-
122-
:::
123-
124117
#### `describe(key) -> str` (Optional)
125118

126119
Returns a human-readable description of the target for logging and debugging purposes.
@@ -165,6 +158,18 @@ def prepare(spec: CustomTarget) -> PreparedCustomTarget:
165158

166159
If not provided, the original spec will be passed directly to `mutate`.
167160

161+
## Best Practices
162+
163+
### Idempotency of Methods with Side Effects
164+
165+
`apply_setup_change()` and `mutate()` are the two methods that are expected to produce side effects.
166+
We expect them to be idempotent, i.e. when calling them with the same arguments multiple times, the effect should remain the same.
167+
168+
For example,
169+
- For `apply_setup_change()`, if the target is a directory, it should be a no-op if we try to create it (`previous` is `None`) when the directory already exists, and also a no-op if we try to delete it (`current` is `None`) when the directory does not exist.
170+
- For `mutate()`, if a mutation is a deletion, it should be a no-op if the row does not exist.
171+
172+
This is to make sure when the system if left in an intermediate state, e.g. interrupted in the middle between a change is made and CocoIndex notes down the change is completed, the targets can still be gracefully rolled forward to the desired states after the system is resumed.
168173

169174
## Examples
170175

0 commit comments

Comments
 (0)