Skip to content
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

feat: Add Worker to IngredientsController #649

Open
wants to merge 3 commits into
base: staging
Choose a base branch
from

Conversation

philipbrembeck
Copy link
Contributor

@philipbrembeck philipbrembeck commented Dec 9, 2024

The v1 Ingredients Endpoint receives a worker in this PR for improved performance.

In the same go, I opted to use Bun at Runtime in the Dockerfile, for mainly faster start up times but also better overall performance.

Summary by Sourcery

Add a worker thread to the IngredientsV1Controller for asynchronous ingredient categorization and switch to Bun in the Dockerfile for improved performance.

New Features:

  • Introduce a worker thread in the IngredientsV1Controller to handle ingredient categorization asynchronously, improving performance.

Enhancements:

  • Replace Node.js with Bun in the Dockerfile to achieve faster startup times and better overall performance.

Copy link

stackblitz bot commented Dec 9, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

sourcery-ai bot commented Dec 9, 2024

Reviewer's Guide by Sourcery

This PR improves the performance of the v1 Ingredients endpoint by moving the ingredient matching logic to a Worker thread and switches the runtime environment from Node.js to Bun. The sophisticated matching logic is moved from the controller to a dedicated worker file, allowing for parallel processing of ingredients categorization. The Docker image is updated to use Bun instead of Node.js for better startup times and performance.

Sequence diagram for ingredient categorization using Worker

sequenceDiagram
    participant Controller as IngredientsV1Controller
    participant Worker as IngredientWorker
    Controller->>Worker: Start Worker with ingredients
    Worker-->>Controller: Categorized results
    Controller->>Controller: Process results
    Controller->>Controller: Translate results if needed
    Controller->>Controller: Send response
Loading

File-Level Changes

Change Details Files
Implementation of Worker thread for ingredient matching
  • Created new worker thread implementation for ingredient categorization
  • Moved sophisticated matching logic to the worker
  • Added worker thread communication setup with proper error handling
  • Refactored ingredient categorization to use the worker thread asynchronously
src/ingredients/v1/ingredients.controller.ts
src/ingredients/v1/ingredient.worker.ts
Improved translation handling in the controller
  • Restructured translation result handling with clearer variable names
  • Improved separation of translated results for different categories
src/ingredients/v1/ingredients.controller.ts
Runtime environment switch to Bun
  • Changed base Docker image from node:lts-alpine to oven/bun:1-alpine
  • Removed explicit CMD instruction as it's handled by Bun
Dockerfile
Test suite updates
  • Removed redundant comments
  • Updated test descriptions for clarity
src/ingredients/tests/ingredientsv1.controller.spec.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @philipbrembeck - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider restoring the comment about priority ordering (not_vegan > maybe_not_vegan > vegan) in the tests as it documents important implementation behavior.
  • It would be helpful to document the specific performance improvements seen with Bun vs Node.js in a comment or the PR description.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

return false;
worker.on("message", resolve);
worker.on("error", reject);
worker.on("exit", (code) => {
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): The promise is not resolved when worker exits normally with code 0

This could cause requests to hang indefinitely. Consider resolving the promise on normal exit.

isVegan: this.isVegan,
},
}
);
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Array slicing for translation results is fragile and error-prone

Consider using a more robust approach, such as maintaining the category information alongside each ingredient through the translation process.

Suggested implementation:

          // Prepare ingredients with their categories
          const ingredientsWithCategories = [
            ...notVeganResult.map(item => ({ text: item, category: 'notVegan' })),
            ...maybeNotVeganResult.map(item => ({ text: item, category: 'maybeNotVegan' })),
            ...veganResult.map(item => ({ text: item, category: 'vegan' }))
          ];

          // Get translations while preserving category information
          const translatedIngredients = backTranslated.map((translation, index) => ({
            text: translation,
            category: ingredientsWithCategories[index].category
          }));

          // Organize translations by category
          const translatedNotVegan = translatedIngredients
            .filter(item => item.category === 'notVegan')
            .map(item => item.text);
          const translatedMaybeNotVegan = translatedIngredients
            .filter(item => item.category === 'maybeNotVegan')
            .map(item => item.text);
          const translatedVegan = translatedIngredients
            .filter(item => item.category === 'vegan')
            .map(item => item.text);

