Skip to content

Conversation

@adityachoudhari26
Copy link
Contributor

@adityachoudhari26 adityachoudhari26 commented Apr 4, 2025

Summary by CodeRabbit

  • New Features

    • Introduced asynchronous processing for environment updates, automatically detecting and queuing changes to resource selections during environment creation or updates.
    • Added a new worker for handling environment selector updates, enhancing resource management capabilities.
    • Enhanced the environment creation process to trigger updates for resource selectors when discrepancies are detected.
    • Introduced a new channel for environment selector updates, expanding event handling capabilities.
  • Bug Fixes

    • Improved error handling for database operations related to environment updates.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 4, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This update introduces a new worker that processes environment selector update events. The changes add a dedicated worker implementation to handle resource matching, system retrieval, and the creation of release targets when environment selectors are modified. Additionally, both the API route and environment router now enqueue update jobs when a resource selector changes. A new channel type is added to standardize event handling, and the worker mapping is updated accordingly.

Changes

File(s) Summary
apps/event-worker/src/workers/env-selector-update.ts, apps/event-worker/src/workers/index.ts Introduces the envSelectorUpdateWorker that fetches resources, retrieves systems, creates release targets, and adds a new mapping for the Channel.EnvironmentSelectorUpdate event.
apps/webservice/src/app/api/v1/environments/route.ts, packages/api/src/router/environment.ts Enhances environment update handling by comparing current and incoming resource selectors and enqueuing an update job if a change is detected, utilizing getQueue and related logic.
packages/events/src/types.ts Adds a new enum value EnvironmentSelectorUpdate and updates ChannelMap with a type that includes oldSelector for proper event type matching.
packages/events/package.json Adds a new dependency @ctrlplane/validators to the dependencies section.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant API as API Handler (POST /environments)
    participant Q as Queue System
    participant W as EnvSelectorUpdate Worker
    participant DB as Database

    C->>API: Send POST request with environment data
    API->>DB: Query for existing environment record
    DB-->>API: Return existing environment data
    API->>API: Compare resource selectors
    API->>Q: Enqueue job if selector change detected
    Q->>W: Dispatch EnvironmentSelectorUpdate job
    W->>DB: Query for system & newly matched resources
    DB-->>W: Return system details and resource list
    W->>DB: Insert new release targets (if applicable)
    W-->>W: Log errors if system is not found
Loading

Possibly related PRs

  • feat: Env selector update worker #449: The changes in the main PR are directly related to those in the retrieved PR, as both introduce the envSelectorUpdateWorker and involve modifications to the same functions and classes within the env-selector-update.ts file.
  • feat: Deployment worker #446: The changes in the main PR are related to the addition of the envSelectorUpdateWorker in the workers object, which is a similar modification to the workers object in the retrieved PR where a new worker is assigned to the Channel.NewDeployment.

Suggested reviewers

  • jsbroks

Poem

I hop through lines of code with cheer,
A worker born to make things clear.
Old selectors give way to the new,
As release targets bloom into view.
With a twitch of whiskers, I celebrate the feat—
CodeRabbit’s rhythm, swift and neat! 🐇


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ffd0a6e and 784c7f2.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (1)
  • packages/events/package.json (1 hunks)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
apps/event-worker/src/workers/env-selector-update.ts (2)

14-45: Consider optimizing large-scale resource comparisons.
If the number of resources is large, repeatedly fetching and comparing entire arrays could become expensive. You might consider incremental or more selective approaches (e.g., limiting pagination, using IDs only, leveraging intersections in the database) to avoid potential performance bottlenecks.


47-59: Enhance documentation clarity.
The docstring references “environment data with old and new selectors” but doesn’t explicitly mention the oldSelector parameter in the @param section. Including it helps clarify the difference between oldSelector and the environment.resourceSelector for future maintainers.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 08a4016 and dbc7abc.

