Skip to content

Commit 5640a72

Browse files
add new cursor instructions (#749)
* add new cursor instructions * del jpg * fix up
1 parent 8ae5dd7 commit 5640a72

3 files changed

Lines changed: 47 additions & 30 deletions

File tree

docs/build/triggers.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,16 @@ After the workflow completes, sometimes seconds (or minutes) later.
113113
}
114114
```
115115

116-
## Cron Triggers (formerly timers)
116+
## Cron Triggers
117117

118118
**Cron Triggers** run Workflows based on a cron schedule, and are good for
119119
repetitive tasks that are time-based (e.g., every day at 8am, sync financial
120-
data).
120+
data between two systems).
121121

122-
These Triggers enable users to pull data from connected systems. You can pick
122+
These Triggers enable users to "pull" data from connected systems. You can pick
123123
a standard schedule (e.g., every day, or every month), or define a custom
124124
schedule using cron expressions.
125125

126-
![Cron Trigger](/img/cron_trigger.webp)
127-
128126
:::tip Help with cron expressions
129127

130128
The best way to learn about `cron`, if you're not already familiar, is through
@@ -137,15 +135,22 @@ Cron Triggers enable Workflows to be run as frequently as once every minute, or
137135
as infrequently as you desire and can be scheduled on very specific dates or
138136
times.
139137

140-
Learn how a workflow's initial `state` gets built from a cron trigger
141-
[here](/documentation/jobs/state#cron-triggered-runs).
138+
### Input `state` for the next run
139+
140+
Every time a cron-triggered workflow is run it will _start_ with the final
141+
output of the last successful run. This allows users to build workflows that
142+
make use of a ["cursor"](/documentation/jobs/job-writing-guide#using-cursors)
143+
that tracks what happened last time the workflow ran. (Only processing data that
144+
changed since that last run, for example.)
145+
146+
![Cron Trigger](/img/cron_trigger.webp)
142147

143-
You can use a Cursor to help build input state when the workflow is triggered:
144-
see the [Job Writing Guide](/documentation/jobs/job-writing-guide#using-cursors)
145-
for more details.
148+
Be default, the input state for the next cron run will be the final output state
149+
of the previous run, but you can configure this to use the output state from a
150+
specific step in your earlier run by changing the "Cron Input Source".
146151

147-
Each time a timed job succeeds, its `final_state` will be saved and used as the
148-
input state for its next run.
152+
More on `state` in cron-triggered runs can be found in the
153+
["Input and Output State"](/documentation/jobs/state#cron-triggered-runs) docs.
149154

150155
### Managing the size of `state` for Cron Workflows
151156

docs/jobs/state.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ State is just a Javascript object. It is the means via which Jobs share
99
information between each other. It also provides a common scope for Operations
1010
to read from and write to.
1111

12-
The final state form a Job must always be a serializable Javascript object (ie,
13-
a JSON object). Any non-serializable keys will be removed.
12+
The final state form a Job must always be a serializable Javascript object
13+
(i.e., a JSON object). Any non-serializable keys will be removed.
1414

1515
![Job State Overview](/img/state-javascript.webp)
1616

@@ -35,16 +35,16 @@ State objects tend to have the following keys:
3535
name.
3636

3737
At the end of a Job, the configuration key will be removed, along with any other
38-
non serialisable keys.
38+
non serializable keys.
3939

4040
Adaptors will occasionally write extra information to state during a run - for
4141
example, database Adaptors tend to write a `client` key to state, used to track
4242
the database connection. These will be removed at the end of a Job.
4343

4444
## Input & output state for runs
4545

46-
Depending on whether you're running Workflows locally via the CLI or on the app, the input
47-
state for a Run must be generated differently:
46+
Depending on whether you're running Workflows locally via the CLI or on the app,
47+
the input state for a Run must be generated differently:
4848

4949
- When manually creating a work order, you must select or generate your input
5050
manually (e.g., by creating a custom `Input` on the app or `state.json` file
@@ -54,24 +54,25 @@ state for a Run must be generated differently:
5454

5555
The final state of a Run is determined by what's returned from the last
5656
operation. Remember that job expressions are a series of operations: they each
57-
take state and return state, after creating any number of side effects. The final returned
58-
state controls what is output by the run at the end of all of these operations.
57+
take state and return state, after creating any number of side effects. The
58+
final returned state controls what is output by the run at the end of all of
59+
these operations.
5960

6061
Best practice is to include a final state cleanup step that removes any data
6162
that should not persist between runs or be output (like PII), for example:
6263

6364
```js
6465
// get data from a data source
65-
get('https://jsonplaceholder.typicode.com/users')
66+
get('https://jsonplaceholder.typicode.com/users');
6667