Make sure that:

  1. The backTranslated array contains translations in the same order as the original ingredients
  2. All ingredient arrays (notVeganResult, maybeNotVeganResult, veganResult) contain string values
  3. The translation service is properly handling the combined array of all ingredients

veganResult: string[];
unknownResult: string[];
}> {
return new Promise((resolve, reject) => {
Copy link

Choose a reason for hiding this comment

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

suggestion (performance): Worker thread is not explicitly terminated after use

Consider adding worker.terminate() after the message is received to clean up resources promptly.

Suggested implementation:

    return new Promise((resolve, reject) => {
      const worker = new Worker(
        path.resolve(__dirname, "ingredient.worker.js"),
        {
          workerData: {
            ingredients,
            isNotVegan: this.isNotVegan,
            isMaybeNotVegan: this.isMaybeNotVegan,
            isVegan: this.isVegan,
          },
        }
      );

      worker.on('message', (result) => {
        worker.terminate();
        resolve(result);
      });

      worker.on('error', (error) => {
        worker.terminate();
        reject(error);
      });

Since I can only see part of the code, you'll need to:

  1. Make sure any existing message and error handlers are merged with this change
  2. Keep any additional error handling or message processing logic that may exist

maybeNotVeganResult,
veganResult,
unknownResult,
});
Copy link

Choose a reason for hiding this comment

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

issue (testing): Missing worker-specific test cases

The implementation has been moved to a worker, but there are no tests verifying the worker's behavior, error handling, or edge cases. Consider adding tests for worker initialization failures, worker communication errors, and proper cleanup.

const wordBoundaryRegex = new RegExp(`\\b${normalizedIngredient}\\b`);
if (list.some((item) => wordBoundaryRegex.test(item.replace(/\s+/g, ""))))
return true;
private runWorker(ingredients: string[]): Promise<{
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the worker thread implementation into a simpler Promise-based service class

The worker thread implementation adds unnecessary complexity for string matching operations. Consider simplifying to a Promise-based service class:

// ingredients.service.ts
export class IngredientsService {
  async categorizeIngredients(ingredients: string[]) {
    return {
      notVeganResult: ingredients.filter(item => 
        this.sophisticatedMatch(item, this.isNotVegan)),
      maybeNotVeganResult: ingredients.filter(item => 
        !this.sophisticatedMatch(item, this.isNotVegan) && 
        this.sophisticatedMatch(item, this.isMaybeNotVegan)),
      veganResult: ingredients.filter(item =>
        this.sophisticatedMatch(item, this.isVegan)),
      unknownResult: ingredients.filter(item =>
        !this.sophisticatedMatch(item, this.isNotVegan) &&
        !this.sophisticatedMatch(item, this.isMaybeNotVegan) &&
        !this.sophisticatedMatch(item, this.isVegan))
    };
  }
}

// controller
const results = await this.ingredientsService.categorizeIngredients(response);

This achieves non-blocking behavior while eliminating worker thread complexity. The translation section can also be simplified by using destructuring:

const translatedResults = backTranslated.reduce((acc, _, i) => {
  if (i < notVeganResult.length) acc.notVegan.push(backTranslated[i]);
  else if (i < notVeganResult.length + maybeNotVeganResult.length) 
    acc.maybeNotVegan.push(backTranslated[i]);
  // ... etc
  return acc;
}, { notVegan: [], maybeNotVegan: [], vegan: [], unknown: [] });

src/ingredients/v1/ingredient.worker.ts Show resolved Hide resolved
src/ingredients/v1/ingredient.worker.ts Show resolved Hide resolved
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.

1 participant