Skip to content

Commit 7b2076d

Browse files
committed
sepa: use p-retry for B2 getFileContent with backoff; add dependency
1 parent c31b313 commit 7b2076d

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

sepa/bun.lock

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

sepa/index-gen/b2.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ListObjectsV2Command,
55
GetObjectCommand,
66
} from "@aws-sdk/client-s3";
7+
import pRetry, { AbortError } from "p-retry";
78

89
function checkEnvVariable(variableName: string) {
910
const value = process.env[variableName];
@@ -63,13 +64,47 @@ export async function listDirectory(directoryName: string) {
6364
export async function getFileContent(fileName: string) {
6465
const { B2_BUCKET_NAME } = getVariables();
6566
const s3 = getS3Client();
66-
const fileContent = await s3.send(
67-
new GetObjectCommand({
68-
Bucket: B2_BUCKET_NAME,
69-
Key: fileName,
70-
})
67+
68+
const result = await pRetry(
69+
async (attempt) => {
70+
try {
71+
const fileContent = await s3.send(
72+
new GetObjectCommand({
73+
Bucket: B2_BUCKET_NAME,
74+
Key: fileName,
75+
})
76+
);
77+
return (await fileContent.Body?.transformToString()) ?? "";
78+
} catch (error) {
79+
const code = (error as any)?.code as string | undefined;
80+
const message = (error as any)?.message as string | undefined;
81+
const isTransient =
82+
code === "ECONNRESET" ||
83+
code === "ENOTFOUND" ||
84+
code === "ETIMEDOUT" ||
85+
code === "ECONNREFUSED" ||
86+
(message && message.includes("socket connection was closed unexpectedly"));
87+
88+
if (!isTransient) {
89+
throw new AbortError(error as Error);
90+
}
91+
92+
console.warn(
93+
`⚠️ B2 getFileContent transient error (attempt ${attempt}): ${code || message}`
94+
);
95+
throw error;
96+
}
97+
},
98+
{
99+
retries: 3,
100+
factor: 2,
101+
minTimeout: 2000,
102+
maxTimeout: 10000,
103+
randomize: false,
104+
}
71105
);
72-
return (await fileContent.Body?.transformToString()) ?? "";
106+
107+
return result;
73108
}
74109

75110
export async function checkFileExists(fileName: string): Promise<boolean> {

sepa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"fast-glob": "^3.3.3",
2727
"jschardet": "^3.1.3",
2828
"p-queue": "^8.0.1",
29+
"p-retry": "6",
2930
"papaparse": "^5.4.1",
3031
"postgres": "^3.4.4",
3132
"waddler": "^0.0.3",

0 commit comments

Comments
 (0)