Skip to content

Commit 0afbf99

Browse files
committed
retry
1 parent 4bd9659 commit 0afbf99

File tree

3 files changed

+111
-42
lines changed

3 files changed

+111
-42
lines changed

dist/index.js

Lines changed: 46 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SupabaseAPI.ts

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,44 @@ import {
55
SubmissionResponse
66
} from './api/adminServiceSchemas.js'
77
import { getInput } from '@actions/core'
8+
9+
async function sleep(ms: number): Promise<void> {
10+
return new Promise((resolve) => setTimeout(resolve, ms))
11+
}
12+
13+
async function retryWithExponentialBackoff<T>(
14+
operation: () => Promise<T>,
15+
maxRetries: number = 5,
16+
baseDelay: number = 1000
17+
): Promise<T> {
18+
let lastError: Error
19+
20+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
21+
try {
22+
return await operation()
23+
} catch (error) {
24+
lastError = error as Error
25+
26+
if (attempt === maxRetries) {
27+
throw lastError
28+
}
29+
30+
// Calculate delay with exponential backoff
31+
// For the last attempt (5th), ensure at least 30 seconds delay
32+
let delay = baseDelay * Math.pow(2, attempt - 1)
33+
if (attempt === maxRetries - 1) {
34+
delay = Math.max(delay, 30000) // Ensure at least 30 seconds before last try
35+
}
36+
37+
console.log(
38+
`Attempt ${attempt} failed: ${lastError.message}. Retrying in ${delay}ms...`
39+
)
40+
await sleep(delay)
41+
}
42+
}
43+
44+
throw lastError!
45+
}
846
export async function submitFeedback(
947
body: GradingScriptResult,
1048
token: string,
@@ -13,31 +51,34 @@ export async function submitFeedback(
1351
}
1452
): Promise<GradeResponse> {
1553
const gradingServerURL = getInput('grading_server')
16-
const response = await fetch(
17-
`${gradingServerURL}/functions/v1/autograder-submit-feedback${
18-
queryParams?.autograder_regression_test_id
19-
? `?autograder_regression_test_id=${queryParams.autograder_regression_test_id}`
20-
: ''
21-
}`,
22-
{
23-
method: 'POST',
24-
body: JSON.stringify(body),
25-
headers: {
26-
'Content-Type': 'application/json',
27-
Authorization: `${token}`
54+
55+
return retryWithExponentialBackoff(async () => {
56+
const response = await fetch(
57+
`${gradingServerURL}/functions/v1/autograder-submit-feedback${
58+
queryParams?.autograder_regression_test_id
59+
? `?autograder_regression_test_id=${queryParams.autograder_regression_test_id}`
60+
: ''
61+
}`,
62+
{
63+
method: 'POST',
64+
body: JSON.stringify(body),
65+
headers: {
66+
'Content-Type': 'application/json',
67+
Authorization: `${token}`
68+
}
2869
}
29-
}
30-
)
31-
if (!response.ok) {
32-
throw new Error(`Failed to submit feedback: ${response.statusText}`)
33-
}
34-
const resp = (await response.json()) as GradeResponse
35-
if (resp.error) {
36-
throw new Error(
37-
`Failed to submit feedback: ${resp.error.message} ${resp.error.details}`
3870
)
39-
}
40-
return resp
71+
if (!response.ok) {
72+
throw new Error(`Failed to submit feedback: ${response.statusText}`)
73+
}
74+
const resp = (await response.json()) as GradeResponse
75+
if (resp.error) {
76+
throw new Error(
77+
`Failed to submit feedback: ${resp.error.message} ${resp.error.details}`
78+
)
79+
}
80+
return resp
81+
})
4182
}
4283

4384
export async function createSubmission(token: string) {

0 commit comments

Comments
 (0)