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 callback to check how many workers are available #34

Merged
merged 5 commits into from
Feb 23, 2024
Merged

Feat: add callback to check how many workers are available #34

merged 5 commits into from
Feb 23, 2024

Conversation

FluffyDiscord
Copy link

@FluffyDiscord FluffyDiscord commented Feb 21, 2024

Q A
Bugfix?
Breaks BC?
New feature? ✔️
Issues #...
Docs PR spiral/docs#...

We can add and remove workers, but we don't know how many of them there are in the first place 🙃

Going by the RPC call roadrunner-server/roadrunner#1728

Summary by CodeRabbit

  • New Features
    • Enhanced Worker Pool with methods to retrieve worker count and worker information.
    • Implemented classes to manage detailed worker process information and count functionality.
    • Added test methods to validate worker count and retrieval functionality.

Copy link

coderabbitai bot commented Feb 21, 2024

Warning

Rate Limit Exceeded

@msmakouz has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 23 minutes and 23 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 2cd654f and 6791dc9.

Walkthrough

This update introduces new functionalities to the WorkerPool class, enabling the counting of workers and retrieval of detailed worker information. Additionally, new classes Worker and Workers have been added to provide comprehensive worker process details and manage workers effectively. Tests have been included to ensure the reliability of these enhancements.

Changes

File Change Summary
src/.../WorkerPool.php Added methods to count and retrieve workers
src/Informer/Worker.php Introduces Worker class with process details
src/Informer/Workers.php Defines Workers class implementing \Countable
tests/Unit/WorkerPoolTest.php Includes tests for counting and retrieving workers

Poem

🐰 In a code land, far and wide,
A rabbit worked with utmost pride.
"Two methods new," it said with glee,
To count and see, as clear as can be.
🌟 Through tests, it hopped, no bug could hide,
In WorkerPool, now amplified.
"With every line, our code does thrive,
Together, in digital burrows, we dive." 🚀

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@FluffyDiscord
Copy link
Author

I do not know how to fix the PSALM array return error. Help would be appreciated.

Copy link

@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.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 605e258 and 3f8f866.
Files selected for processing (2)
  • src/WorkerPool.php (1 hunks)
  • tests/Unit/WorkerPoolTest.php (1 hunks)

Comment on lines 30 to 38
/**
* Get the worker count for a pool.
*
* @param non-empty-string $plugin
*/
public function countWorkers(string $plugin): int
{
return count($this->getWorkers($plugin));
}
Copy link

Choose a reason for hiding this comment

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

The implementation of countWorkers directly relies on the getWorkers method to determine the count. This approach is straightforward and leverages existing functionality for consistency. However, consider the performance implications if getWorkers performs heavy operations or fetches a large amount of data. In such cases, a more direct approach to counting, if available through the underlying RPC mechanism, might be more efficient.

Comment on lines 40 to 48
/**
* Get the info about running workers for a pool.
*
* @param non-empty-string $plugin
*/
public function getWorkers(string $plugin): array
{
return $this->rpc->call('informer.Workers', $plugin)['workers'];
}
Copy link

Choose a reason for hiding this comment

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

The getWorkers method fetches worker information using the informer.Workers RPC call and directly returns the workers array from the response. This method assumes that the response structure always contains a workers key. To improve robustness, consider adding error handling to manage cases where the response might not contain the expected structure or when the RPC call fails. This could include checking if the workers key exists and handling potential exceptions from the RPC call.

-        return $this->rpc->call('informer.Workers', $plugin)['workers'];
+        $response = $this->rpc->call('informer.Workers', $plugin);
+        if (!isset($response['workers'])) {
+            throw new \RuntimeException("Unexpected response structure from informer.Workers");
+        }
+        return $response['workers'];

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
/**
* Get the info about running workers for a pool.
*
* @param non-empty-string $plugin
*/
public function getWorkers(string $plugin): array
{
return $this->rpc->call('informer.Workers', $plugin)['workers'];
}
/**
* Get the info about running workers for a pool.
*
* @param non-empty-string $plugin
*/
public function getWorkers(string $plugin): array
{
$response = $this->rpc->call('informer.Workers', $plugin);
if (!isset($response['workers'])) {
throw new \RuntimeException("Unexpected response structure from informer.Workers");
}
return $response['workers'];
}

Comment on lines 38 to 42
public function testCountWorkers(): void
{
$this->rpc->expects($this->once())->method('call')->with('informer.Workers', 'test')->willReturn(['workers' => []]);

$this->workerPool->countWorkers('test');
Copy link

Choose a reason for hiding this comment

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

The testCountWorkers method correctly mocks the informer.Workers RPC call to return an empty array of workers. This test verifies that the countWorkers method can handle an empty worker list. To enhance the test coverage, consider adding more test cases that simulate different scenarios, such as a non-empty list of workers, to ensure the countWorkers method accurately counts the workers under various conditions.

Comment on lines 45 to 49
public function testGetWorkers(): void
{
$this->rpc->expects($this->once())->method('call')->with('informer.Workers', 'test')->willReturn(['workers' => []]);

$this->workerPool->getWorkers('test');
Copy link

Choose a reason for hiding this comment

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

The testGetWorkers method effectively tests the getWorkers method by mocking the informer.Workers RPC call to return an empty array of workers. Similar to the testCountWorkers method, it would be beneficial to expand the test cases to cover scenarios with non-empty worker lists. Additionally, testing error handling and edge cases, such as unexpected response structures, would further ensure the robustness of the getWorkers method.

Copy link

codecov bot commented Feb 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 52.56%. Comparing base (605e258) to head (6791dc9).

Additional details and impacted files
@@             Coverage Diff              @@
##                3.x      #34      +/-   ##
============================================
+ Coverage     48.37%   52.56%   +4.19%     
- Complexity       94       99       +5     
============================================
  Files             9       11       +2     
  Lines           215      234      +19     
============================================
+ Hits            104      123      +19     
  Misses          111      111              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@msmakouz
Copy link
Member

@FluffyDiscord Hi, I have fixed Psalm and made some improvements to your PR. Can I commit the changes here?

@msmakouz msmakouz added the enhancement New feature or request label Feb 22, 2024
@FluffyDiscord
Copy link
Author

@FluffyDiscord Hi, I have fixed Psalm and made some improvements to your PR. Can I commit the changes here?

yes, no need to ask :]

Copy link

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3f8f866 and 680ec4f.
Files selected for processing (4)
  • src/Informer/Worker.php (1 hunks)
  • src/Informer/Workers.php (1 hunks)
  • src/WorkerPool.php (2 hunks)
  • tests/Unit/WorkerPoolTest.php (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/WorkerPool.php
  • tests/Unit/WorkerPoolTest.php
Additional comments: 2
src/Informer/Workers.php (1)
  • 7-21: The implementation of the Workers class and its adherence to the Countable interface is well done. It provides a clear and straightforward way to manage and count Worker instances. This addition aligns with the PR objectives of enhancing system capabilities without disrupting existing functionality.
src/Informer/Worker.php (1)
  • 7-29: The Worker class is well-structured and provides a clear representation of a worker's state with appropriately typed properties. The use of positive-int in PHPDoc comments for certain properties is a good practice, enhancing type safety and documentation clarity. This class effectively supports the new features introduced in the PR, enabling detailed information about workers to be retrieved.

Copy link

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 680ec4f and 2cd654f.
Files selected for processing (1)
  • src/Informer/Worker.php (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/Informer/Worker.php

@msmakouz msmakouz merged commit 0014c41 into roadrunner-php:3.x Feb 23, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants