Skip to content

Add bucket initialization to SourceService operations#28

Merged
marcelsamyn merged 1 commit intomainfrom
claude/fix-s3-bucket-error-utK9g
Mar 17, 2026
Merged

Add bucket initialization to SourceService operations#28
marcelsamyn merged 1 commit intomainfrom
claude/fix-s3-bucket-error-utK9g

Conversation

@marcelsamyn
Copy link
Copy Markdown
Owner

Summary

Added lazy initialization of S3/MinIO bucket to ensure it exists before performing operations that require it. This prevents errors when the bucket hasn't been created yet.

Key Changes

  • Added ensureBucket() private method that lazily initializes the bucket on first use
    • Checks if bucket exists, creates it if necessary
    • Caches the promise to avoid redundant checks
    • Resets cache on error to allow retries
  • Added await this.ensureBucket() calls before blob operations in:
    • insertMany() - before handling payloads
    • deleteHard() - before deleting blob
    • getRaw() - before retrieving raw payloads

Implementation Details

The ensureBucket() method uses a promise-based approach to ensure the bucket initialization only happens once, even with concurrent calls. If an error occurs during initialization, the cache is reset so subsequent calls will retry the operation.

https://claude.ai/code/session_014AmzJVb4WYP42hW3LDiXtm

The MinIO bucket was never created - docker-compose starts MinIO but
nothing ensures the SOURCES_BUCKET exists. Added lazy ensureBucket()
that checks/creates the bucket before any S3 operation.

https://claude.ai/code/session_014AmzJVb4WYP42hW3LDiXtm
@marcelsamyn marcelsamyn merged commit c464e99 into main Mar 17, 2026
1 check failed
@marcelsamyn marcelsamyn deleted the claude/fix-s3-bucket-error-utK9g branch March 17, 2026 14:29
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the SourceService by implementing a robust, lazy initialization strategy for S3/MinIO buckets. The primary goal is to prevent runtime errors that could occur if blob storage operations are attempted before the target bucket has been created. By ensuring the bucket's existence on first use and handling potential retries, the service becomes more resilient and self-sufficient in managing its storage dependencies.

Highlights

  • Bucket Initialization Logic: Introduced a new private method, ensureBucket(), within the SourceService class to handle the lazy initialization of the S3/MinIO bucket. This method checks if the bucket exists and creates it if it doesn't.
  • Idempotent Bucket Creation: The ensureBucket() method employs a promise-based caching mechanism (bucketReady) to ensure that the bucket existence check and creation process runs only once, even with concurrent calls. It also resets the cache on error to allow for retries.
  • Integration with Blob Operations: Integrated calls to await this.ensureBucket() before performing blob-related operations in insertMany(), deleteHard(), and getRaw() methods to guarantee the bucket is ready prior to use.
Changelog
  • src/lib/sources.ts
    • Added a bucketReady private property to cache the bucket initialization promise.
    • Implemented a new private asynchronous method ensureBucket() to check for and create the S3/MinIO bucket if it does not exist, with error handling and retry logic.
    • Added a call to await this.ensureBucket() within the insertMany() method before processing payloads.
    • Added a call to await this.ensureBucket() within the deleteHard() method before attempting to delete a blob.
    • Added a call to await this.ensureBucket() within the getRaw() method before retrieving raw payloads.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds lazy initialization for the S3/MinIO bucket. The implementation uses a cached promise to handle concurrency. My review includes a suggestion to refactor the ensureBucket method using async/await for improved readability. Additionally, I've pointed out opportunities in insertMany and fetchRaw to call ensureBucket more lazily, only when a blob operation is certain, which would be a minor efficiency improvement.

Comment on lines +61 to +77
private ensureBucket(): Promise<void> {
if (!this.bucketReady) {
this.bucketReady = this.minioClient
.bucketExists(this.bucket)
.then((exists) => {
if (!exists) {
return this.minioClient.makeBucket(this.bucket);
}
})
.catch((err) => {
// Reset so next call retries
this.bucketReady = null;
throw err;
});
}
return this.bucketReady;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the current promise-based implementation is correct, using async/await can improve readability and make the logic easier to follow. This is more idiomatic in modern TypeScript/JavaScript. The async IIFE (Immediately Invoked Function Expression) pattern ensures the promise is created and assigned synchronously, preserving the lazy-initialization and race-condition-free behavior of your original implementation.

Suggested change
private ensureBucket(): Promise<void> {
if (!this.bucketReady) {
this.bucketReady = this.minioClient
.bucketExists(this.bucket)
.then((exists) => {
if (!exists) {
return this.minioClient.makeBucket(this.bucket);
}
})
.catch((err) => {
// Reset so next call retries
this.bucketReady = null;
throw err;
});
}
return this.bucketReady;
}
private ensureBucket(): Promise<void> {
if (!this.bucketReady) {
this.bucketReady = (async () => {
try {
const exists = await this.minioClient.bucketExists(this.bucket);
if (!exists) {
await this.minioClient.makeBucket(this.bucket);
}
} catch (err) {
// Reset so next call retries
this.bucketReady = null;
throw err;
}
})();
}
return this.bucketReady;
}

.returning();

// 2. Handle payloads
await this.ensureBucket();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For a minor optimization, you could defer calling ensureBucket() until it's certain a blob operation is needed. This avoids the call if all sources are handled as inline content. You could move this call inside the else if (input.fileBuffer) block (line 155) where minioClient.putObject is used. While the performance impact is negligible due to promise caching in ensureBucket, this would make the code slightly more efficient and precise.

and(eq(src.userId, userId), inArray(src.id, sourceIds)),
});
const results: RawResult[] = [];
await this.ensureBucket();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For a minor optimization, you could defer calling ensureBucket() until it's certain a blob operation is needed. This avoids the call if all requested sources have inline content. You could move this call inside the else block (line 239) where minioClient.getObject is used. While the performance impact is negligible due to promise caching in ensureBucket, this would make the code slightly more efficient and precise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants