You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/metatype.dev/docs/reference/runtimes/substantial/index.mdx
+75-2Lines changed: 75 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,13 +37,86 @@ Additionally, if we were to shut down the Typegate node executing it and then re
37
37
38
38
## Key Concepts
39
39
40
+
41
+
### Backend
42
+
43
+
This abstraction implements a set of atomic operations that allows Typegate to persist and recover the workflow state. Currently, we have the **Redis** backend available, along with others like **fs** and **memory**, which are primarily intended for development or testing purposes.
44
+
40
45
### Workflows
41
46
42
47
A special type of function with **durable state** and an execution mechanism directly tied to time. A workflow can also trigger other workflows (child workflows).
43
48
44
-
###Backend
49
+
#### Persistence and Lifecycle
45
50
46
-
This abstraction implements a set of atomic operations that allows Typegate to persist and recover the workflow state. Currently, we have the **Redis** backend available, along with others like **fs** and **memory**, which are primarily intended for development or testing purposes.
51
+
***Context**
52
+
53
+
The context object contains the workflow input (namely `kwargs` as seen in the example above), but it can also be thought as a namespace that contains all of the core functions used for durableness.
54
+
55
+
***Interrupts**
56
+
57
+
An special state of the program that is produced by any function that can trigger a workflow **replay**.
58
+
59
+
One simple example of such function is when you want to wait for a given amount of time, Substantial will save the current time and the end time, interrupts the workflow then requeue it to execute later.
60
+
Any agent (Typegate node) that picks the workflow, will **replay** it, the cycle repeats until the actual current time is greater or equal to the end time.
61
+
62
+
```typescript
63
+
awaitctx.sleep(24*3600*1000); // 1 day
64
+
```
65
+
66
+
***Save**
67
+
68
+
A save is one of the main building blocks of a workflow, many functions avalaiable on the context object relays on it.
69
+
70
+
This is mainly because, a save call converts any function into a durable one: the function output is saved.
71
+
This ensures that when a workflow is resumed(after a typgate reboot for example) or replayed (after interrupts), if the the result was saved the saved function result will be
72
+
73
+
```typescript
74
+
// For example, if the ouptut was 7, after replay, save will not execute the function inside but directly return the
75
+
const rand =awaitctx.save(() =>Math.random());
76
+
77
+
// If you keep in mind that the workflow can be replayed many times
78
+
// A save call should make more sense!
79
+
const now =awaitctx.save(() =>Date.now());
80
+
81
+
// And even more for functions that can produce outside effects
82
+
const result =awaitctx.save(sendEmail());
83
+
```
84
+
85
+
:::info Notes
86
+
87
+
Only json-compliant values can be persisted.
88
+
89
+
:::
90
+
91
+
92
+
***Send/Receive**
93
+
94
+
You can send events to a workflow through GraphQL, any receive call on the workflow will await for it and will **interrupt** the workflow if it was not noticed yet.
95
+
```gralhql
96
+
query {
97
+
awesomeSend(
98
+
run_id: "<workflow_run_id>",
99
+
event: { name: "myEvent", payload: 1234 }
100
+
)
101
+
}
102
+
```
103
+
104
+
```typescript
105
+
const value =awaitctx.receive<number>("myEvent"); // 1234
106
+
```
107
+
108
+
***ensure**
109
+
110
+
It's a function that takes a function that returns a boolean, if the returned value is false, it will **interrupt** the workflow.
0 commit comments