Skip to content

Fix issue that happens when trying to fetch replicas with CPLN CLI #254

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 2 commits into from
Mar 14, 2025

Conversation

rafaelgomesxyz
Copy link
Collaborator

@rafaelgomesxyz rafaelgomesxyz commented Mar 14, 2025

The CPLN CLI is currently raising an error when no replicas are found.

That causes the commands ps, ps:start, ps:stop, ps:wait, and run to fail when trying to fetch replicas.

This PR fixes that.

Summary by CodeRabbit

  • Documentation
    • Updated the changelog to document a fix addressing unstable behavior in replica-related commands.
  • Bug Fixes
    • Improved error handling to prevent failures during replica data retrieval.
    • Enhanced command stability by suppressing extraneous error messages for a smoother user experience.

@rafaelgomesxyz rafaelgomesxyz added the bug Something isn't working label Mar 14, 2025
@rafaelgomesxyz rafaelgomesxyz self-assigned this Mar 14, 2025
Copy link

coderabbitai bot commented Mar 14, 2025

Walkthrough

This pull request introduces error-handling improvements and documentation updates. A new "Fixed" section was added to the "Unreleased" part of the CHANGELOG.md to document a CLI command fix. Several commands in the lib/command/ files now use the safe navigation operator combined with dig to safely extract nested data from result hashes. Additionally, the lib/core/controlplane.rb file has been updated to redirect standard error output when executing a CLI command and to use safe navigation when determining the number of deployed replicas.

Changes

File(s) Summary
CHANGELOG.md Added a new "Fixed" section under "Unreleased" documenting a fix for CLI command failures with a reference to PR 254 by Rafael Gomes.
lib/command/ps.rb, lib/command/ps_stop.rb, lib/command/run.rb Modified code to use safe navigation (&.) with dig to safely extract the "items" key from result hashes, preventing potential runtime errors if nil.
lib/core/controlplane.rb Updated the CLI command in fetch_workload_replicas to redirect stderr to /dev/null and modified workload_deployments_ready? to safely extract and count "items".

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Controlplane
    participant CLI

    User->>Controlplane: invoke workload_deployments_ready?(workload, location, expected_status)
    Controlplane->>Controlplane: Call fetch_workload_replicas(workload, location)
    Controlplane->>CLI: Execute "cpln workload replica get ..." (stderr redirected)
    CLI-->>Controlplane: Return command result (result hash or nil)
    Controlplane->>Controlplane: Safely extract "items" using &.dig and determine count (fallback to 0)
    Controlplane-->>User: Return deployed_replicas (or 0)
Loading

Suggested labels

approved for merge

Suggested reviewers

  • justin808
  • dzirtusss

Poem

I'm a rabbit, hopping with glee,
In lines of code, so safe and free.
I dodge nils with a clever dig,
Swift fixes, making errors big shrink!
With each safe hop, the bugs flee – oh me, oh my!
Carrots and commits, a joyful, bug-free sky!

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 016357d and cdcdb6e.

📒 Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • lib/command/ps.rb (1 hunks)
  • lib/command/ps_stop.rb (1 hunks)
  • lib/command/run.rb (1 hunks)
  • lib/core/controlplane.rb (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: RSpec (Fast) / rspec
🔇 Additional comments (6)
CHANGELOG.md (1)

17-19: Well-documented change in the Changelog.

The changelog entry clearly explains the issue being fixed and provides proper attribution to the PR and author. This will help users understand what was fixed when they update to the next version.

lib/command/ps.rb (1)

37-37: Good error handling improvement.

The change to use safe navigation (&.dig) instead of direct hash access improves robustness by preventing a potential NoMethodError when either result is nil or doesn't have an "items" key.

lib/command/run.rb (1)

277-277: Enhanced error handling for replica retrieval.

Good use of safe navigation operators to handle cases where either result is nil or doesn't contain an "items" key. This defensive programming approach will prevent runtime errors when no replicas are found.

lib/command/ps_stop.rb (1)

78-79: Improved error handling for checking replica existence.

The refactoring breaks down the operation into two steps:

  1. Safely extract the "items" array using &.dig
  2. Check if the array exists before attempting to use include?

This prevents potential NoMethodError when either result is nil or doesn't have an "items" key, making the code more robust.

lib/core/controlplane.rb (2)

195-195: Good approach to handle errors gracefully

This change redirects stderr to /dev/null, which prevents error messages from being displayed when no replicas are found. This is a valid approach to handle the case of no replicas without causing command failures.


216-217: Great defensive programming approach

The use of safe navigation operators (&.dig) combined with a fallback value (|| 0) is an excellent way to handle the case when no replicas are found. This prevents a potential NoMethodError when the response doesn't contain an "items" key or is nil.

Let's verify this handles the actual issue:

#!/bin/bash
# Check if we can find any calls to fetch_workload_replicas that were relying on the "items" key
rg "fetch_workload_replicas.*\[\"items\"\]" --type ruby
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@justin808 justin808 merged commit 3ba0049 into main Mar 14, 2025
5 checks passed
@justin808 justin808 deleted the replica-fix branch March 14, 2025 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants