Skip to content

Conversation

@adityachoudhari26
Copy link
Contributor

@adityachoudhari26 adityachoudhari26 commented Apr 7, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a dedicated worker that enhances resource update processing, including dispatching exit hooks when resources are removed.
  • Refactor
    • Streamlined upsert and deletion operations for resources, ensuring more efficient handling and consistent API responses.
  • Chores
    • Cleaned up legacy code related to resource dispatching and metadata/variable management, improving system maintainability and performance.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 7, 2025

Walkthrough

The changes introduce a dedicated worker for processing upserted resources by adding the processUpsertedResourceWorker function and a new channel value in the events system. Updates include streamlined resource upsert logic, exit hook dispatching, and modifications to deletion methods in API routes and job dispatch modules. Enhancements in resource metadata typing and restructured module exports are also incorporated, while several redundant functions and files in the job-dispatch package have been removed.

Changes

File(s) Change Summary
apps/event-worker/src/workers/index.ts Added import and worker entry for processUpsertedResourceWorker tied to the new channel.
packages/events/src/types.ts Introduced new enum value ProcessUpsertedResource and updated ChannelMap accordingly.
packages/job-dispatch/src/resource/upsert.ts Refactored upsertResources to streamline resource upsert and deletion logic.
apps/event-worker/src/workers/process-upserted-resource/*
(dispatch-exit-hooks.ts, process-upserted-resource.ts, upsert-release-targets.ts)
Added new files for dispatching exit hooks, processing upserted resources, and upserting release targets with concurrent operations.
apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts Modified DELETE handler to replace bulk deletion (deleteResources) with single resource deletion (deleteResource).
packages/api/src/router/environment.ts
packages/api/src/router/resources.ts
Removed resource selector handling in environment updates and renamed deletion functions in resource mutations.
packages/db/src/schema/resource.ts Added new type alias ResourceMetadata for improved type safety.
packages/job-dispatch/src/index.ts Removed general resource exports and added specific exports for delete-resource.js and upsert.js.
packages/job-dispatch/src/resource/db-upsert-resource.ts Introduced new file for upserting resources along with metadata and variables management.
packages/job-dispatch/src/resource/delete-resource.ts Introduced new deletion function to remove a resource, handle associated events, and cancel related jobs.
Multiple files in packages/job-dispatch/src/resource/
(delete.ts, dispatch-resource.ts, insert-resource-metadata.ts, insert-resource-variables.ts, utils.ts, and changes in find-existing-resources.ts & index.ts)
Removed redundant functions and files to focus on streamlined resource upsert and deletion functionality.

Sequence Diagram(s)

sequenceDiagram
    participant R as Resource
    participant W as processUpsertedResourceWorker
    participant DB as Database
    participant U as upsertReleaseTargets
    participant E as dispatchExitHooks
    participant Q as EvaluationQueue

    R->>W: Trigger upserted resource event
    W->>DB: Query current release targets by resource.id
    DB-->>W: Return current release targets
    W->>U: Call upsertReleaseTargets(resource)
    U-->>W: Return new release targets
    W->>DB: Delete obsolete release targets
    W->>E: Dispatch exit hooks concurrently
    W->>Q: Add new release targets to evaluation queue
    W-->>R: Complete processing
Loading

Possibly related PRs

Suggested reviewers

  • jsbroks

Poem

I'm a rabbit in the code, on a joyful quest,
Hopping through updates, always doing my best.
New workers and channels, like carrots they gleam,
Cleaning up functions, streamlining the team.
With each little commit, my heart skips a beat,
Celebrating these changes with a hip, bouncy leap!
CodeRabbit Inc. leads the way; oh, what a treat!

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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: 0

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

12-35: Consider validating job.data.resource before usage.

The code directly destructures resource from job.data. If the job payload is malformed or missing the resource property, an unhandled error may occur. Adding optional checks or assertions can provide clearer feedback.

 try {
   const { data } = job;
+  if (!data || !data.resource) {
+    log.error("Invalid job data received, missing `resource`");
+    return;
+  }
   const { resource } = data;
apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts (2)

11-31: Upsert logic is straightforward and efficient.

Using onConflictDoUpdate with buildConflictUpdateColumns is an effective approach. Consider adding any relevant columns (e.g., 'lockedAt') if needed for conflict resolution, but that may be optional depending on your requirements.


42-49: Review handling of sensitive variables.

Storing sensitive data in plain text carries risk if the database is compromised. Evaluate whether additional encryption or a secret vault approach would be beneficial when v.sensitive is true.

apps/event-worker/src/workers/upsert-resource/existing-resource.ts (2)

1-16: Imports and type definitions are consistent.

Type ResourceToInsert is repeated here for convenience, matching usage in resource-db-upsert.ts. Keeping them in a shared location could reduce duplication, but it’s acceptable if intentionally segregated.


84-132: handleExistingResource flow is coherent but note partial failures.

The concurrent calls in Promise.allSettled ensure that errors in one part do not halt the entire process, but also imply that unhandled failures might leave partial state changes. This is typically acceptable in event-driven systems but confirm that partial updates are safe.

apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts (2)

5-40: getReleaseTargetInsertsForSystem usage is correct but be mindful of repeated queries.

The code individually queries db.query.resource.findFirst for each environment-deployment pair. For large sets, consider bulk-finding or caching. Otherwise, logic is sound.


41-62: Upsert of release targets is clean.

Using .onConflictDoNothing() helps avoid duplication, and returning the inserted rows is helpful for subsequent usage. Consider logging the generated release targets for debugging or monitoring.

 .returning();
+// Optional: add a debug log to confirm how many targets were inserted
+console.debug("Inserted release targets", releaseTargetInserts.length);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 327e448 and f2022e0.

📒 Files selected for processing (8)
  • apps/event-worker/src/workers/index.ts (2 hunks)
  • apps/event-worker/src/workers/upsert-resource/existing-resource.ts (1 hunks)
  • apps/event-worker/src/workers/upsert-resource/new-resource.ts (1 hunks)
  • apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts (1 hunks)
  • apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts (1 hunks)
  • apps/event-worker/src/workers/upsert-resource/worker.ts (1 hunks)
  • packages/events/src/types.ts (2 hunks)
  • packages/job-dispatch/src/resource/upsert.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/index.ts
  • packages/job-dispatch/src/resource/upsert.ts
  • apps/event-worker/src/workers/upsert-resource/worker.ts
  • apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts
  • apps/event-worker/src/workers/upsert-resource/new-resource.ts
  • packages/events/src/types.ts
  • apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts
  • apps/event-worker/src/workers/upsert-resource/existing-resource.ts
🧬 Code Definitions (7)
apps/event-worker/src/workers/index.ts (1)
apps/event-worker/src/workers/upsert-resource/worker.ts (1)
  • upsertResourceWorker (12-35)
packages/job-dispatch/src/resource/upsert.ts (1)
packages/db/src/schema/resource.ts (1)
  • InsertResource (115-115)
apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts (4)
packages/db/src/common.ts (1)
  • Tx (22-22)
packages/rule-engine/src/types.ts (1)
  • ReleaseTargetIdentifier (74-78)
packages/db/src/schema/resource.ts (1)
  • resource (59-87)
packages/db/src/schema/workspace.ts (1)
  • workspace (18-27)
apps/event-worker/src/workers/upsert-resource/new-resource.ts (5)
packages/db/src/schema/resource.ts (2)
  • InsertResource (115-115)
  • resource (59-87)
packages/db/src/common.ts (1)
  • Tx (22-22)
apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts (1)
  • dbUpsertResource (11-51)
apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts (1)
  • upsertReleaseTargets (41-62)
packages/events/src/index.ts (1)
  • getQueue (26-32)
packages/events/src/types.ts (1)
packages/db/src/schema/resource.ts (1)
  • InsertResource (115-115)
apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts (2)
packages/db/src/schema/resource.ts (2)
  • InsertResource (115-115)
  • resource (59-87)
packages/db/src/common.ts (3)
  • Tx (22-22)
  • buildConflictUpdateColumns (30-46)
  • takeFirst (9-13)
apps/event-worker/src/workers/upsert-resource/existing-resource.ts (4)
packages/db/src/common.ts (1)
  • Tx (22-22)
packages/rule-engine/src/types.ts (1)
  • ReleaseTargetIdentifier (74-78)
apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts (1)
  • dbUpsertResource (11-51)
apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts (1)
  • upsertReleaseTargets (41-62)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Typecheck
  • GitHub Check: build (linux/amd64)
  • GitHub Check: Lint
  • GitHub Check: build (linux/amd64)
🔇 Additional comments (13)
apps/event-worker/src/workers/index.ts (2)

14-14: Clean import for the new worker

The import statement for the new upsertResourceWorker follows the existing import pattern, maintaining consistency with the codebase.


30-30: Proper worker registration for the new channel

The upsertResourceWorker is correctly registered with Channel.UpsertResource, following the established pattern for worker registration in this file.

packages/job-dispatch/src/resource/upsert.ts (1)

19-22: Appropriate encapsulation change

Removing the export keyword from ResourceToInsert type appropriately restricts it to the module scope. This is good practice for types that should only be used within the module.

apps/event-worker/src/workers/upsert-resource/new-resource.ts (2)

9-12: Well-defined resource type

The ResourceToInsert type appropriately extends the base InsertResource schema with optional fields for metadata and variables, maintaining type safety throughout the resource creation process.


14-23: Well-structured handler function with clear workflow

The handleNewResource function follows a clean workflow:

  1. Inserts/updates the resource
  2. Upserts associated release targets
  3. Adds release targets to the evaluation queue with unique names

The approach handles the complete lifecycle of resource creation with proper asynchronous flow.

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

25-25: Consistent channel enumeration

The new UpsertResource enum value follows the naming convention pattern of other channels in the enum, using kebab-case for the string value.


47-52: Complete channel map type definition

The type definition for Channel.UpsertResource in the ChannelMap is complete, including all necessary fields for resource processing with appropriate types. This ensures type safety throughout the event handling pipeline.

apps/event-worker/src/workers/upsert-resource/worker.ts (1)

1-11: Imports and logger initialization look good.

No obvious syntax or security issues. The logger child configuration is clear and consistent with typical usage patterns.

apps/event-worker/src/workers/upsert-resource/resource-db-upsert.ts (2)

1-9: Resource type structure is clear.

Defining ResourceToInsert to extend the base InsertResource while adding optional metadata and variables is a clean approach for capturing additional resource-related data.


32-41: Consider handling conflicts for metadata entries.

While inserting metadata directly works for new entries, existing keys could cause unique constraints or duplication issues if the schema enforces them. If such conflicts are possible, you may consider an upsert or cleanup step for metadata.

apps/event-worker/src/workers/upsert-resource/existing-resource.ts (2)

18-53: Function getSystemsForUnmatchedEnvs is well-structured.

Fetching the missing environment IDs, then retrieving systems tied to those environments, is logical. Consider verifying that large environment lists won't cause performance bottlenecks.


54-83: Hooks dispatch logic appears robust.

The function checks if the resource still matches the relevant environment filters before dispatching exit hooks, avoiding unnecessary calls. This approach helps keep the system in sync.

apps/event-worker/src/workers/upsert-resource/upsert-release-targets.ts (1)

1-4: Initial imports are standard.

No concerns observed.

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 (6)
packages/job-dispatch/src/resource/upsert.ts (1)

46-54: Queueing upserted resources for post-processing is sound.

Adding resources to the queue in bulk for asynchronous processing is a clean approach. Tagging jobs by resource ID looks fine; ensure no collisions occur if IDs can be repeated across tenants.

apps/event-worker/src/workers/process-upserted-resource/process-upserted-resource.ts (1)

9-48: Verify object comparison in releaseTargetsToDelete.
Line 18 uses newReleaseTargets.includes(rt), which performs reference equality rather than matching by ID fields. If rt objects differ by reference even though they share the same ID, they may not match. Consider comparing by rt.id instead.

Check for partial failures with Promise.allSettled.
Promise.allSettled ensures the function won’t throw on individual rejections, but rejections might be silently ignored. Consider logging or handling errors for better traceability.

packages/job-dispatch/src/resource/db-upsert-resource.ts (1)

76-125: Confirm concurrency requirements for upsert.
dbUpsertResource concurrently upserts metadata and variables, which is generally efficient. Just confirm no race conditions if multiple upserts for the same resource happen in parallel.

apps/event-worker/src/workers/process-upserted-resource/upsert-release-targets.ts (1)

8-39: Consider consolidating the nested loop to reduce query overhead.
The code currently makes a separate database query for each environment–deployment pair, potentially leading to high overhead when handling large numbers of environments or deployments. A possible improvement is to batch or consolidate these lookups into fewer queries if feasible, improving performance.

apps/event-worker/src/workers/process-upserted-resource/dispatch-exit-hooks.ts (2)

45-73: Handle empty environment arrays gracefully.
When mapping environments.map(...) inside or(...), an empty array might yield an empty condition list. Consider adding a quick guard to skip the query if there are no environments. This ensures we avoid potential edge cases.


75-90: Consider error handling or logging for rejected event dispatches.
Promise.allSettled ensures all dispatch calls proceed, but any rejections are currently swallowed. Adding logs or handling for rejections can help diagnose unexpected behavior in production.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f2022e0 and 5ebc5bb.

📒 Files selected for processing (20)
  • apps/event-worker/src/workers/index.ts (2 hunks)
  • apps/event-worker/src/workers/process-upserted-resource/dispatch-exit-hooks.ts (1 hunks)
  • apps/event-worker/src/workers/process-upserted-resource/process-upserted-resource.ts (1 hunks)
  • apps/event-worker/src/workers/process-upserted-resource/upsert-release-targets.ts (1 hunks)
  • apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts (2 hunks)
  • packages/api/src/router/environment.ts (1 hunks)
  • packages/api/src/router/resources.ts (2 hunks)
  • packages/db/src/schema/resource.ts (1 hunks)
  • packages/events/src/types.ts (2 hunks)
  • packages/job-dispatch/src/index.ts (1 hunks)
  • packages/job-dispatch/src/resource/db-upsert-resource.ts (1 hunks)
  • packages/job-dispatch/src/resource/delete-resource.ts (1 hunks)
  • packages/job-dispatch/src/resource/delete.ts (0 hunks)
  • packages/job-dispatch/src/resource/dispatch-resource.ts (0 hunks)
  • packages/job-dispatch/src/resource/find-existing-resources.ts (2 hunks)
  • packages/job-dispatch/src/resource/index.ts (0 hunks)
  • packages/job-dispatch/src/resource/insert-resource-metadata.ts (0 hunks)
  • packages/job-dispatch/src/resource/insert-resource-variables.ts (0 hunks)
  • packages/job-dispatch/src/resource/upsert.ts (2 hunks)
  • packages/job-dispatch/src/resource/utils.ts (0 hunks)
💤 Files with no reviewable changes (6)
  • packages/job-dispatch/src/resource/insert-resource-metadata.ts
  • packages/job-dispatch/src/resource/index.ts
  • packages/job-dispatch/src/resource/utils.ts
  • packages/job-dispatch/src/resource/dispatch-resource.ts
  • packages/job-dispatch/src/resource/delete.ts
  • packages/job-dispatch/src/resource/insert-resource-variables.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/events/src/types.ts
  • apps/event-worker/src/workers/index.ts
🧰 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/db/src/schema/resource.ts
  • packages/job-dispatch/src/index.ts
  • packages/job-dispatch/src/resource/find-existing-resources.ts
  • packages/api/src/router/resources.ts
  • packages/api/src/router/environment.ts
  • apps/event-worker/src/workers/process-upserted-resource/process-upserted-resource.ts
  • packages/job-dispatch/src/resource/upsert.ts
  • packages/job-dispatch/src/resource/delete-resource.ts
  • apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts
  • apps/event-worker/src/workers/process-upserted-resource/upsert-release-targets.ts
  • packages/job-dispatch/src/resource/db-upsert-resource.ts
  • apps/event-worker/src/workers/process-upserted-resource/dispatch-exit-hooks.ts
🧬 Code Definitions (5)
apps/event-worker/src/workers/process-upserted-resource/process-upserted-resource.ts (3)
packages/events/src/index.ts (2)
  • createWorker (9-23)
  • getQueue (26-32)
apps/event-worker/src/workers/process-upserted-resource/upsert-release-targets.ts (1)
  • upsertReleaseTargets (41-62)
apps/event-worker/src/workers/process-upserted-resource/dispatch-exit-hooks.ts (1)
  • dispatchExitHooks (75-90)
packages/job-dispatch/src/resource/upsert.ts (3)
packages/job-dispatch/src/resource/find-existing-resources.ts (1)
  • findExistingResources (52-70)
packages/job-dispatch/src/resource/db-upsert-resource.ts (1)
  • dbUpsertResource (76-125)
packages/job-dispatch/src/resource/delete-resource.ts (1)
  • deleteResource (52-72)
apps/event-worker/src/workers/process-upserted-resource/upsert-release-targets.ts (4)
packages/db/src/common.ts (1)
  • Tx (22-22)
packages/rule-engine/src/types.ts (1)
  • ReleaseTargetIdentifier (74-78)
packages/db/src/schema/resource.ts (1)
  • resource (59-87)
packages/db/src/schema/workspace.ts (1)
  • workspace (18-27)
packages/job-dispatch/src/resource/db-upsert-resource.ts (3)
packages/job-dispatch/src/resource/upsert.ts (1)
  • ResourceToInsert (13-16)
packages/db/src/schema/resource.ts (2)
  • InsertResource (115-115)
  • resource (59-87)
packages/db/src/common.ts (3)
  • Tx (22-22)
  • buildConflictUpdateColumns (30-46)
  • takeFirst (9-13)
apps/event-worker/src/workers/process-upserted-resource/dispatch-exit-hooks.ts (3)
packages/db/src/common.ts (1)
  • Tx (22-22)
packages/rule-engine/src/types.ts (1)
  • ReleaseTargetIdentifier (74-78)
packages/db/src/schema/resource.ts (2)
  • resource (59-87)
  • Resource (105-105)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: Typecheck
  • GitHub Check: Lint
🔇 Additional comments (29)
packages/db/src/schema/resource.ts (1)

174-174: Well-structured type export for resource metadata.

The new ResourceMetadata type alias provides better type safety when working with resource metadata entities, which aligns well with TypeScript best practices.

packages/job-dispatch/src/index.ts (1)

26-27: Good module restructuring for better code organization.

Replacing the general resource module export with specific exports for delete and upsert operations provides more granular control and improves code organization. This change supports the new upsert resource worker functionality indicated in the PR objectives.

apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts (2)

7-7: Import updated to match the new resource management approach.

The import statement has been correctly updated to use the more specific deleteResource function, aligning with the changes in the job-dispatch package.


123-123: Function call refactored to use resource ID directly.

The deletion logic has been simplified to pass the resource ID directly to deleteResource instead of passing an array of resources. This matches the new function signature and provides a more direct approach.

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

5-5: Import statement streamlined for better readability.

The updated import statement includes upsertEnv which is relevant to the environment management functionality.


291-295: Event dispatch for environment updates preserved.

The code correctly maintains the environment update event dispatch while removing the detailed resource selector processing logic. This aligns with moving that responsibility to the new dedicated upsert resource worker.

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

25-25: Good use of the newly introduced deleteResource import.

This import seems correct and aligns with the refactored single-resource deletion API.


698-705: Consider partial failure handling for batched deletions.

If any resource deletion fails, Promise.all will reject immediately, potentially leaving other resources undeleted. This may be acceptable, but verify if partial-deletion scenarios should be handled differently or if an all-or-nothing approach is desired.

packages/job-dispatch/src/resource/upsert.ts (6)

4-9: Imports for queuing and resource helpers look good.

These imports correctly reflect the new architecture for upsert logic, event queuing, and resource existence checks.


32-41: Verify logic for identifying resources to delete.

Filtering out existing resources not included in resourcesToInsert triggers deletions. Confirm that excluding resources from this array is always the intended signal to remove them, especially if resources were renamed or repurposed.


42-45: Concurrent upsert is valid but double-check error handling.

Using Promise.all for upserts is concise; however, if one upsert fails, the entire batch rejects. Verify that this collective failure is acceptable and consistent with the desired behavior.


55-58: Parallel deletions are okay but confirm race conditions.

Deleting resources in parallel is efficient; ensure no follow-up writes rely on fully removed states. Also verify the desired outcome if one deletion fails mid-batch.


59-62: Synchronized promise resolution.

The combined Promise.all usage for adding upsert jobs and performing deletions is clear and consistent. Good concurrency handling.


64-64: Return consolidated result object.

Returning both upserted and deleted resources is intuitive and helpful for downstream logic. No issues.

packages/job-dispatch/src/resource/delete-resource.ts (4)

1-10: New imports and status definitions are correct.

The references to the DB schema, job status, and event handlers appear accurate for the new deletion workflow.


12-24: Resource relationship deletion logic looks consistent.

Removing entries from resourceRelationship by matching both sides (from/to) ensures no stale links remain. This approach handles scenarios where the resource is a dependency or a dependent.


25-50: Job cancellation approach is logical.

Fetching pending or in-progress jobs for the associated resource and marking them as cancelled prevents orphaned jobs. This aligns well with typical tidy-up patterns after resource removal.


52-72: End-to-end resource deletion with event handling is well-structured.

The combined steps:

  1. Retrieve resource,
  2. Emit deletion events,
  3. Clean up relationships/jobs,
  4. Delete resource.

All happen in one flow. This implementation is sensible and thorough.

packages/job-dispatch/src/resource/find-existing-resources.ts (2)

5-5: No concerns for this import.
Imports are concise and consistent with usage in this file.


52-70: Consider error handling for Promise failures.
This function aggregates database queries via Promise.all. If one query fails, the entire chain will reject. Verify that this behavior is desirable and ensure it won’t leave the system in a partial or inconsistent state. Consider adding logging or try/catch to handle failures more gracefully.

apps/event-worker/src/workers/process-upserted-resource/process-upserted-resource.ts (1)

1-7: Imports look good.
All imported modules appear relevant and correctly used in subsequent code.

packages/job-dispatch/src/resource/db-upsert-resource.ts (6)

1-2: No issues with the Tx import.
This matches the project’s transaction pattern.


3-11: Approved usage of conflict update helper.
buildConflictUpdateColumns usage is aligned with your approach for handling unique constraints.


12-16: VariableInsert definition is straightforward.
No correctness issues identified.


18-21: ResourceToInsert extension is valid.
Enables optional metadata and variables, which is consistent with the upsert logic.


23-49: Validate possible edge cases for empty metadata.
While the upsert logic looks sound, confirm that inserting/updating with an empty object or key collisions doesn’t lead to unexpected results.


51-74: Same caution for variable collisions.
Ensure that partially updated variables with the same key but different sensitivity or type are handled correctly.

apps/event-worker/src/workers/process-upserted-resource/upsert-release-targets.ts (1)

41-62: No issues identified; approach seems solid.
Fetching the workspace and inserting release targets via onConflictDoNothing() appears logically consistent. The usage of throw new Error("Workspace not found") provides clear failure feedback, and the final return matches the expected inserted records.

apps/event-worker/src/workers/process-upserted-resource/dispatch-exit-hooks.ts (1)

9-43: Logic for identifying unmatched environments is coherent.
Building sets of environment IDs and filtering them out is a clear approach. The subsequent queries to retrieve systems and their respective environments and deployments appear correct.

@adityachoudhari26 adityachoudhari26 deleted the upsert-resource-worker branch May 5, 2025 19:49
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