6768
// store retrieved data in state for use later in job
6869
fn(state => {
69-
state.users = state.data;
70+
state.users = state.data;
7071
return state;
7172
});
7273

7374
// get more data from another data source
74-
get('https://jsonplaceholder.typicode.com/posts')
75+
get('https://jsonplaceholder.typicode.com/posts');
7576

7677
// store additional retrieved data in state for use later in job
7778
fn(state => {
@@ -89,10 +90,10 @@ fn(state => {
8990

9091
// cleanup state at the end before finishing job
9192
fn(state => {
92-
state.data = null
93-
state.users = null
94-
state.posts = null
95-
93+
state.data = null;
94+
state.users = null;
95+
state.posts = null;
96+
9697
return state;
9798
});
9899
```
@@ -148,20 +149,31 @@ The input state looks like this:
148149

149150
### Cron triggered runs
150151

151-
When a run is triggered by a cron job, its input state will be the output of the **first step** from the previous run. This allows each subsequent run to know about previous runs. In other words, you can pass information from one run to another even if they happen days apart.
152+
When a run is triggered by a cron job, its input state will be the final state
153+
of the previous run. This allows each subsequent run to know about previous
154+
runs. In other words, you can pass information from one run to another even if
155+
they happen days apart.
152156

153-
**Example scenario**: You have a **daily sync at 9 AM** with a workflow that has 3 steps: (1) fetch patient records, (2) transform data, (3) send to database. On Monday, the **first step** processes records up to ID 1000 and outputs `{ lastProcessedId: 1000 }`. Even though steps 2 and 3 modify the state further, only the **first step's output** gets saved for the next cron run. On Tuesday at 9 AM, the cron job starts again with `{ lastProcessedId: 1000 }` from Monday's first step, so it knows to fetch records starting from ID 1001.
157+
**Example scenario**: You have a **daily sync at 9 AM** with a workflow that has
158+
3 steps: (1) fetch patient records, (2) transform data, (3) send to database. On
159+
Monday, the workflow processes records up to ID 1000 and outputs
160+
`{ lastProcessedId: 1000 }` as its final state. On Tuesday at 9 AM, the cron job
161+
starts again with `{ lastProcessedId: 1000 }` as its input, so it knows to fetch
162+
and process records starting from ID 1001.
154163

155-
The first time the workflow runs, the initial state will simply be an empty JavaScript object: `{}`
164+
The first time the workflow runs, the initial state will simply be an empty
165+
JavaScript object: `{}`
156166

157167
#### Overriding cron input
158168

159169
You can always manually run a cron-triggered workflow with:
170+
160171
- **Empty input**: `{}` - starts fresh without previous state.
161172
- **Custom input**: Your own data to test specific scenarios.
162173
- **Default input**: Uses the same input as the scheduled runs.
163174

164-
If the manual run succeeds, the next scheduled cron run will start with whatever output state your manual run produced.
175+
If the manual run succeeds, the next scheduled cron run will start with whatever
176+
output state your manual run produced.
165177

166178
## Input & output state for steps
167179

static/img/cron_trigger.webp

53.7 KB
Loading

0 commit comments

Comments
 (0)