-
Notifications
You must be signed in to change notification settings - Fork 15
Add opt-in client/server mode to the rdx executable #869
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
Open
paracycle
wants to merge
1
commit into
main
Choose a base branch
from
uk-add-client-server-for-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rubydex/cli/command" | ||
|
|
||
| module Rubydex | ||
| module CLI | ||
| class Command | ||
| class Server < Command | ||
| command "server" | ||
| arguments "<action>" | ||
| summary "Manage the resident server (start, stop, restart, status)" | ||
|
|
||
| ACTIONS = ["start", "stop", "restart", "status"].freeze #: Array[String] | ||
|
|
||
| USAGE = <<~TEXT #: String | ||
| Usage: rdx server <action> [options] | ||
|
|
||
| Actions: | ||
| start Start the server for this workspace | ||
| stop Stop the running server for this workspace | ||
| restart Restart the server for this workspace | ||
| status Print the status of the server for this workspace | ||
| TEXT | ||
|
|
||
| #: -> void | ||
| def run | ||
| # Options are parsed before the action is shifted, so a flag is not read as the action. | ||
| # `OptionParser#parse!` permutes, so the action may come before or after a flag. | ||
| parse_options!(options: true, banner: USAGE) | ||
|
|
||
| action = argv.shift | ||
| abort_with_actions("unknown server action: #{action.inspect}") unless ACTIONS.include?(action) | ||
|
|
||
| require "rubydex/server" | ||
|
|
||
| unless Rubydex::Server.supported? | ||
| abort("rdx server mode is not supported on this platform " \ | ||
| "(requires fork, UNIX sockets and O_NOFOLLOW)") | ||
| end | ||
|
|
||
| exit(dispatch_action(action)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| #: (String action) -> Integer | ||
| def dispatch_action(action) | ||
| state = Rubydex::Server::State.new(workspace_path: Dir.pwd) | ||
|
|
||
| case action | ||
| when "start" then Rubydex::Server::Commands.start(state) | ||
| when "stop" then Rubydex::Server::Commands.stop(state) | ||
| when "restart" then Rubydex::Server::Commands.restart(state) | ||
| else Rubydex::Server::Commands.status(state) | ||
| end | ||
| end | ||
|
|
||
| # Uses the action list, not the top-level command list, because the error is about an action | ||
| # of this command. | ||
| #: (String message) -> void | ||
| def abort_with_actions(message) | ||
| warn(message) | ||
| warn("") | ||
| warn(USAGE) | ||
| exit(1) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Rubydex | ||
| # The CLI and the server print the same progress lines, so the measurement is defined here, not | ||
| # in either of them. | ||
| module Progress | ||
| class << self | ||
| # The server passes a `nil` `io` when it has no log. | ||
| #: (IO? io, String message) { -> void } -> void | ||
| def with_timer(io, message) | ||
| unless io | ||
| yield | ||
| return | ||
| end | ||
|
|
||
| io.print(message) | ||
| start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) | ||
| yield | ||
| duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start | ||
| io.puts(" finished in #{duration.round(2)}ms") | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rubydex/progress" | ||
| require "rubydex/version" | ||
|
|
||
| module Rubydex | ||
| # Client/server mode for the `rdx` executable. | ||
| # | ||
| # A resident server process indexes and resolves the workspace once, and keeps the graph in | ||
| # memory. Later commands reach it over a UNIX domain socket and skip that work. | ||
| module Server | ||
| # Increase this after any incompatible change to the request or response shape. | ||
| PROTOCOL = 1 | ||
|
|
||
| class Error < StandardError; end | ||
|
|
||
| class << self | ||
| # Without `O_NOFOLLOW`, a symlink can redirect the permissions this code sets on its runtime | ||
| # directory. | ||
| #: -> bool | ||
| def supported? | ||
| Process.respond_to?(:fork) && defined?(::UNIXSocket) && !Gem.win_platform? && | ||
| !State::NOFOLLOW.nil? | ||
| end | ||
|
|
||
| #: (workspace_path: String, ?progress_io: IO?) -> Rubydex::Graph | ||
| def build_graph(workspace_path:, progress_io: nil) | ||
| # The server boot must build the same graph as the inline CLI path. | ||
| graph = Rubydex::Graph.configure_for_workspace(workspace_path) | ||
| Progress.with_timer(progress_io, "Indexing workspace...") { graph.index_workspace } | ||
| Progress.with_timer(progress_io, "Resolving graph...") { graph.resolve } | ||
| graph | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| require "rubydex/server/state" | ||
| require "rubydex/server/frame" | ||
| require "rubydex/server/core" | ||
| require "rubydex/server/client" | ||
| require "rubydex/server/commands" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this defeat the
--serverfast path?CLI.startstill requiresrubydexbefore dispatch, sordx query --server ...loads the native extension in the client beforeQuery#server_available?runs. That means the client keeps paying the extension load cost and can still fail when the local extension is incompatible, even if a server could answer. I think we need to move the native require into the inline commands/paths (query --schema,querywithout--server, console/mcp as needed) and keep the top-level dispatcher Ruby-only.