Class: Raptor::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor/cli.rb,
sig/generated/raptor/cli.rbs

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 =

Returns:

  • (Object)
Etc.nprocessors
DEFAULT_OPTIONS =

Returns:

  • (Object)
{
  binds: ["tcp://0.0.0.0:9292"].freeze,
  threads: 3,
  ractors: 1,
  workers: DEFAULT_WORKER_COUNT,
  rackup: "config.ru",
  stats_file: "tmp/raptor.json",
  client: {
    first_data_timeout: 30,
    chunk_data_timeout: 10,
    persistent_data_timeout: 65,
  },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/raptor/cli.rb', line 64

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

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

Instance Method Details

#create_parserOptionParser

Creates the OptionParser instance with all supported command-line options.

Returns:

  • (OptionParser)

    configured option parser



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/raptor/cli.rb', line 119

def create_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: raptor [options] [rackup file]"

    opts.on("-b", "--bind URI", String, "Bind address (default: tcp://0.0.0.0:9292)") do |bind|
      if @options[:binds] == DEFAULT_OPTIONS[:binds]
        @options[:binds] = [bind]
      else
        @options[:binds] << bind
      end
    end

    opts.on("-t", "--threads NUM", Integer, "Number of threads (default: 3)") do |num|
      @options[:threads] = num
    end

    opts.on("-r", "--ractors NUM", Integer, "Number of ractors (default: 1)") do |num|
      @options[:ractors] = num
    end

    opts.on("-w", "--workers NUM", Integer, "Number of worker processes (default: #{DEFAULT_WORKER_COUNT})") do |num|
      @options[:workers] = num
    end

    opts.on("--first-data-timeout SECONDS", Integer, "First data timeout in seconds (default: 30)") do |timeout|
      @options[:client][:first_data_timeout] = timeout
    end

    opts.on("--chunk-data-timeout SECONDS", Integer, "Chunk data timeout in seconds (default: 10)") do |timeout|
      @options[:client][:chunk_data_timeout] = timeout
    end

    opts.on("--persistent-data-timeout SECONDS", Integer, "Persistent data timeout in seconds (default: 65)") do |timeout|
      @options[:client][:persistent_data_timeout] = timeout
    end

    opts.on("--stats-file PATH", String, "Stats file path (default: tmp/raptor.json)") do |path|
      @options[:stats_file] = path
    end

    opts.on("--help", "Show this help") do
      puts opts
      exit
    end

    opts.on("-v", "--version", "Show version") do
      puts Raptor::VERSION
      exit
    end
  end
end

#runvoid

This method returns an undefined value.

Runs the requested command.



84
85
86
# File 'lib/raptor/cli.rb', line 84

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

#run_statsvoid

This method returns an undefined value.

Reads and prints the stats file.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/raptor/cli.rb', line 95

def run_stats
  stats_file = @options[:stats_file]

  unless File.exist?(stats_file)
    warn "No stats file at #{stats_file.inspect}. Is Raptor running?"
    exit 1
  end

  data = JSON.parse(File.read(stats_file), symbolize_names: true)

  puts "Master PID: #{data[:master_pid]}"
  data[:workers].each_with_index do |worker, index|
    status = worker[:booted] ? "booted" : "starting"
    last_checkin = Time.at(worker[:last_checkin]).strftime("%H:%M:%S")
    puts "Worker #{index}: pid=#{worker[:pid]}, requests=#{worker[:requests]}, " \
         "backlog=#{worker[:backlog]}, #{status}, last_checkin=#{last_checkin}"
  end
end