Skip to content

feat: Add variable change workers#453

Merged
adityachoudhari26 merged 3 commits intomainfrom
add-var-change-workers
Apr 5, 2025
Merged

feat: Add variable change workers#453
adityachoudhari26 merged 3 commits intomainfrom
add-var-change-workers

Conversation

@adityachoudhari26
Copy link
Copy Markdown
Member

@adityachoudhari26 adityachoudhari26 commented Apr 5, 2025

Summary by CodeRabbit

  • New Features

    • Introduced new workers for handling updates to deployment and resource variables, enhancing background processing capabilities.
    • Added a queuing mechanism for processing updates to resource variables, ensuring reliable handling of created, updated, or deleted variables.
  • Refactor

    • Streamlined API workflows for managing variable updates, reducing complexity while improving overall update responsiveness.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 5, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This update introduces two new worker functions in the event worker system: one for updating deployment variables and another for updating resource variables. Changes include registering these new workers in the main workers object, updating API routers to enqueue variable change events, and extending event channel definitions and type mappings. The modifications streamline the logic for handling variable updates by simplifying database queries and error handling while integrating a queue-based approach for processing updates.

Changes

File(s) Change Summary
apps/event-worker/src/workers/index.ts, apps/event-worker/src/workers/update-deployment-variable.ts, apps/event-worker/src/workers/update-resource-variable.ts Added two new worker functions (updateDeploymentVariableWorker and updateResourceVariableWorker) and updated the workers object to handle deployment and resource variable updates.
packages/api/src/router/deployment-variable.ts, packages/api/src/router/resource-variables.ts Modified API routers by removing obsolete imports, simplifying mutation logic, and adding queue enqueuing for deployment and resource variable changes.
packages/events/src/types.ts Extended the event channels by adding UpdateDeploymentVariable and UpdateResourceVariable enum values, and updated type mappings to include these new channels.

Sequence Diagram(s)

sequenceDiagram
    participant API as API Router
    participant Queue as updateDeploymentVariableQueue
    participant Worker as updateDeploymentVariableWorker
    participant DB as Database
    participant EvalQ as evaluateReleaseTargetQueue

    API->>Queue: Enqueue deployment variable update job
    Queue->>Worker: Dispatch job
    Worker->>DB: Query variable by ID
    DB-->>Worker: Return deployment variable or error
    alt Variable Found
        Worker->>DB: Query release targets for deployment
        DB-->>Worker: Return release targets
        Worker->>EvalQ: Add release targets for evaluation
    else Variable Not Found
        Worker-->>Worker: Throw error
    end
Loading
sequenceDiagram
    participant API as API Router
    participant Queue as updateResourceVariableQueue
    participant Worker as updateResourceVariableWorker
    participant DB as Database
    participant EvalQ as evaluateReleaseTargetQueue

    API->>Queue: Enqueue resource variable update job
    Queue->>Worker: Dispatch job
    Worker->>DB: Query deployment IDs via joins using resourceId & key
    DB-->>Worker: Return deployment IDs
    Worker->>DB: Query release targets using deployment IDs
    DB-->>Worker: Return matching release targets
    Worker->>EvalQ: Add release targets for processing
Loading

Suggested Reviewers

  • jsbroks

Poem

I'm a happy bunny in the code garden,
Hopping through workers with a cheerful air,
New channels and queues make my heart glisten,
Deployment and resource updates everywhere.
With each line of change, my floppy ears dance in delight! 🐇✨


📜 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 d58b01a and 086d5e4.

