Skip to content

Commit e8e73f1

Browse files
authored
fix: polling logic for app action call (#1806)
1 parent 43b654d commit e8e73f1

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

lib/adapters/REST/endpoints/app-action-call.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import * as raw from './raw'
99
import { RestEndpoint } from '../types'
1010
import { GetAppActionCallDetailsParams, GetAppActionCallParams } from '../../../common-types'
11+
import { isSuccessful, shouldRePoll, waitFor } from '../../../common-utils'
1112

1213
export const create: RestEndpoint<'AppActionCall', 'create'> = (
1314
http: AxiosInstance,
@@ -50,28 +51,46 @@ async function callAppActionResult(
5051
callId: string
5152
}
5253
) {
53-
const appActionResponse = await getCallDetails(http, { ...params, callId })
54+
const result = await getCallDetails(http, { ...params, callId })
5455

55-
if (appActionResponse && appActionResponse.sys && appActionResponse.sys.id) {
56-
resolve(appActionResponse)
56+
// The lambda failed or returned a 404, so we shouldn't re-poll anymore
57+
if (result?.response?.statusCode && !isSuccessful(result?.response?.statusCode)) {
58+
const error = new Error(result.response.body)
59+
reject(error)
60+
}
61+
62+
if (isSuccessful(result.statusCode)) {
63+
resolve(result)
64+
}
65+
66+
// The logs are not ready yet. Continue waiting for them
67+
if (shouldRePoll(result.statusCode) && checkCount !== retries) {
68+
waitFor(retryInterval)
69+
70+
await callAppActionResult(http, params, {
71+
resolve,
72+
reject,
73+
retryInterval,
74+
retries,
75+
checkCount,
76+
callId,
77+
})
5778
} else if (checkCount === retries) {
5879
const error = new Error()
5980
error.message = 'The app action response is taking longer than expected to process.'
6081
reject(error)
6182
} else {
6283
checkCount++
63-
setTimeout(
64-
() =>
65-
callAppActionResult(http, params, {
66-
resolve,
67-
reject,
68-
retryInterval,
69-
retries,
70-
checkCount,
71-
callId,
72-
}),
73-
retryInterval
74-
)
84+
waitFor(retryInterval)
85+
86+
await callAppActionResult(http, params, {
87+
resolve,
88+
reject,
89+
retryInterval,
90+
retries,
91+
checkCount,
92+
callId,
93+
})
7594
}
7695
}
7796

lib/common-utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ export const wrapCursorPaginatedCollection =
3636
// @ts-expect-error
3737
return collectionData
3838
}
39+
export function isSuccessful(statusCode: number) {
40+
return statusCode < 300
41+
}
42+
43+
export function shouldRePoll(statusCode: number) {
44+
return [404, 422, 429].includes(statusCode)
45+
}
46+
47+
export async function waitFor(ms = 1000) {
48+
return new Promise((resolve) => setTimeout(resolve, ms))
49+
}

0 commit comments

Comments
 (0)