Class: Raptor::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor/cli.rb

Overview

Command-line interface for the Raptor web server.

CLI parses command-line arguments and starts the server cluster with the specified configuration options. It supports configuring the number of workers, threads, ractors, bind addresses, and various client timeout settings.

Examples:

Basic usage

cli = Raptor::CLI.new(["config.ru", "-t", "8", "-w", "4"])
cli.run

With custom timeouts

cli = Raptor::CLI.new(["--first-data-timeout", "60", "--threads", "8"])
cli.run

Constant Summary collapse

DEFAULT_WORKER_COUNT =
Etc.nprocessors
DEFAULT_OPTIONS =
{
  binds: ["tcp://0.0.0.0:9292"].freeze,
  threads: 3,
  ractors: 1,
  workers: DEFAULT_WORKER_COUNT,
  rackup: "config.ru",
  client: {
    first_data_timeout: 30,
    chunk_data_timeout: 10,
    persistent_data_timeout: 65,
    max_body_size: nil,
    body_spool_threshold: 1024 * 1024,
  },
  stats_file: "tmp/raptor.json",
  pid_file: nil,
}.freeze
DEFAULT_CONFIG_PATHS =
["raptor.rb", "config/raptor.rb"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ void

Creates a new CLI instance and parses command-line arguments.

Parses the provided command-line arguments and configures the server options accordingly. A rackup file can be provided as the first positional argument (defaults to config.ru).

Examples:

With rackup file

cli = CLI.new(["my_app.ru", "-w", "4"])

With options only

cli = CLI.new(["-t", "8", "-r", "2"])

Parameters:

  • argv (Array<String>)

    command-line arguments to parse

Raises:

  • (OptionParser::ParseError)

    if invalid options are provided



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/raptor/cli.rb', line 101

def initialize(argv)
  if argv.first == "stats"
    argv.shift
    @command = :stats
  else
    @command = :server
  end
  @options = DEFAULT_OPTIONS.dup
  @options[:client] = @options[:client].dup

  apply_config_file(extract_config_path(argv) || self.class.default_config_path)

  @parser = create_parser
  @parser.parse!(argv)

  @options[:rackup] = argv.first if @command == :server && argv.first
end

Class Method Details

.default_config_path(root = Dir.pwd) ⇒ String?

Returns the first existing path in DEFAULT_CONFIG_PATHS resolved against ‘root`, or nil if none exist.

Used to pick up a project-local config file when no ‘-c`/`–config` flag was supplied.

Parameters:

  • root (String) (defaults to: Dir.pwd)

    directory to resolve the default paths against

Returns:

  • (String, nil)

    the config path, or nil if no default file exists



76
77
78
# File 'lib/raptor/cli.rb', line 76

def self.default_config_path(root = Dir.pwd)
  DEFAULT_CONFIG_PATHS.find { |path| File.exist?(File.join(root, path)) }
end

.load_config_file(path) ⇒ Hash{Symbol => untyped}

Loads a configuration file and returns the hash it evaluates to.

The file is evaluated at the top level so constants like ‘Raptor::*` resolve the same as in a regular Ruby script. The final expression must be a Hash of cluster options (the same keys accepted by Raptor::Cluster#initialize).

Parameters:

  • path (String)

    path to a Ruby file that evaluates to a Hash

Returns:

  • (Hash{Symbol => untyped})

    cluster options

Raises:

  • (ArgumentError)

    if the file does not evaluate to a Hash



59
60
61
62
63
64
# File 'lib/raptor/cli.rb', line 59

def self.load_config_file(path)
  config = eval(File.read(path), TOPLEVEL_BINDING, path, 1)
  raise ArgumentError, "Config file at #{path.inspect} must return a Hash, got #{config.class}" unless config.is_a?(Hash)

  config
end

Instance Method Details

#runvoid

This method returns an undefined value.

Runs the requested command.



124
125
126
# File 'lib/raptor/cli.rb', line 124

def run
  @command == :stats ? run_stats : Cluster.run(@options)
end