Class: Gritz::CLI
- Inherits:
-
Object
- Object
- Gritz::CLI
- Defined in:
- /home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/cli.rb
Overview
Starts and operates servers, lists routes, or checks configuration and fork safety.
Constant Summary collapse
- CHILD_BOOTSTRAP =
<<~RUBY require "socket" fd = ENV.delete("GRITZ_INTERNAL_OWNER_FD") exit Gritz::CLI.new(owner_io: UNIXSocket.for_fd(Integer(fd))).run(ARGV) RUBY
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(stdout: $stdout, stderr: $stderr, env: ENV, status_io: nil, owner_io: nil, launch: false, launch_command: nil) ⇒ CLI
constructor
A new instance of CLI.
- #run(argv) ⇒ Object
Constructor Details
#initialize(stdout: $stdout, stderr: $stderr, env: ENV, status_io: nil, owner_io: nil, launch: false, launch_command: nil) ⇒ CLI
Returns a new instance of CLI.
25 26 27 28 29 30 31 32 33 |
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/cli.rb', line 25 def initialize(stdout: $stdout, stderr: $stderr, env: ENV, status_io: nil, owner_io: nil, launch: false, launch_command: nil) @stdout = stdout @stderr = stderr @env = env @status_io = status_io @owner_channel = Supervisor::StatusChannel.new(owner_io) if owner_io @launch = launch @launch_command = launch_command end |
Class Method Details
.child_command(argv) ⇒ Object
20 21 22 23 |
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/cli.rb', line 20 def self.child_command(argv) library = defined?(Transport::Async) ? "gritz/async" : "gritz/core" [RbConfig.ruby, "-I", $LOAD_PATH.join(File::PATH_SEPARATOR), "-r#{library}", "-e", CHILD_BOOTSTRAP, "--", *argv] end |
Instance Method Details
#run(argv) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/cli.rb', line 35 def run(argv) args = argv.dup path = nil overrides = {} parser = OptionParser.new do || . = "Usage: gritz [start|routes|check|stats|stop|restart] [-C config/gritz.rb] [options]" .on("-C", "--config PATH", "Configuration file") { |value| path = value } .on("--workers N", Integer) { |value| overrides[:workers] = value } .on("--threads N", Integer) { |value| overrides[:threads] = value } .on("--bind ADDRESS") { |value| overrides[:bind] = value } .on("--admin-bind ADDRESS", "Admin HTTP host:port (operations do not load config)") { |value| overrides[:admin_bind] = value } .on("--pid-file PATH", "Active master PID file; operations fall back if Admin is unavailable") { |value| overrides[:pid_file] = value } .on("--strict-routes") { overrides[:strict_routes] = true } .on("-v", "--version") { @stdout.puts(Core::VERSION) return 0 } .on("-h", "--help") { @stdout.puts() return 0 } end parser.parse!(args) command = args.shift || "start" raise ConfigurationError, "Unknown command #{command}" unless %w[start routes check stats stop restart].include?(command) raise ConfigurationError, "Unexpected argument #{args.first}" unless args.empty? if %w[stats stop restart].include?(command) raise ConfigurationError, "#{command} does not load configuration; use --admin-bind or --pid-file instead of -C" if path return run_operation(command, overrides) end logger = Logger.new(@stdout) logger.formatter = ->(_severity, _time, _progname, ) { "#{}\n" } if command == "start" && @launch && !@owner_channel return Supervisor::Launcher.new(command: @launch_command || self.class.child_command(argv), env: @env, stdout: @stdout, stderr: @stderr, logger: logger, status_io: @status_io).run end path ||= "config/gritz.rb" if File.file?("config/gritz.rb") guard = ForkGuard.activate(mode: :record) if command != "routes" if command != "routes" && Gem.loaded_specs.key?("gritz-native") && !defined?(Transport::Async) require "gritz/native" end config = Configuration.load(path: path, env: @env, overrides: overrides) if command == "check" config.preload! if config.preload_app? guard.violations.each { |violation| @stderr.puts(violation.) } return 1 unless guard.violations.empty? config.validate_runtime! require "gritz/#{config.transport}" Router.new(controllers: config.controllers, strict: config.strict_routes, logger: logger) @stdout.puts "Configuration and fork safety checks passed" elsif command == "start" config.validate_runtime! require "gritz/#{config.transport}" if config.workers.positive? && config.fork_mode == :clean raise guard.violations.first if config.fork_guard == :raise && !guard.violations.empty? guard.violations.each { |violation| logger.warn(violation.) } if config.fork_guard == :warn end @stdout.sync = true if @stdout.respond_to?(:sync=) @owner_channel&.write(type: "configured", pid: Process.pid, workers: config.workers, bind: config.bind, admin_bind: config.admin_bind, min_ready_workers: config.min_ready_workers, pid_file: config.pid_file, reexec_timeout: config.reexec_timeout, drain_delay: config.drain_delay, shutdown_timeout: config.shutdown_timeout, listener_strategy: config.listener_strategy.to_s) if @owner_channel && config.listener_strategy == :inherited_fd until @owner_channel.flush raise ConfigurationError, "listener owner closed its control channel" if @owner_channel.closed? @owner_channel.io.wait_writable(0.01) end begin listener = @owner_channel.io.recv_io(Socket) rescue IOError, SocketError => e raise ConfigurationError, "listener owner failed to pass its socket: #{e.}" end end if config.workers.positive? return Supervisor::Master.new(config, logger: logger, status_io: @status_io, owner_channel: @owner_channel, listener:).run end ForkGuard.deactivate config.preload! if config.preload_app? return Worker::Runner.new(index: 0, config: config, logger: logger, owner_channel: @owner_channel, listener:).run else config.preload! if config.preload_app? router = Router.new(controllers: config.controllers, strict: config.strict_routes, logger: logger) end if command == "routes" router.routes.each_value do |route| @stdout.puts "#{route.full_name} #{route.kind} #{route.controller}##{route.action}" end end 0 rescue ConfigurationError, ForkGuard::Violation, ArgumentError, Errno::ENOENT, SyntaxError, LoadError, RuntimeError => e @stderr.puts "gritz: #{e.}" 1 ensure ForkGuard.deactivate if guard @owner_channel&.close listener&.close unless listener&.closed? end |