Skip to content

Feat- worker start callback#6

Closed
lohanidamodar wants to merge 5 commits into
mainfrom
feat-worker-start-log
Closed

Feat- worker start callback#6
lohanidamodar wants to merge 5 commits into
mainfrom
feat-worker-start-log

Conversation

@lohanidamodar

@lohanidamodar lohanidamodar commented Jun 29, 2025

Copy link
Copy Markdown
Contributor
  • Support callback for worker start
  • Swoole adapter enable coroutine

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added support for registering worker start callbacks in DNS adapters, allowing custom logic to be executed when a worker starts.
    • Exposed the internal server instance in the Swoole DNS adapter for advanced use cases.
    • Enabled coroutine support in the Swoole DNS adapter for improved performance.
    • Introduced configurable server settings in the Swoole DNS adapter for better customization.
  • Chores

    • Updated Dockerfile alias capitalization for consistency.
    • Removed the version declaration from the docker-compose configuration.

@coderabbitai

coderabbitai Bot commented Jun 29, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes introduce new optional onWorkerStart methods to DNS adapter classes, allowing registration of worker start callbacks. The Swoole adapter also enables coroutine support and exposes its server instance. Minor Dockerfile alias capitalizations and the removal of the version declaration from docker-compose.yml are included.

Changes

File(s) Change Summary
Dockerfile Capitalized AS in image alias names in FROM instructions.
docker-compose.yml Removed version: '3' declaration and following blank line.
src/DNS/Adapter.php Added optional onWorkerStart(callable $callback): void method to abstract class.
src/DNS/Adapter/Native.php Added onWorkerStart(callable $callback): void method; invokes callback with server resource and dummy ID.
src/DNS/Adapter/Swoole.php Added configurable server settings, enabled coroutine support in start(). Added onWorkerStart(callable) and getServer().

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Adapter
    participant NativeAdapter
    participant SwooleAdapter
    participant SwooleServer

    User->>Adapter: onWorkerStart(callback)
    Adapter-->>User: (default: does nothing)

    User->>NativeAdapter: onWorkerStart(callback)
    NativeAdapter->>User: callback(server_resource, 0)

    User->>SwooleAdapter: onWorkerStart(callback)
    SwooleAdapter->>SwooleServer: on('WorkerStart', callback)
    SwooleServer->>User: callback(server_instance, workerId)
Loading

Poem

A hop, a skip, a callback call,
Worker starts now greet us all!
Swoole coroutines leap with glee,
Dockerfile’s caps as bold as can be.
Compose file trimmed, so neat and small—
Rabbits cheer these changes, one and all!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 phpcs (3.7.2)
src/DNS/Adapter/Swoole.php

Warning: PHP Startup: Invalid date.timezone value 'UTC', using 'UTC' instead in Unknown on line 0

Fatal error: Uncaught Error: Class "Phar" not found in /usr/local/bin/phpcs:3
Stack trace:
#0 {main}
thrown in /usr/local/bin/phpcs on line 3

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9cf961f and 5ed1451.

📒 Files selected for processing (6)
  • Dockerfile (2 hunks)
  • docker-compose.yml (0 hunks)
  • src/DNS/Adapter.php (1 hunks)
  • src/DNS/Adapter/Native.php (1 hunks)
  • src/DNS/Adapter/Swoole.php (2 hunks)
  • src/DNS/Server.php (1 hunks)
💤 Files with no reviewable changes (1)
  • docker-compose.yml
🧰 Additional context used
🧬 Code Graph Analysis (4)
src/DNS/Server.php (3)
src/DNS/Adapter.php (1)
  • onWorkerStart (31-35)
src/DNS/Adapter/Swoole.php (1)
  • onWorkerStart (41-47)
src/DNS/Adapter/Native.php (1)
  • onWorkerStart (79-86)
src/DNS/Adapter.php (2)
src/DNS/Adapter/Swoole.php (1)
  • onWorkerStart (41-47)
src/DNS/Adapter/Native.php (1)
  • onWorkerStart (79-86)
src/DNS/Adapter/Native.php (2)
src/DNS/Adapter.php (1)
  • onWorkerStart (31-35)
src/DNS/Adapter/Swoole.php (1)
  • onWorkerStart (41-47)
src/DNS/Adapter/Swoole.php (3)
src/DNS/Adapter.php (1)
  • onWorkerStart (31-35)
src/DNS/Adapter/Native.php (1)
  • onWorkerStart (79-86)
src/DNS/Server.php (1)
  • Server (53-460)
🪛 PHPMD (2.15.0)
src/DNS/Server.php

158-158: Avoid using static access to class '\Utopia\CLI\Console' in method 'start'. (Clean Code Rules)

(StaticAccess)

src/DNS/Adapter/Native.php

83-85: The method onWorkerStart uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. (Clean Code Rules)