📒 Files selected for processing (1)
  • apps/event-worker/src/workers/update-resource-variable.ts (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
Copy Markdown
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: 2

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

41-46: Optional safeguard against empty releases.

If releaseTargets is empty, calling .addBulk([]) will effectively enqueue no jobs. While this is not harmful, you may consider short-circuiting to skip unnecessary queue interactions when the array is empty.

 if (releaseTargets.length > 0) {
   await getQueue(Channel.EvaluateReleaseTarget).addBulk(
     releaseTargets.map((rt) => ({
       name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`,
       data: rt,
     })),
   );
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8900eb7 and d58b01a.

📒 Files selected for processing (6)
  • apps/event-worker/src/workers/index.ts (2 hunks)
  • apps/event-worker/src/workers/update-deployment-variable.ts (1 hunks)
  • apps/event-worker/src/workers/update-resource-variable.ts (1 hunks)
  • packages/api/src/router/deployment-variable.ts (9 hunks)
  • packages/api/src/router/resource-variables.ts (4 hunks)
  • packages/events/src/types.ts (2 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/update-deployment-variable.ts
  • apps/event-worker/src/workers/index.ts
  • packages/events/src/types.ts
  • apps/event-worker/src/workers/update-resource-variable.ts
  • packages/api/src/router/resource-variables.ts
  • packages/api/src/router/deployment-variable.ts
🧬 Code Definitions (1)
apps/event-worker/src/workers/index.ts (2)
apps/event-worker/src/workers/update-deployment-variable.ts (1)
  • updateDeploymentVariableWorker (14-35)
apps/event-worker/src/workers/update-resource-variable.ts (1)
  • updateResourceVariableWorker (6-48)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Typecheck
  • GitHub Check: build (linux/amd64)
  • GitHub Check: Lint
🔇 Additional comments (10)
apps/event-worker/src/workers/index.ts (1)

11-12: Worker registration looks good!

The new workers for handling deployment and resource variable updates are properly imported and registered in the workers object.

Also applies to: 23-24

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

20-22: Channel enum values added appropriately.

The new enum values for variable updates are well-placed and follow the existing naming conventions.


35-37: ChannelMap type extensions look good.

The type mappings correctly associate the new channels with their respective schema types.

apps/event-worker/src/workers/update-deployment-variable.ts (2)

6-13: Good documentation!

The comments clearly explain the worker's purpose and assumptions, which helps with maintainability.


14-35: Worker implementation looks correct.

The worker follows the established pattern using createWorker, has appropriate error handling for missing variables, and properly queues release targets for evaluation.

packages/api/src/router/resource-variables.ts (4)

5-5: Queue initialization looks good.

Proper imports and initialization of the resource variable update queue.

Also applies to: 11-11


28-35: Create mutation update looks good.

The implementation correctly awaits the database operation, captures the result, adds it to the queue, and returns the variable.


67-71: Update mutation change looks good.

The chained promise approach correctly captures the updated variable, queues it for processing, and returns it.


93-99: Delete mutation update looks good.

The implementation now includes returning the deleted record for queuing and properly notifies the worker system about the deletion.

packages/api/src/router/deployment-variable.ts (1)

27-34: Queue initialization looks good.

Using Channel.UpdateDeploymentVariable via getQueue() is aligned with the new worker-based architecture. The approach is clean and maintains consistency with the rest of the codebase.

Comment on lines +64 to +69
const variable = await tx
.select()
.from(deploymentVariable)
.where(eq(deploymentVariable.id, input.variableId))
.then(takeFirst);
await updateDeploymentVariableQueue.add(variable.id, variable);
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.

💡 Verification agent

🧩 Analysis chain

Streamline repeated enqueue logic for better maintainability.

Across create, update, and delete operations, the code repeatedly fetches a variable and enqueues it with identical statements (updateDeploymentVariableQueue.add(variable.id, variable)). Consider creating a shared helper function, such as enqueueVariableUpdate(variable), to reduce duplication. This also ensures consistent error handling or logging for queue interactions.

Additionally, note that after deleting a variable (lines 358-368), the worker might receive a “null” or “missing” entry. Confirm whether the worker can gracefully handle or ignore events for non-existent data.

- await updateDeploymentVariableQueue.add(variable.id, variable);

// Proposed helper usage
+ async function enqueueVariableUpdate(variable: DeploymentVariable) {
+   await updateDeploymentVariableQueue.add(variable.id, variable);
+ }

// Then in create, update, delete:
+ await enqueueVariableUpdate(variable);

Also applies to: 96-96, 136-136, 160-172, 336-346, 358-368


Please run the above script to confirm that the enqueue logic is indeed duplicated across various operations (e.g., in create, update, and delete flows). Once verified, consider refactoring by extracting a helper function (like enqueueVariableUpdate(variable)). Also, double-check that the worker processing these queue events can gracefully handle cases where a variable is null or missing, especially in the deletion scenario.

Action: Refactor Repeated Enqueue Logic for Deployment Variable Updates

It appears that the enqueue operation (updateDeploymentVariableQueue.add(variable.id, variable)) is repeated identically across multiple code sections. To improve maintainability and ensure consistent error handling/logging:

  • Extract a helper function: Create a dedicated function (e.g., enqueueVariableUpdate(variable)) to encapsulate the enqueue logic.
  • Verify worker safety: Confirm that the worker processing the update queue can gracefully ignore or handle events when the corresponding variable is null/missing (notably after deletions).

Proposed refactor diff:

- await updateDeploymentVariableQueue.add(variable.id, variable);

+ async function enqueueVariableUpdate(variable: DeploymentVariable) {
+   await updateDeploymentVariableQueue.add(variable.id, variable);
+ }
+
+ // In create, update, and delete operations, replace with:
+ await enqueueVariableUpdate(variable);

This change is relevant in the following areas:

  • Lines 64-69 (current snippet)
  • Lines 96-96, 136-136, 160-172, 336-346, and 358-368

Please verify the duplicated enqueue point using the above script and review the worker’s logic regarding null or missing variables.


🏁 Script executed:

#!/bin/bash
# Search for all occurrences of updateDeploymentVariableQueue.add in the repository to verify repeated enqueue usage.
rg "updateDeploymentVariableQueue.add\(" -n .

Length of output: 798


Refactor Enqueue Logic in Deployment Variable Router

The enqueue operation (updateDeploymentVariableQueue.add(variable.id, variable)) has been confirmed to be duplicated in multiple locations within the file (found at lines 69, 136, 172, 324, 344, and 365 in packages/api/src/router/deployment-variable.ts). To improve maintainability and ensure consistent error handling/logging, please extract a helper function (e.g., enqueueVariableUpdate(variable)) to encapsulate this repeated logic.

Additionally, ensure that the worker processing the update queue can gracefully handle cases where the variable is null or missing (for instance, after a deletion). This may involve adding checks before processing the queued event or updating the worker’s error handling to ignore or safely log such events.

Proposed refactor diff:

- await updateDeploymentVariableQueue.add(variable.id, variable);

+ async function enqueueVariableUpdate(variable: DeploymentVariable) {
+   await updateDeploymentVariableQueue.add(variable.id, variable);
+ }
+
+ // Then in create, update, and delete operations:
+ await enqueueVariableUpdate(variable);

Action Items:

  • Refactor the duplicate enqueue calls at lines 69, 136, 172, 324, 344, and 365.
  • Review the worker handling the update queue and ensure it safely manages null/missing variable entries.

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