@sanity/runtime-cli 17.21.4 → 17.21.5-next.g162b704
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 +2 -3
- package/dist/utils/bundles/prepare-asset.js +1 -6
- package/dist/utils/functions/child-process-wrapper.js +15 -4
- package/dist/utils/functions/child-runtime/callback.d.ts +10 -10
- package/dist/utils/functions/child-runtime/callback.js +4 -29
- package/dist/utils/functions/child-runtime/durables/delegate.js +44 -0
- package/dist/utils/functions/child-runtime/durables/durable.d.ts +29 -0
- package/dist/utils/functions/child-runtime/durables/durable.js +91 -0
- package/dist/utils/functions/child-runtime/durables/eventsFactoryPlugin.js +171 -0
- package/dist/utils/functions/child-runtime/durables/fanout.js +24 -0
- package/dist/utils/functions/child-runtime/durables/index.js +45 -0
- package/dist/utils/functions/child-runtime/durables/logger.js +31 -0
- package/dist/utils/functions/child-runtime/durables/retry.js +60 -0
- package/dist/utils/functions/child-runtime/durables/run.js +23 -0
- package/dist/utils/functions/child-runtime/durables/utils.js +20 -0
- package/dist/utils/functions/child-runtime/durables/wait.js +11 -0
- package/dist/utils/functions/child-runtime/durables/waitForCallback.js +40 -0
- package/dist/utils/functions/child-runtime/durables/waitForCondition.js +44 -0
- package/oclif.manifest.json +1 -1
- package/package.json +7 -5
- package/dist/utils/functions/child-runtime/durable.d.ts +0 -18
- package/dist/utils/functions/child-runtime/durable.js +0 -264
- package/dist/utils/functions/child-runtime/getBatchResults.d.ts +0 -6
- package/dist/utils/functions/child-runtime/getBatchResults.js +0 -74
- package/dist/utils/functions/child-runtime/step-history.d.ts +0 -15
- package/dist/utils/functions/child-runtime/step-history.js +0 -52
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import {randomUUID} from 'node:crypto'
|
|
2
|
-
import {performance} from 'node:perf_hooks'
|
|
3
|
-
import {inspect} from 'node:util'
|
|
4
|
-
import {getBatchResults} from './getBatchResults.js'
|
|
5
|
-
import {createStepHistory} from './step-history.js'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @param duration
|
|
10
|
-
* @return {number}
|
|
11
|
-
*/
|
|
12
|
-
const durationToMs = (duration = {}) => {
|
|
13
|
-
const {days = 0, hours = 0, minutes = 0, seconds = 0} = duration
|
|
14
|
-
return (((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
19
|
-
* @param args
|
|
20
|
-
*/
|
|
21
|
-
const formatArgs = (args) =>
|
|
22
|
-
args.map((a) => (typeof a === 'string' ? a : inspect(a, {depth: null}))).join(' ')
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Runs a durable function handler
|
|
26
|
-
* @param handler - callback being run
|
|
27
|
-
* @param context - data to send to the operation
|
|
28
|
-
* @param event - the step being run
|
|
29
|
-
* @param [branchName] - the name of the branch being run
|
|
30
|
-
* @param io
|
|
31
|
-
*/
|
|
32
|
-
export async function runDurable(handler, {context, event, branchName}, io) {
|
|
33
|
-
const history = createStepHistory()
|
|
34
|
-
let currentStep = null
|
|
35
|
-
|
|
36
|
-
const streamLog = (level, args) => {
|
|
37
|
-
const date = new Date()
|
|
38
|
-
const timestamp = `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
|
39
|
-
|
|
40
|
-
return io.emit({
|
|
41
|
-
timestamp,
|
|
42
|
-
kind: 'log',
|
|
43
|
-
phase: 'log',
|
|
44
|
-
step: currentStep,
|
|
45
|
-
level,
|
|
46
|
-
message: formatArgs(args),
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const logger = {
|
|
51
|
-
log: (...a) => streamLog('info', a),
|
|
52
|
-
info: (...a) => streamLog('info', a),
|
|
53
|
-
warn: (...a) => streamLog('warn', a),
|
|
54
|
-
error: (...a) => streamLog('error', a),
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const emitFailure = (pending) => {
|
|
58
|
-
history.settle(pending)
|
|
59
|
-
io.emit(pending.event)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const settleUnretried = () => {
|
|
63
|
-
const pending = history.getPending()
|
|
64
|
-
if (pending !== null) {
|
|
65
|
-
emitFailure(pending)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const track = async (name, kind, produce, retry) => {
|
|
70
|
-
settleUnretried()
|
|
71
|
-
const {index, saved} = history.nextStep()
|
|
72
|
-
|
|
73
|
-
if (saved?.phase === 'complete') {
|
|
74
|
-
return saved.output
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const maxAttempts = retry?.maxAttempts ?? 1
|
|
78
|
-
const retries = Math.max((retry?.maxAttempts ?? 1) - 1, 0)
|
|
79
|
-
const waitingOnRetry = typeof saved?.retryDelayMs === 'number'
|
|
80
|
-
|
|
81
|
-
if (saved?.phase === 'error' && !waitingOnRetry) {
|
|
82
|
-
throw saved.output
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const attempt = (saved?.attempt ?? 0) + 1
|
|
86
|
-
const prev = currentStep
|
|
87
|
-
currentStep = name
|
|
88
|
-
|
|
89
|
-
io.emit({name, kind, phase: 'start'})
|
|
90
|
-
const startedAt = performance.now()
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
const value = await produce(attempt)
|
|
94
|
-
const completeEvent = {
|
|
95
|
-
name,
|
|
96
|
-
kind,
|
|
97
|
-
phase: 'complete',
|
|
98
|
-
output: value,
|
|
99
|
-
durationMs: performance.now() - startedAt,
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
history.recordSuccess(index, completeEvent)
|
|
103
|
-
io.emit(completeEvent)
|
|
104
|
-
|
|
105
|
-
return value
|
|
106
|
-
} catch (err) {
|
|
107
|
-
const errorEvent = {
|
|
108
|
-
name,
|
|
109
|
-
kind,
|
|
110
|
-
phase: 'error',
|
|
111
|
-
error: {
|
|
112
|
-
name: err?.name ?? 'Error',
|
|
113
|
-
message: err?.message ?? String(err),
|
|
114
|
-
stack: err?.stack ?? '',
|
|
115
|
-
},
|
|
116
|
-
durationMs: performance.now() - startedAt,
|
|
117
|
-
attempt,
|
|
118
|
-
retries,
|
|
119
|
-
maxAttempts,
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const retryPending = history.recordFailure(index, {
|
|
123
|
-
event: errorEvent,
|
|
124
|
-
error: err,
|
|
125
|
-
delayMs: retry?.delay ? durationToMs(retry.delay) : 0,
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
if (!retryPending) {
|
|
129
|
-
io.emit(errorEvent)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
throw err
|
|
133
|
-
} finally {
|
|
134
|
-
currentStep = prev
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const FANOUT_STATUS = {
|
|
139
|
-
STARTED: 'STARTED',
|
|
140
|
-
ALL_COMPLETED: 'ALL_COMPLETED',
|
|
141
|
-
FAILURE_TOLERANCE_EXCEEDED: 'FAILURE_TOLERANCE_EXCEEDED',
|
|
142
|
-
SUCCEEDED: 'SUCCEEDED',
|
|
143
|
-
FAILED: 'FAILED',
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const step = {
|
|
147
|
-
run: ({name, handler: fn, retry}) =>
|
|
148
|
-
track(name || '', 'run', (attempt) => fn({logger, attempt}), retry),
|
|
149
|
-
delegate: ({name, handler, event, retry}) =>
|
|
150
|
-
track(
|
|
151
|
-
name || '',
|
|
152
|
-
'delegate',
|
|
153
|
-
() => {
|
|
154
|
-
const fn = typeof handler === 'string' ? handler : handler?.name
|
|
155
|
-
if (!fn) {
|
|
156
|
-
throw new Error('No function name provided for delegation')
|
|
157
|
-
}
|
|
158
|
-
return io.invoke(fn, event?.data ?? {})
|
|
159
|
-
},
|
|
160
|
-
retry,
|
|
161
|
-
),
|
|
162
|
-
fanout: async ({name, branches}) => {
|
|
163
|
-
if (
|
|
164
|
-
!Array.isArray(branches) ||
|
|
165
|
-
Array.from(branches).some((branch) => typeof branch !== 'function')
|
|
166
|
-
) {
|
|
167
|
-
throw new TypeError('fanout branches must be an array of functions')
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const outcomes = branches.map((_, index) => ({index, status: FANOUT_STATUS.STARTED}))
|
|
171
|
-
return new Promise((resolve, reject) => {
|
|
172
|
-
const execs = branches.map(async (branch, index) => {
|
|
173
|
-
try {
|
|
174
|
-
const branchName = `${name || 'fanout'} / branch ${index}`
|
|
175
|
-
const result = await runDurable(branch, {context, event, branchName}, io)
|
|
176
|
-
outcomes[index] = {index, status: FANOUT_STATUS.SUCCEEDED, result}
|
|
177
|
-
} catch (err) {
|
|
178
|
-
const error = err instanceof Error ? err : new Error(String(err))
|
|
179
|
-
outcomes[index] = {index, status: FANOUT_STATUS.FAILED, error}
|
|
180
|
-
}
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
Promise.allSettled(execs)
|
|
184
|
-
.then(() => {
|
|
185
|
-
resolve(getBatchResults(outcomes, FANOUT_STATUS.ALL_COMPLETED))
|
|
186
|
-
})
|
|
187
|
-
.catch(() => {
|
|
188
|
-
reject(reject)
|
|
189
|
-
})
|
|
190
|
-
})
|
|
191
|
-
},
|
|
192
|
-
waitForCondition: ({name, initial, poller, next}) =>
|
|
193
|
-
track(name || '', 'wait-for-condition', async () => {
|
|
194
|
-
let state = initial
|
|
195
|
-
let attempt = 1
|
|
196
|
-
let polling = true
|
|
197
|
-
while (polling) {
|
|
198
|
-
state = await poller(state, {logger, attempt})
|
|
199
|
-
const decision = next(state, {attempt})
|
|
200
|
-
if (decision.shouldRetry) {
|
|
201
|
-
await io.wait(durationToMs(decision.delay))
|
|
202
|
-
attempt += 1
|
|
203
|
-
} else {
|
|
204
|
-
polling = false
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return state
|
|
208
|
-
}),
|
|
209
|
-
waitForCallback: ({name, submitter, timeout, retry}) => {
|
|
210
|
-
return track(
|
|
211
|
-
name || '',
|
|
212
|
-
'wait-for-callback',
|
|
213
|
-
async () => {
|
|
214
|
-
const callbackId = randomUUID()
|
|
215
|
-
const result = io.registerCallback({
|
|
216
|
-
callbackId,
|
|
217
|
-
name,
|
|
218
|
-
timeout: durationToMs(timeout),
|
|
219
|
-
})
|
|
220
|
-
io.emit({name, kind: 'wait-for-callback', phase: 'waiting', callbackId})
|
|
221
|
-
try {
|
|
222
|
-
await submitter({callbackId, logger})
|
|
223
|
-
} catch (err) {
|
|
224
|
-
io.cancelCallback(callbackId)
|
|
225
|
-
throw err
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return await result
|
|
229
|
-
},
|
|
230
|
-
retry,
|
|
231
|
-
)
|
|
232
|
-
},
|
|
233
|
-
wait: ({name, duration}) =>
|
|
234
|
-
track(name || '', 'wait', async () => {
|
|
235
|
-
await io.wait(durationToMs(duration))
|
|
236
|
-
return undefined
|
|
237
|
-
}),
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
let result
|
|
241
|
-
let done = false
|
|
242
|
-
const handlerArgs = branchName !== undefined ? {step, logger} : {context, event, step, logger}
|
|
243
|
-
while (!done) {
|
|
244
|
-
history.startRun()
|
|
245
|
-
try {
|
|
246
|
-
result = await handler(handlerArgs)
|
|
247
|
-
settleUnretried()
|
|
248
|
-
done = true
|
|
249
|
-
} catch (err) {
|
|
250
|
-
const pending = history.getPending()
|
|
251
|
-
if (pending === null) {
|
|
252
|
-
throw err
|
|
253
|
-
}
|
|
254
|
-
if (err?.name === 'UnrecoverableError') {
|
|
255
|
-
emitFailure(pending)
|
|
256
|
-
throw err
|
|
257
|
-
}
|
|
258
|
-
io.emit({...pending.event, retryDelayMs: pending.delayMs})
|
|
259
|
-
await io.wait(pending.delayMs)
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return result
|
|
264
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Maps to runtime APIs from @sanity/functions
|
|
3
|
-
* @param outcomes - array of outcomes from the batch execution, each outcome should have a status and result or error
|
|
4
|
-
* @param reason - a string indicating the reason for the batch result, e.g., 'ALL_COMPLETED' or 'FAILED'
|
|
5
|
-
*/
|
|
6
|
-
export const getBatchResults = (outcomes, reason) => {
|
|
7
|
-
/**
|
|
8
|
-
* Returns an array of all outcomes, regardless of their status.
|
|
9
|
-
*/
|
|
10
|
-
const all = outcomes.map((outcome) => ({...outcome}))
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Returns an array of outcomes that have failed (status 'FAILED').
|
|
14
|
-
*/
|
|
15
|
-
const failed = () =>
|
|
16
|
-
all.filter((outcome) => outcome.error !== undefined && outcome.status === 'FAILED')
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Returns an array of outcomes that have succeeded (status 'SUCCEEDED').
|
|
20
|
-
*/
|
|
21
|
-
const succeeded = () => all.filter((outcome) => outcome.status === 'SUCCEEDED')
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Returns an array of outcomes that are still in progress (status 'STARTED').
|
|
25
|
-
*/
|
|
26
|
-
const started = () => all.filter((outcome) => outcome.status === 'STARTED')
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Returns the count of successful outcomes (status 'SUCCEEDED')
|
|
30
|
-
*/
|
|
31
|
-
const successCount = all.filter((item) => item.status === 'SUCCEEDED').length
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Returns the count of failed outcomes (status 'FAILED')
|
|
35
|
-
*/
|
|
36
|
-
const failureCount = all.filter((item) => item.status === 'FAILED').length
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns an array of results from successful outcomes (status 'SUCCEEDED').
|
|
40
|
-
*/
|
|
41
|
-
const getResults = () => succeeded().map((item) => item.result)
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Returns an array of errors from failed outcomes (status 'FAILED').
|
|
45
|
-
*/
|
|
46
|
-
const getErrors = () => failed().map((item) => item.error)
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Throws the first error found in the failed outcomes (status 'FAILED').
|
|
50
|
-
*/
|
|
51
|
-
const throwIfError = () => {
|
|
52
|
-
const failure = all.find((outcome) => outcome.status === 'FAILED')
|
|
53
|
-
if (failure?.error) {
|
|
54
|
-
throw failure.error
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
all,
|
|
60
|
-
status: failureCount > 0 ? 'FAILED' : 'SUCCEEDED',
|
|
61
|
-
hasFailure: failureCount > 0,
|
|
62
|
-
failed,
|
|
63
|
-
succeeded,
|
|
64
|
-
started,
|
|
65
|
-
reason,
|
|
66
|
-
successCount,
|
|
67
|
-
failureCount,
|
|
68
|
-
startedCount: started().length,
|
|
69
|
-
totalCount: all.length,
|
|
70
|
-
getResults,
|
|
71
|
-
getErrors,
|
|
72
|
-
throwIfError,
|
|
73
|
-
}
|
|
74
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type {DurableStepEvent} from '../../types.js'
|
|
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
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
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}
|