Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
fix(s3): fix recursion and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Apr 16, 2024
1 parent b00b75d commit 28b0f24
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export const bootstrapCache = async () => {

if (SAVE_CACHE_TO_S3) {
// save cache to S3 every s3CacheIntervalMs
setInterval(saveCacheToS3, s3CacheIntervalMs);
logger.info('Cache will be saved to S3 on an interval', {
intervalMs: s3CacheIntervalMs,
});
setInterval(async () => {
await saveCacheToS3();
}, s3CacheIntervalMs);
}
};

Expand Down Expand Up @@ -212,6 +217,12 @@ export const saveCacheToS3 = async () => {
keyPrefix: string;
}) => {
const files = fs.readdirSync(folderPath);
logger.debug('Uploading folder to S3', {
folderPath,
bucket,
keyPrefix,
files: files.length,
});
await Promise.all(
files.map(async (file) => {
// wrap in a pLimit to avoid resource exhaustion
Expand All @@ -224,29 +235,45 @@ export const saveCacheToS3 = async () => {
keyPrefix,
});
const fileStream = fs.createReadStream(filePath);
const key = path.basename(filePath);
const upload = new Upload({
client: s3,
params: {
Bucket: bucket,
Key: key,
Key: filePath,
Body: fileStream,
},
});

const fileStartTime = Date.now();

// catch errors for a single file
return upload.done().catch((error: unknown) => {
const message =
error instanceof Error ? error : new Error('Unknown error');
logger.error('Failed to upload file to S3', {
error: message,
file,
return upload
.done()
.then(() => {
logger.debug('Successfully uploaded file to S3', {
filePath,
bucket,
keyPrefix,
durationMs: Date.now() - fileStartTime,
});
})
.catch((error: unknown) => {
const message =
error instanceof Error ? error : new Error('Unknown error');
logger.error('Failed to upload file to S3', {
error: message,
file,
});
});
});
} else {
logger.debug('Recursively uploading folder to S3', {
filePath,
bucket,
keyPrefix,
});
// recursively upload folders
return uploadFolder({
folderPath,
folderPath: filePath,
bucket,
keyPrefix: keyPrefix + file + '/',
});
Expand Down

0 comments on commit 28b0f24

Please sign in to comment.