(ElseExpression)

🪛 GitHub Actions: CodeQL
src/DNS/Adapter/Native.php

[error] 83-83: PHPStan: Else branch is unreachable because previous condition is always true.

src/DNS/Adapter/Swoole.php

[error] 13-13: PHPStan: Property Utopia\DNS\Adapter\Swoole::$workerStartCallback has no type specified.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Unit & E2E
🔇 Additional comments (5)
Dockerfile (1)

1-1: LGTM! Good Docker formatting improvement.

Capitalizing the AS keyword in multi-stage builds follows Docker best practices.

Also applies to: 14-14

src/DNS/Adapter.php (1)

25-35: LGTM! Well-designed extensibility pattern.

The default empty implementation allows adapters to optionally override worker start behavior while maintaining backward compatibility.

src/DNS/Server.php (1)

157-159: LGTM! Useful worker start logging.

The callback provides valuable diagnostic information for monitoring worker processes. The logged details (worker ID, PID, timestamp, memory usage) will be helpful for debugging and performance monitoring.

src/DNS/Adapter/Swoole.php (2)

36-47: LGTM! Proper Swoole event integration.

The implementation correctly registers the callback with Swoole's 'WorkerStart' event and follows the expected callback signature pattern.


49-57: LGTM! Useful server accessor method.

The getServer() method provides necessary access to the internal Swoole server instance for external configuration and monitoring.

Comment thread src/DNS/Adapter/Native.php
Comment thread src/DNS/Adapter/Swoole.php Outdated
protected Server $server;
protected string $host;
protected int $port;
protected $workerStartCallback = null;

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.

⚠️ Potential issue

Add type annotation for the property.

The $workerStartCallback property lacks type specification, causing static analysis failures.

Apply this diff to fix the type annotation:

-protected $workerStartCallback = null;
+protected ?callable $workerStartCallback = null;
🧰 Tools
🪛 GitHub Actions: CodeQL

[error] 13-13: PHPStan: Property Utopia\DNS\Adapter\Swoole::$workerStartCallback has no type specified.

🤖 Prompt for AI Agents
In src/DNS/Adapter/Swoole.php at line 13, the property $workerStartCallback
lacks a type annotation, causing static analysis issues. Add an appropriate type
declaration to the property, such as specifying it as a callable or nullable
callable, to ensure proper type checking and resolve the static analysis
failures.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/DNS/Adapter/Swoole.php (1)

48-56: Consider the trade-offs of exposing the server instance.

While this method provides flexibility for advanced server configuration, it breaks encapsulation by exposing internal state. Consider whether the intended use cases could be addressed through more specific configuration methods instead.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e87e206 and b8bbf36.

📒 Files selected for processing (1)
  • src/DNS/Adapter/Swoole.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/DNS/Adapter/Swoole.php (3)
src/DNS/Adapter.php (2)
  • onWorkerStart (31-35)
  • start (17-17)
src/DNS/Adapter/Native.php (2)
  • onWorkerStart (79-82)
  • start (40-61)
src/DNS/Server.php (2)
  • Server (53-456)
  • start (142-320)
🪛 PHPMD (2.15.0)
src/DNS/Adapter/Swoole.php

63-63: Avoid using static access to class '\Swoole\Runtime' in method 'start'. (Clean Code Rules)

(StaticAccess)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Unit & E2E
🔇 Additional comments (3)
src/DNS/Adapter/Swoole.php (3)

5-5: LGTM! Import statement correctly added.

The Swoole\Runtime import is necessary for the coroutine enablement in the start() method.


36-46: Well-implemented worker start callback registration.

The implementation correctly registers the callback with Swoole's WorkerStart event system, which will trigger when workers actually start. This differs from the Native adapter's immediate execution but is the appropriate approach for Swoole's event-driven architecture.


63-63: Coroutine enablement is correctly implemented.

The Runtime::enableCoroutine() call is the standard and documented way to enable Swoole's coroutine support. The static analysis warning about static access is a false positive in this context.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 3

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8bbf36 and 14e33a2.

