Add bucket initialization to SourceService operations#28
Conversation
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
Summary of ChangesHello, 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 Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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.
| 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(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
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
ensureBucket()private method that lazily initializes the bucket on first useawait this.ensureBucket()calls before blob operations in:insertMany()- before handling payloadsdeleteHard()- before deleting blobgetRaw()- before retrieving raw payloadsImplementation 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