@sanity/runtime-cli 17.16.0-next.g8d59d6f → 17.16.0-next.g91e6dd2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -19
- package/dist/cores/functions/test.js +14 -3
- package/dist/utils/functions/child-runtime/durable.js +99 -22
- package/dist/utils/functions/child-runtime/step-history.d.ts +15 -0
- package/dist/utils/functions/child-runtime/step-history.js +52 -0
- package/dist/utils/functions/invoke/local.js +16 -6
- package/dist/utils/types.d.ts +4 -0
- package/oclif.manifest.json +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -90,7 +90,7 @@ EXAMPLES
|
|
|
90
90
|
$ sanity-run blueprints add function --name my-function --fn-type document-create --fn-type document-update --lang js
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
93
|
+
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/add.ts)_
|
|
94
94
|
|
|
95
95
|
## `sanity-run blueprints config`
|
|
96
96
|
|
|
@@ -127,7 +127,7 @@ EXAMPLES
|
|
|
127
127
|
$ sanity-run blueprints config --edit --project-id <projectId> --stack <name-or-id>
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
130
|
+
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/config.ts)_
|
|
131
131
|
|
|
132
132
|
## `sanity-run blueprints deploy`
|
|
133
133
|
|
|
@@ -183,7 +183,7 @@ EXAMPLES
|
|
|
183
183
|
$ sanity-run blueprints deploy --new-stack-name <new-name>
|
|
184
184
|
```
|
|
185
185
|
|
|
186
|
-
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
186
|
+
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/deploy.ts)_
|
|
187
187
|
|
|
188
188
|
## `sanity-run blueprints destroy`
|
|
189
189
|
|
|
@@ -221,7 +221,7 @@ EXAMPLES
|
|
|
221
221
|
$ sanity-run blueprints destroy --stack <name-or-id> --project-id <projectId> --force --no-wait
|
|
222
222
|
```
|
|
223
223
|
|
|
224
|
-
_See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
224
|
+
_See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/destroy.ts)_
|
|
225
225
|
|
|
226
226
|
## `sanity-run blueprints doctor`
|
|
227
227
|
|
|
@@ -253,7 +253,7 @@ EXAMPLES
|
|
|
253
253
|
$ sanity-run blueprints doctor --fix
|
|
254
254
|
```
|
|
255
255
|
|
|
256
|
-
_See code: [src/commands/blueprints/doctor.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
256
|
+
_See code: [src/commands/blueprints/doctor.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/doctor.ts)_
|
|
257
257
|
|
|
258
258
|
## `sanity-run blueprints info`
|
|
259
259
|
|
|
@@ -291,7 +291,7 @@ EXAMPLES
|
|
|
291
291
|
$ sanity-run blueprints info --organization-id <orgId> --stack <name-or-id>
|
|
292
292
|
```
|
|
293
293
|
|
|
294
|
-
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
294
|
+
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/info.ts)_
|
|
295
295
|
|
|
296
296
|
## `sanity-run blueprints init [DIR]`
|
|
297
297
|
|
|
@@ -355,7 +355,7 @@ EXAMPLES
|
|
|
355
355
|
$ sanity-run blueprints init old-stack --type <json|js|ts> --project-id <projectId> --stack-id <existingStackId>
|
|
356
356
|
```
|
|
357
357
|
|
|
358
|
-
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
358
|
+
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/init.ts)_
|
|
359
359
|
|
|
360
360
|
## `sanity-run blueprints logs`
|
|
361
361
|
|
|
@@ -401,7 +401,7 @@ EXAMPLES
|
|
|
401
401
|
$ sanity-run blueprints logs --before 2026-05-01T00:00:00Z
|
|
402
402
|
```
|
|
403
403
|
|
|
404
|
-
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
404
|
+
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/logs.ts)_
|
|
405
405
|
|
|
406
406
|
## `sanity-run blueprints mint-deploy-token`
|
|
407
407
|
|
|
@@ -446,7 +446,7 @@ EXAMPLES
|
|
|
446
446
|
$ sanity-run blueprints mint-deploy-token --organization-id <orgId>
|
|
447
447
|
```
|
|
448
448
|
|
|
449
|
-
_See code: [src/commands/blueprints/mint-deploy-token.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
449
|
+
_See code: [src/commands/blueprints/mint-deploy-token.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/mint-deploy-token.ts)_
|
|
450
450
|
|
|
451
451
|
## `sanity-run blueprints plan`
|
|
452
452
|
|
|
@@ -479,7 +479,7 @@ EXAMPLES
|
|
|
479
479
|
$ sanity-run blueprints plan --organization-id <orgId> --stack <name-or-id>
|
|
480
480
|
```
|
|
481
481
|
|
|
482
|
-
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
482
|
+
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/plan.ts)_
|
|
483
483
|
|
|
484
484
|
## `sanity-run blueprints promote`
|
|
485
485
|
|
|
@@ -515,7 +515,7 @@ EXAMPLES
|
|
|
515
515
|
$ sanity-run blueprints promote --new-stack-name <new-name>
|
|
516
516
|
```
|
|
517
517
|
|
|
518
|
-
_See code: [src/commands/blueprints/promote.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
518
|
+
_See code: [src/commands/blueprints/promote.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/promote.ts)_
|
|
519
519
|
|
|
520
520
|
## `sanity-run blueprints stacks`
|
|
521
521
|
|
|
@@ -556,7 +556,7 @@ EXAMPLES
|
|
|
556
556
|
$ sanity-run blueprints stacks --organization-id <organizationId> --include-projects
|
|
557
557
|
```
|
|
558
558
|
|
|
559
|
-
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
559
|
+
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/blueprints/stacks.ts)_
|
|
560
560
|
|
|
561
561
|
## `sanity-run functions add`
|
|
562
562
|
|
|
@@ -608,7 +608,7 @@ EXAMPLES
|
|
|
608
608
|
$ sanity-run functions add --name my-function --type document-create --type document-update --lang js
|
|
609
609
|
```
|
|
610
610
|
|
|
611
|
-
_See code: [src/commands/functions/add.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
611
|
+
_See code: [src/commands/functions/add.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/add.ts)_
|
|
612
612
|
|
|
613
613
|
## `sanity-run functions dev`
|
|
614
614
|
|
|
@@ -643,7 +643,7 @@ EXAMPLES
|
|
|
643
643
|
$ sanity-run functions dev --timeout 60
|
|
644
644
|
```
|
|
645
645
|
|
|
646
|
-
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
646
|
+
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/dev.ts)_
|
|
647
647
|
|
|
648
648
|
## `sanity-run functions env add NAME KEY VALUE`
|
|
649
649
|
|
|
@@ -676,7 +676,7 @@ EXAMPLES
|
|
|
676
676
|
$ sanity-run functions env add --stack <name-or-id> MyFunction API_URL https://api.example.com/
|
|
677
677
|
```
|
|
678
678
|
|
|
679
|
-
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
679
|
+
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/env/add.ts)_
|
|
680
680
|
|
|
681
681
|
## `sanity-run functions env list NAME`
|
|
682
682
|
|
|
@@ -706,7 +706,7 @@ EXAMPLES
|
|
|
706
706
|
$ sanity-run functions env list --stack <name-or-id> MyFunction
|
|
707
707
|
```
|
|
708
708
|
|
|
709
|
-
_See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
709
|
+
_See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/env/list.ts)_
|
|
710
710
|
|
|
711
711
|
## `sanity-run functions env remove NAME KEY`
|
|
712
712
|
|
|
@@ -738,7 +738,7 @@ EXAMPLES
|
|
|
738
738
|
$ sanity-run functions env remove --stack <name-or-id> MyFunction API_URL
|
|
739
739
|
```
|
|
740
740
|
|
|
741
|
-
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
741
|
+
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/env/remove.ts)_
|
|
742
742
|
|
|
743
743
|
## `sanity-run functions logs [NAME]`
|
|
744
744
|
|
|
@@ -778,7 +778,7 @@ EXAMPLES
|
|
|
778
778
|
$ sanity-run functions logs <name> --delete
|
|
779
779
|
```
|
|
780
780
|
|
|
781
|
-
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
781
|
+
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/logs.ts)_
|
|
782
782
|
|
|
783
783
|
## `sanity-run functions test [NAME]`
|
|
784
784
|
|
|
@@ -836,7 +836,7 @@ EXAMPLES
|
|
|
836
836
|
$ sanity-run functions test <name> --event update --data-before '{ "title": "before" }' --data-after '{ "title": "after" }'
|
|
837
837
|
```
|
|
838
838
|
|
|
839
|
-
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.
|
|
839
|
+
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v17.16.0-next.g91e6dd2/src/commands/functions/test.ts)_
|
|
840
840
|
|
|
841
841
|
## `sanity-run help [COMMAND]`
|
|
842
842
|
|
|
@@ -116,7 +116,9 @@ export async function functionTestCore(options) {
|
|
|
116
116
|
}
|
|
117
117
|
};
|
|
118
118
|
const logNestedRuns = () => {
|
|
119
|
-
|
|
119
|
+
// Remove buffered logs for durable runs
|
|
120
|
+
const runs = plan.isDurable ? nestedRuns.splice(0) : [...nestedRuns];
|
|
121
|
+
for (const run of runs.sort((a, b) => a.order - b.order)) {
|
|
120
122
|
// Skip if is Durable so log ordering is retained
|
|
121
123
|
if (run.isLiveDurable)
|
|
122
124
|
continue;
|
|
@@ -155,10 +157,19 @@ export async function functionTestCore(options) {
|
|
|
155
157
|
if (event.kind === 'delegate') {
|
|
156
158
|
logNestedRuns();
|
|
157
159
|
}
|
|
158
|
-
const
|
|
160
|
+
const willRetry = event.phase === 'error' && typeof event.retryDelayMs === 'number';
|
|
161
|
+
const glyph = willRetry ? '⟳' : (STEP_PHASE_GLYPH[event.phase] ?? '•');
|
|
159
162
|
const duration = typeof event.durationMs === 'number' ? ` (${Math.round(event.durationMs)}ms)` : '';
|
|
163
|
+
const retriesPerformed = (event.attempt ?? 1) - 1;
|
|
164
|
+
let retryNote = '';
|
|
165
|
+
if (willRetry) {
|
|
166
|
+
retryNote = ` retry ${event.attempt} of ${event.maxAttempts}`;
|
|
167
|
+
}
|
|
168
|
+
else if (event.phase === 'error' && retriesPerformed > 0) {
|
|
169
|
+
retryNote = ` failed after ${retriesPerformed} retries`;
|
|
170
|
+
}
|
|
160
171
|
const detail = event.phase === 'error' && event.error ? ` ${event.error.message}` : '';
|
|
161
|
-
lines.push(` ${glyph} ${event.name} [${event.kind}]${duration}${detail}`);
|
|
172
|
+
lines.push(` ${glyph} ${event.name} [${event.kind}]${duration}${detail}${retryNote}`);
|
|
162
173
|
}
|
|
163
174
|
return lines;
|
|
164
175
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {performance} from 'node:perf_hooks'
|
|
2
2
|
import {inspect} from 'node:util'
|
|
3
|
+
import {createStepHistory} from './step-history.js'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
*
|
|
@@ -26,7 +27,9 @@ const formatArgs = (args) =>
|
|
|
26
27
|
* @param io
|
|
27
28
|
*/
|
|
28
29
|
export async function runDurable(handler, {context, event}, io) {
|
|
30
|
+
const history = createStepHistory()
|
|
29
31
|
let currentStep = null
|
|
32
|
+
|
|
30
33
|
const streamLog = (level, args) => {
|
|
31
34
|
const date = new Date()
|
|
32
35
|
const timestamp = `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
|
@@ -41,7 +44,6 @@ export async function runDurable(handler, {context, event}, io) {
|
|
|
41
44
|
})
|
|
42
45
|
}
|
|
43
46
|
|
|
44
|
-
// @todo: need to throw error for use of `console.log` in a durable function
|
|
45
47
|
const logger = {
|
|
46
48
|
log: (...a) => streamLog('info', a),
|
|
47
49
|
info: (...a) => streamLog('info', a),
|
|
@@ -49,24 +51,57 @@ export async function runDurable(handler, {context, event}, io) {
|
|
|
49
51
|
error: (...a) => streamLog('error', a),
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
const emitFailure = (pending) => {
|
|
55
|
+
history.settle(pending)
|
|
56
|
+
io.emit(pending.event)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const settleUnretried = () => {
|
|
60
|
+
const pending = history.getPending()
|
|
61
|
+
if (pending !== null) {
|
|
62
|
+
emitFailure(pending)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const track = async (name, kind, produce, retry) => {
|
|
67
|
+
settleUnretried()
|
|
68
|
+
const {index, saved} = history.nextStep()
|
|
69
|
+
|
|
70
|
+
if (saved?.phase === 'complete') {
|
|
71
|
+
return saved.output
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const maxAttempts = retry?.maxAttempts ?? 1
|
|
75
|
+
const retries = Math.max((retry?.maxAttempts ?? 1) - 1, 0)
|
|
76
|
+
const waitingOnRetry = typeof saved?.retryDelayMs === 'number'
|
|
77
|
+
|
|
78
|
+
if (saved?.phase === 'error' && !waitingOnRetry) {
|
|
79
|
+
throw saved.output
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const attempt = (saved?.attempt ?? 0) + 1
|
|
55
83
|
const prev = currentStep
|
|
56
84
|
currentStep = name
|
|
57
85
|
|
|
86
|
+
io.emit({name, kind, phase: 'start'})
|
|
87
|
+
const startedAt = performance.now()
|
|
88
|
+
|
|
58
89
|
try {
|
|
59
|
-
const value = await produce()
|
|
60
|
-
|
|
90
|
+
const value = await produce(attempt)
|
|
91
|
+
const completeEvent = {
|
|
61
92
|
name,
|
|
62
93
|
kind,
|
|
63
94
|
phase: 'complete',
|
|
64
95
|
output: value,
|
|
65
96
|
durationMs: performance.now() - startedAt,
|
|
66
|
-
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
history.recordSuccess(index, completeEvent)
|
|
100
|
+
io.emit(completeEvent)
|
|
101
|
+
|
|
67
102
|
return value
|
|
68
103
|
} catch (err) {
|
|
69
|
-
|
|
104
|
+
const errorEvent = {
|
|
70
105
|
name,
|
|
71
106
|
kind,
|
|
72
107
|
phase: 'error',
|
|
@@ -76,7 +111,21 @@ export async function runDurable(handler, {context, event}, io) {
|
|
|
76
111
|
stack: err?.stack ?? '',
|
|
77
112
|
},
|
|
78
113
|
durationMs: performance.now() - startedAt,
|
|
114
|
+
attempt,
|
|
115
|
+
retries,
|
|
116
|
+
maxAttempts,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const retryPending = history.recordFailure(index, {
|
|
120
|
+
event: errorEvent,
|
|
121
|
+
error: err,
|
|
122
|
+
delayMs: retry?.delay ? durationToMs(retry.delay) : 0,
|
|
79
123
|
})
|
|
124
|
+
|
|
125
|
+
if (!retryPending) {
|
|
126
|
+
io.emit(errorEvent)
|
|
127
|
+
}
|
|
128
|
+
|
|
80
129
|
throw err
|
|
81
130
|
} finally {
|
|
82
131
|
currentStep = prev
|
|
@@ -84,12 +133,21 @@ export async function runDurable(handler, {context, event}, io) {
|
|
|
84
133
|
}
|
|
85
134
|
|
|
86
135
|
const step = {
|
|
87
|
-
run: ({name, handler: fn
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
136
|
+
run: ({name, handler: fn, retry}) =>
|
|
137
|
+
track(name || '', 'run', (attempt) => fn({logger, attempt}), retry),
|
|
138
|
+
delegate: ({name, handler, event, retry}) =>
|
|
139
|
+
track(
|
|
140
|
+
name || '',
|
|
141
|
+
'delegate',
|
|
142
|
+
() => {
|
|
143
|
+
const fn = typeof handler === 'string' ? handler : handler?.name
|
|
144
|
+
if (!fn) {
|
|
145
|
+
throw new Error('No function name provided for delegation')
|
|
146
|
+
}
|
|
147
|
+
return io.invoke(fn, event?.data ?? {})
|
|
148
|
+
},
|
|
149
|
+
retry,
|
|
150
|
+
),
|
|
93
151
|
waitForCondition: ({name, initial, poller, next}) =>
|
|
94
152
|
track(name || '', 'wait-for-condition', async () => {
|
|
95
153
|
let state = initial
|
|
@@ -107,15 +165,34 @@ export async function runDurable(handler, {context, event}, io) {
|
|
|
107
165
|
}
|
|
108
166
|
return state
|
|
109
167
|
}),
|
|
110
|
-
|
|
111
|
-
track(name || '', '
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
throw new Error('No function name provided for delegation')
|
|
115
|
-
}
|
|
116
|
-
return io.invoke(fn, event)
|
|
168
|
+
wait: ({name, duration}) =>
|
|
169
|
+
track(name || '', 'wait', async () => {
|
|
170
|
+
await io.wait(durationToMs(duration))
|
|
171
|
+
return undefined
|
|
117
172
|
}),
|
|
118
173
|
}
|
|
119
174
|
|
|
120
|
-
|
|
175
|
+
let result
|
|
176
|
+
let done = false
|
|
177
|
+
while (!done) {
|
|
178
|
+
history.startRun()
|
|
179
|
+
try {
|
|
180
|
+
result = await handler({context, event, step, logger})
|
|
181
|
+
settleUnretried()
|
|
182
|
+
done = true
|
|
183
|
+
} catch (err) {
|
|
184
|
+
const pending = history.getPending()
|
|
185
|
+
if (pending === null) {
|
|
186
|
+
throw err
|
|
187
|
+
}
|
|
188
|
+
if (err?.name === 'UnrecoverableError') {
|
|
189
|
+
emitFailure(pending)
|
|
190
|
+
throw err
|
|
191
|
+
}
|
|
192
|
+
io.emit({...pending.event, retryDelayMs: pending.delayMs})
|
|
193
|
+
await io.wait(pending.delayMs)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return result
|
|
121
198
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type {DurableStepEvent} from './types'
|
|
2
|
+
|
|
3
|
+
type PendingRetry = {index: number; event: DurableStepEvent; delayMs: number}
|
|
4
|
+
|
|
5
|
+
export function createStepHistory(): {
|
|
6
|
+
startRun: () => void
|
|
7
|
+
nextStep: () => {index: number; saved: DurableStepEvent | undefined}
|
|
8
|
+
recordSuccess: (index: number, event: DurableStepEvent) => void
|
|
9
|
+
recordFailure: (
|
|
10
|
+
index: number,
|
|
11
|
+
failure: {event: DurableStepEvent; error: unknown; delayMs: number},
|
|
12
|
+
) => boolean
|
|
13
|
+
getPending: () => PendingRetry | null
|
|
14
|
+
settle: (pending: PendingRetry) => void
|
|
15
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const createStepHistory = () => {
|
|
2
|
+
const history = new Map([])
|
|
3
|
+
let cursor = 0
|
|
4
|
+
let pending = null
|
|
5
|
+
|
|
6
|
+
const startRun = () => {
|
|
7
|
+
cursor = 0
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const nextStep = () => {
|
|
11
|
+
const index = cursor
|
|
12
|
+
cursor++
|
|
13
|
+
return {index, saved: history.get(index)}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const recordSuccess = (index, event) => {
|
|
17
|
+
history.set(index, event)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const recordFailure = (index, {event, error, delayMs}) => {
|
|
21
|
+
const retryable = event.attempt <= event.retries && error?.name !== 'UnrecoverableError'
|
|
22
|
+
|
|
23
|
+
if (retryable) {
|
|
24
|
+
history.set(index, {...event, output: error, retryDelayMs: delayMs})
|
|
25
|
+
pending = {index, event, delayMs}
|
|
26
|
+
} else {
|
|
27
|
+
history.set(index, {...event, output: error})
|
|
28
|
+
}
|
|
29
|
+
return retryable
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const getPending = () => {
|
|
33
|
+
const item = pending
|
|
34
|
+
pending = null
|
|
35
|
+
return item
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const settle = ({index, event}) => {
|
|
39
|
+
history.set(index, {...event, output: history.get(index).output})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
startRun,
|
|
44
|
+
nextStep,
|
|
45
|
+
recordSuccess,
|
|
46
|
+
recordFailure,
|
|
47
|
+
getPending,
|
|
48
|
+
settle,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export {createStepHistory}
|
|
@@ -174,12 +174,22 @@ export default async function invoke(resource, payload, context, options) {
|
|
|
174
174
|
// of `nested.error`. The error isn't lost: it travels back in the returned
|
|
175
175
|
// result and is logged/streamed to the UI by the caller (app/test core).
|
|
176
176
|
if (child && !child.killed) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
177
|
+
// step.delegate errors need to be surfaced to the caller for retry behavior and for the durable step to be marked as failed.
|
|
178
|
+
const origin = metadata?.origin || undefined;
|
|
179
|
+
const error = nested.error || undefined;
|
|
180
|
+
const response = origin === 'step.delegate' && error
|
|
181
|
+
? {
|
|
182
|
+
type: 'invoke-response',
|
|
183
|
+
id,
|
|
184
|
+
ok: false,
|
|
185
|
+
error,
|
|
186
|
+
}
|
|
187
|
+
: {
|
|
188
|
+
type: 'invoke-response',
|
|
189
|
+
id,
|
|
190
|
+
ok: true,
|
|
191
|
+
result: { json: nested.json },
|
|
192
|
+
};
|
|
183
193
|
child.send(JSON.stringify(response));
|
|
184
194
|
}
|
|
185
195
|
}
|
package/dist/utils/types.d.ts
CHANGED
|
@@ -379,6 +379,10 @@ export interface DurableStepEvent {
|
|
|
379
379
|
output?: unknown;
|
|
380
380
|
error?: InvocationError;
|
|
381
381
|
durationMs?: number;
|
|
382
|
+
retryDelayMs?: number;
|
|
383
|
+
attempt?: number;
|
|
384
|
+
retries?: number;
|
|
385
|
+
maxAttempts?: number;
|
|
382
386
|
}
|
|
383
387
|
export interface DurableLogEvent {
|
|
384
388
|
kind: 'log';
|
package/oclif.manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/runtime-cli",
|
|
3
3
|
"description": "Sanity's Runtime CLI for Blueprints and Functions",
|
|
4
|
-
"version": "17.16.0-next.
|
|
4
|
+
"version": "17.16.0-next.g91e6dd2",
|
|
5
5
|
"author": "Sanity Runtime Team",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
"@oclif/test": "^4.2.0",
|
|
101
101
|
"@playwright/test": "^1.63.0",
|
|
102
102
|
"@sanity-labs/design-tokens": "^0.0.2-alpha.2",
|
|
103
|
-
"@sanity/functions": "^1.
|
|
103
|
+
"@sanity/functions": "^1.8.1",
|
|
104
104
|
"@types/cardinal": "^2.1.1",
|
|
105
105
|
"@types/mime-types": "^3.0.1",
|
|
106
106
|
"@types/node": "^22",
|