📒 Files selected for processing (5)
  • apps/event-worker/src/workers/env-selector-update.ts (1 hunks)
  • apps/event-worker/src/workers/index.ts (2 hunks)
  • apps/webservice/src/app/api/v1/environments/route.ts (2 hunks)
  • packages/api/src/router/environment.ts (2 hunks)
  • packages/events/src/types.ts (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{ts,tsx}`: **Note on Error Handling:** Avoid strict enforcement of try/catch blocks. Code may use early returns, Promise chains (.then().catch()), or other patterns for error...

**/*.{ts,tsx}: Note on Error Handling:
Avoid strict enforcement of try/catch blocks. Code may use early returns, Promise chains (.then().catch()), or other patterns for error handling. These are acceptable as long as they maintain clarity and predictability.

  • packages/events/src/types.ts
  • apps/event-worker/src/workers/index.ts
  • apps/webservice/src/app/api/v1/environments/route.ts
  • packages/api/src/router/environment.ts
  • apps/event-worker/src/workers/env-selector-update.ts
🧬 Code Definitions (2)
apps/event-worker/src/workers/index.ts (1)
apps/event-worker/src/workers/env-selector-update.ts (1)
  • envSelectorUpdateWorker (60-96)
apps/webservice/src/app/api/v1/environments/route.ts (2)
packages/db/src/schema/environment.ts (1)
  • environment (57-82)
packages/db/src/upsert-env.ts (1)
  • upsertEnv (34-80)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: Lint
🔇 Additional comments (11)
apps/event-worker/src/workers/index.ts (2)

6-6: LGTM: Import for the new worker added correctly.

The new import for the environment selector update worker is correctly added and follows the naming and file path conventions of other workers in the file.


20-20: LGTM: Worker properly registered for the new channel.

The new environment selector update worker is correctly registered in the workers map, mapping it to the Channel.EnvironmentSelectorUpdate enum value. This ensures the worker will process the appropriate channel events.

packages/events/src/types.ts (3)

2-2: LGTM: Import for ResourceCondition type added correctly.

The ResourceCondition type is imported from the appropriate module for use in the ChannelMap type definition.


16-16: LGTM: New channel type added to enum.

The EnvironmentSelectorUpdate enum value is correctly added to the Channel enum with an appropriate string identifier that follows the naming pattern of other channels.


31-33: LGTM: ChannelMap type extended correctly for new channel.

The ChannelMap type is properly extended to include the data structure for the environment selector update channel. The type definition correctly includes the environment object along with the oldSelector property to track changes.

packages/api/src/router/environment.ts (2)

32-32: LGTM: New imports added correctly.

The Channel and getQueue imports are correctly added from the @ctrlplane/events module for handling environment selector update events.


315-319: LGTM: Environment selector update event properly enqueued.

The code correctly enqueues an environment selector update event when a resource selector changes. The job includes the updated environment information and the old selector value, providing all necessary context for the worker to process the changes.

apps/webservice/src/app/api/v1/environments/route.ts (3)

7-9: LGTM: New imports added correctly.

The imports are correctly updated to include takeFirstOrNull, Channel, and getQueue, which are needed for the new functionality that checks existing environments and enqueues update events.


53-58: LGTM: Query for existing environment added correctly.

The code properly queries for an existing environment with the same name to determine if selector changes need to be processed. The use of takeFirstOrNull is appropriate to handle cases where no environment exists.


64-72: LGTM: Environment selector update event properly enqueued.

The code correctly checks if an existing environment exists and if its resource selector has changed. When both conditions are met, it properly enqueues an environment selector update event with the updated environment and the old selector value.

apps/event-worker/src/workers/env-selector-update.ts (1)

1-2: Imports look good.
No issues with these imports; they correctly reference types and maintain clarity.

Comment on lines 60 to 96
export const envSelectorUpdateWorker = createWorker(
Channel.EnvironmentSelectorUpdate,
async (job) => {
const { oldSelector, ...environment } = job.data;
const system = await db.query.environment
.findFirst({
where: eq(schema.environment.id, environment.id),
with: { system: { with: { deployments: true } } },
})
.then((res) => res?.system);

if (system == null) {
log.error("System not found", { environmentId: environment.id });
return;
}

const { workspaceId, deployments } = system;

const newlyMatchedResources = await getNewlyMatchedResources(
workspaceId,
oldSelector,
environment.resourceSelector,
);

if (newlyMatchedResources.length === 0) return;

const releaseTargets = deployments.flatMap((deployment) =>
newlyMatchedResources.map((resource) => ({
resourceId: resource.id,
deploymentId: deployment.id,
environmentId: environment.id,
})),
);

await db.insert(schema.releaseTarget).values(releaseTargets);
},
);
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify release target uniqueness.
If the system receives rapid consecutive updates with overlapping resource matches, there’s a risk of duplicate release targets unless the database schema enforces a uniqueness constraint or your logic performs deduplication. Please confirm there’s either a unique index across (resourceId, deploymentId, environmentId) or an upsert approach.

You can verify any existing uniqueness constraints with a script like:

If no constraint is found, consider updating the schema or implementing deduplication logic.


🏁 Script executed:

#!/bin/bash
# Description: Verify if releaseTarget has a unique constraint across (resourceId, deploymentId, environmentId).

ast-grep --pattern $'CREATE TABLE $_ ( $$$ )'
rg 'releaseTarget'

Length of output: 5395


Ensure Release Target Uniqueness in Database or via Deduplication

The current implementation in apps/event-worker/src/workers/env-selector-update.ts flattens and inserts release targets without any deduplication. Our script output did not reveal any explicit unique constraint or index enforcing uniqueness across (resourceId, deploymentId, environmentId) in the releaseTarget table (as defined in files like packages/db/src/schema/release.ts). This leaves the system vulnerable to duplicate entries if rapid, overlapping updates occur.

Please confirm one of the following:

  • There is a unique constraint (or unique index) on (resourceId, deploymentId, environmentId) at the database schema level.
  • Deduplication or an upsert mechanism is implemented during the insert operation.

Action Items:

  • Verify and, if necessary, update the schema in packages/db/src/schema/release.ts to enforce uniqueness.
  • Alternatively, implement deduplication/upsert logic in apps/event-worker/src/workers/env-selector-update.ts to avoid duplicate records.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/event-worker/src/workers/env-selector-update.ts (1)

167-219: Consider wrapping database operations in a single transaction.

All database calls (inserts, deletes, event dispatch) run concurrently in separate promises. In the event of an error or concurrent updates, partial changes could be committed. Wrapping them in a transaction (or applying consistent concurrency safeguards) would maintain data integrity.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dbc7abc and ffd0a6e.

📒 Files selected for processing (1)
  • apps/event-worker/src/workers/env-selector-update.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{ts,tsx}`: **Note on Error Handling:** Avoid strict enforcement of try/catch blocks. Code may use early returns, Promise chains (.then().catch()), or other patterns for error...

**/*.{ts,tsx}: Note on Error Handling:
Avoid strict enforcement of try/catch blocks. Code may use early returns, Promise chains (.then().catch()), or other patterns for error handling. These are acceptable as long as they maintain clarity and predictability.

  • apps/event-worker/src/workers/env-selector-update.ts
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: Lint
🔇 Additional comments (1)
apps/event-worker/src/workers/env-selector-update.ts (1)

62-77: Revisit Release Target deduplication or uniqueness constraints.

This code path inserts multiple release targets without enforcing uniqueness across (resourceId, deploymentId, environmentId). If the system receives overlapping updates, duplicates may be inserted unless there’s a uniqueness constraint or deduplication logic.

@adityachoudhari26 adityachoudhari26 merged commit 8c0580a into main Apr 4, 2025
1 of 9 checks passed
@adityachoudhari26 adityachoudhari26 deleted the env-selector-update-worker branch April 4, 2025 19:52
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