- 
                Notifications
    
You must be signed in to change notification settings  - Fork 4
 
fix: Fix Question Service seed-batch bugs and edge cases #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 28 commits
      Commits
    
    
            Show all changes
          
          
            29 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6743375
              
                Fix sync when retrieving all questions
              
              
                xGladiate 51b8417
              
                Replace insertOne with updateOne but with $setOnInsert only
              
              
                xGladiate e2dd31b
              
                Remove unnecessary unique constrain enforcement
              
              
                xGladiate fd3cd12
              
                Remove initial_count from return
              
              
                xGladiate 36982ed
              
                Remove fully initial_count from fetchNonPaidQuestionList
              
              
                xGladiate 8e9081e
              
                Remove done attribute for cursor across files
              
              
                xGladiate d2a2a45
              
                Remove entirely initial_count
              
              
                xGladiate 425e37a
              
                Replace magic number total + 1 with total instead
              
              
                xGladiate 58bbe42
              
                Fix minor naming issues for comments
              
              
                xGladiate 68280a8
              
                Add question-service health check before question retrieval at seed-b…
              
              
                xGladiate f18914a
              
                Modify API in .env.example
              
              
                xGladiate 98c127a
              
                Reformat mongo db connection for easier logging and debugging
              
              
                xGladiate afcdd04
              
                Fix docker run command in question-backend-service README
              
              
                xGladiate a213802
              
                Add infrastructure for failedQuestion storing
              
              
                xGladiate 169d6cd
              
                Refactor fetchNonPaidQuestionInfo to reduce repetition
              
              
                xGladiate c6d5e95
              
                Fix comments
              
              
                xGladiate 7f2a81f
              
                Add debug statement
              
              
                xGladiate 3d1a492
              
                Replace magic number with constant
              
              
                xGladiate bf50547
              
                Remove quesion and dbSchema for failedQuestion
              
              
                xGladiate 3ed9d26
              
                Fix minor comment and error messages
              
              
                xGladiate 93259cd
              
                Add error handling for failed leetcode connection
              
              
                xGladiate 9929a2d
              
                Fix README.md
              
              
                xGladiate 1259069
              
                Fix naming issues
              
              
                xGladiate de94690
              
                Fix README for leetcode-service
              
              
                xGladiate f34d832
              
                Fix magic number issue
              
              
                xGladiate cd2f45f
              
                Remove redundant params
              
              
                xGladiate 43bffb3
              
                Fix reties count and comments for setOnInsert
              
              
                xGladiate 72d9ba5
              
                Add new line for .env.example
              
              
                xGladiate 1f16df4
              
                Fix retries issue
              
              
                xGladiate File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| MONGODB_URI=mongodb+srv://admin:<db-password>@cluster3219.ll0nzkk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster3219 | ||
| ADMIN_TOKEN=<db-password> | ||
| QUESTION_API_URL=http://question-backend:5275/api/v1/questions | ||
| QUESTION_API_URL=http://localhost:5275/api/v1/question-service | ||
| # Maximum number of concurrent LeetCode detail fetches. | ||
| # Optional: defaults to 6 if not set. | ||
| LEETCODE_DETAIL_CONCURRENCY=6 | ||
| LEETCODE_DETAIL_CONCURRENCY=6 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { setTimeout as delay } from "node:timers/promises"; | ||
| 
     | 
||
| type HealthOpts = { | ||
| url?: string; // health check URL | ||
| timeoutMs?: number; // per-attempt timeout | ||
| retries?: number; // number of retries after the first attempt | ||
| }; | ||
| 
     | 
||
| interface HealthResponse { | ||
| ok?: boolean; | ||
| } | ||
| 
     | 
||
| const BATCH_HEALTH_TIMEOUT_MS = 1500; | ||
| const BATCH_HEALTH_RETRIES = 2; | ||
| const BASE_DELAY_MS = 250; | ||
| 
     | 
||
| /** Check the health of the question service. | ||
| * Throws an error if the service is unhealthy or unreachable after retries. | ||
| * @param opts - Options for health check. | ||
| * @returns A promise that resolves to true if healthy, otherwise throws an error. | ||
| */ | ||
| 
     | 
||
| export async function checkQuestionServiceHealth({ | ||
| url = `${process.env.QUESTION_API_URL}/health`, | ||
| timeoutMs = BATCH_HEALTH_TIMEOUT_MS, | ||
| retries = BATCH_HEALTH_RETRIES, | ||
| }: HealthOpts = {}) { | ||
| if (!process.env.QUESTION_API_URL) | ||
| throw new Error("QUESTION_API_URL is not set"); | ||
| 
     | 
||
| let lastErr: unknown; | ||
| 
     | 
||
| for (let attempt = 0; attempt < retries; attempt++) { | ||
| const controller = new AbortController(); | ||
| const t = setTimeout(() => controller.abort(), timeoutMs); | ||
| 
     | 
||
| try { | ||
| const res = await fetch(url, { | ||
| method: "GET", | ||
| headers: { accept: "application/json" }, | ||
| signal: controller.signal, | ||
| }); | ||
| clearTimeout(t); | ||
| 
     | 
||
| if (!res.ok) { | ||
| lastErr = new Error(`Health endpoint returned ${res.status}`); | ||
| } else { | ||
| const body: HealthResponse = (await res.json()) as HealthResponse; | ||
| if (body?.ok !== true) { | ||
| lastErr = new Error("Health endpoint did not return ok=true"); | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
| } catch (err) { | ||
| lastErr = err; | ||
| } | ||
| 
     | 
||
| if (attempt < retries) { | ||
                
      
                  xGladiate marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| await delay(BASE_DELAY_MS * 2 ** attempt); | ||
| } | ||
| } | ||
| 
     | 
||
| throw new Error(`Question service health check failed: ${String(lastErr)}`); | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.