📒 Files selected for processing (1)
  • src/DNS/Adapter/Swoole.php (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/DNS/Adapter/Swoole.php (3)
src/DNS/Adapter/Native.php (3)
  • __construct (20-30)
  • onWorkerStart (79-82)
  • start (40-61)
src/DNS/Server.php (3)
  • __construct (70-75)
  • Server (53-456)
  • start (142-320)
src/DNS/Adapter.php (2)
  • onWorkerStart (31-35)
  • start (17-17)
🪛 PHPMD (2.15.0)
src/DNS/Adapter/Swoole.php

145-145: Avoid using static access to class '\Swoole\Runtime' in method 'start'. (Clean Code Rules)

(StaticAccess)

🪛 GitHub Actions: CodeQL
src/DNS/Adapter/Swoole.php

[error] 14-14: PHPStan: Property Utopia\DNS\Adapter\Swoole::$settings type has no value type specified in iterable type array.


[error] 39-39: PHPStan: Method Utopia\DNS\Adapter\Swoole::setSettings() has parameter $settings with no value type specified in iterable type array.


[error] 63-63: PHPStan: Method Utopia\DNS\Adapter\Swoole::getSettings() return type has no value type specified in iterable type array.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Unit & E2E
🔇 Additional comments (5)
src/DNS/Adapter/Swoole.php (5)

5-5: LGTM!

The Swoole\Runtime import is necessary for enabling coroutine support.


21-31: Well-configured default settings!

The default server configuration is appropriate for a DNS server:

  • Worker calculation (2x CPU cores) is suitable for I/O-bound workloads
  • High coroutine limit (10,000) enables good concurrency
  • Worker restart after 10k requests helps prevent memory leaks
  • Dispatch mode 2 is correct for UDP servers

115-125: Correct implementation of worker start callback!

The method properly registers the callback with Swoole's 'WorkerStart' event and passes the expected parameters (server instance and worker ID).


127-136: Good addition for advanced configuration!

Exposing the server instance allows users to configure additional Swoole-specific settings if needed.


140-147: Correct implementation of server startup!

The method properly applies settings before starting the server and enables coroutine support as intended. The static analysis warning about Runtime::enableCoroutine() can be safely ignored as this is the standard Swoole API for enabling coroutines.

Comment on lines +63 to +66
public function getSettings(): array
{
return $this->settings;
}

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.

⚠️ Potential issue

Add value type specification for the return type.

Apply this diff to fix the type specification:

+/**
+ * @return array<string, mixed>
+ */
 public function getSettings(): array
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function getSettings(): array
{
return $this->settings;
}
/**
* @return array<string, mixed>
*/
public function getSettings(): array
{
return $this->settings;
}
🧰 Tools
🪛 GitHub Actions: CodeQL

[error] 63-63: PHPStan: Method Utopia\DNS\Adapter\Swoole::getSettings() return type has no value type specified in iterable type array.

🤖 Prompt for AI Agents
In src/DNS/Adapter/Swoole.php around lines 63 to 66, the getSettings method's
return type is specified as array but lacks a value type specification. Update
the return type declaration to specify the type of values contained in the
array, for example, array<string, mixed> or the appropriate value type based on
the settings property, to improve type safety and clarity.

Comment on lines +39 to +43
public function setSettings(array $settings): self
{
$this->settings = array_merge($this->settings, $settings);
return $this;
}

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.

⚠️ Potential issue

Add value type specification for the settings parameter.

Apply this diff to fix the type specification:

-public function setSettings(array $settings): self
+/**
+ * @param array<string, mixed> $settings
+ */
+public function setSettings(array $settings): self
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function setSettings(array $settings): self
{
$this->settings = array_merge($this->settings, $settings);
return $this;
}
/**
* @param array<string, mixed> $settings
*/
public function setSettings(array $settings): self
{
$this->settings = array_merge($this->settings, $settings);
return $this;
}
🧰 Tools
🪛 GitHub Actions: CodeQL

[error] 39-39: PHPStan: Method Utopia\DNS\Adapter\Swoole::setSettings() has parameter $settings with no value type specified in iterable type array.

🤖 Prompt for AI Agents
In src/DNS/Adapter/Swoole.php around lines 39 to 43, the setSettings method's
parameter $settings lacks a value type specification. Update the method
signature to specify the type of values expected in the $settings array, for
example, array<string, mixed> or the appropriate value type, to improve type
safety and clarity.

protected Server $server;
protected string $host;
protected int $port;
protected array $settings = [];

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.

⚠️ Potential issue

Add value type specification for the array property.

The static analysis indicates that the array type lacks value type specification. Since the settings array contains mixed types (int, bool), specify the value type explicitly.

Apply this diff to fix the type specification:

-protected array $settings = [];
+/** @var array<string, mixed> */
+protected array $settings = [];
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
protected array $settings = [];
/** @var array<string, mixed> */
protected array $settings = [];
🧰 Tools
🪛 GitHub Actions: CodeQL

[error] 14-14: PHPStan: Property Utopia\DNS\Adapter\Swoole::$settings type has no value type specified in iterable type array.

🤖 Prompt for AI Agents
In src/DNS/Adapter/Swoole.php at line 14, the property $settings is declared as
an array without specifying the value types, which causes static analysis
warnings. Update the property declaration to specify the value types explicitly
as int|bool, for example, declare it as protected array<string, int|bool>
$settings to indicate it is an associative array with string keys and values of
type int or bool.

@loks0n

loks0n commented Nov 12, 2025

Copy link
Copy Markdown
Contributor

#21
Rebased with this PR

@loks0n loks0n closed this Nov 12, 2025
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