Skip to content

Commit 0334e67

Browse files
committed
chore: add function synchronous execution doc
1 parent 010c127 commit 0334e67

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

fern/docs/pages/references/functions.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,58 @@ functions:
2727
- `name`: It is the function name that should match the corresponding function name in the folder.
2828
- `description`: It describes the function.
2929

30+
### Synchronous Execution with Synchronization Key
31+
32+
To ensure that function executions are processed in order (FIFO) for a given context (such as a user or conversation), you can enable synchronization support in your function’s manifest and use a synchronization key (sync-id).
33+
34+
#### How it Works
35+
36+
1. **Enable Synchronization in Manifest**
37+
- In your function’s manifest, add the field:
38+
```yaml
39+
functions:
40+
- name: foobar
41+
description: Let me execute
42+
supports_synchronization: true
43+
```
44+
- This tells the platform to use a FIFO queue for this function.
45+
46+
1. **Set the Synchronization Key**
47+
- When triggering an automation, include the sync-id in your rego policy along with the event_key. For example:
48+
```rego focus
49+
package rego
50+
51+
output = {"event": event, "event_key": event_key, "sync_id": sync_id} {
52+
input.request.method == "POST"
53+
event := input.request.body
54+
event_key := "user-enter"
55+
sync_id := "1234567890"
56+
} else = {"response": response }{
57+
response := {
58+
"status_code": 400
59+
}
60+
}
61+
```
62+
63+
- The sync-id groups related executions for ordered processing.
64+
65+
66+
3. **Execution Behavior**
67+
- If supports_synchronization is true and a sync-id is provided, executions with the same sync-id are processed in order.
68+
- If no sync-id is provided, executions are grouped by a random string.
69+
- If supports_synchronization is false, the sync-id is ignored and executions are processed as usual.
70+
71+
#### Platform Behavior
72+
73+
- The platform uses the sync-id as the message group for FIFO processing.
74+
- This ensures that all executions with the same sync-id are processed in order, preventing race conditions or duplicate conversations.
75+
76+
#### Use Cases
77+
78+
- Ensuring order of message processing for chatbots or messaging integrations.
79+
- Preventing duplicate or out-of-order actions when multiple events are triggered rapidly.
80+
81+
3082

3183
<Callout intent="note">
3284
Each function should be registered in `src/function-factory.ts` to be available for execution.

0 commit comments

Comments
 (0)