class Protobuf::CLI

Attributes

exit_requested[RW]
mode[RW]
runner[RW]

Public Instance Methods

configure_deprecation_warnings() click to toggle source

Tell protobuf how to handle the printing of deprecated field usage.

# File lib/protobuf/cli.rb, line 74
def configure_deprecation_warnings
  ::Protobuf.print_deprecation_warnings =
    if options.print_deprecation_warnings.nil?
      !ENV.key?("PB_IGNORE_DEPRECATIONS")
    else
      options.print_deprecation_warnings?
    end
end
configure_gc() click to toggle source

If we pause during request we don’t need to pause in serialization

# File lib/protobuf/cli.rb, line 84
def configure_gc
  say "DEPRECATED: The gc_pause_request option is deprecated and will be removed in 4.0." if options.gc_pause_request?

  debug_say('Configuring gc')

  ::Protobuf.gc_pause_server_request =
    if defined?(JRUBY_VERSION)
      # GC.enable/disable are noop's on Jruby
      false
    else
      options.gc_pause_request?
    end
end
configure_logger() click to toggle source

Setup the protobuf logger.

# File lib/protobuf/cli.rb, line 99
def configure_logger
  debug_say('Configuring logger')

  log_level = options.debug? ? ::Logger::DEBUG : options.level

  ::Protobuf::Logging.initialize_logger(options.log, log_level)

  # Debug output the server options to the log file.
  logger.debug { 'Debugging options:' }
  logger.debug { options.inspect }
end
configure_process_name(app_file) click to toggle source

Re-write the $0 var to have a nice process name in ps.

# File lib/protobuf/cli.rb, line 112
def configure_process_name(app_file)
  debug_say('Configuring process name')
  $0 = "rpc_server --#{mode} #{options.host}:#{options.port} #{app_file}"
end
configure_runner_mode() click to toggle source

Configure the mode of the server and the runner class.

# File lib/protobuf/cli.rb, line 118
def configure_runner_mode
  debug_say('Configuring runner mode')
  server_type = ENV["PB_SERVER_TYPE"]

  self.mode = if multi_mode?
                say('WARNING: You have provided multiple mode options. Defaulting to socket mode.', :yellow)
                :socket
              elsif options.zmq?
                :zmq
              else
                case server_type
                when nil, /\Asocket[[:space:]]*\z/i
                  :socket
                when /\Azmq[[:space:]]*\z/i
                  :zmq
                else
                  require server_type.to_s
                  server_type
                end
              end
end
configure_traps() click to toggle source

Configure signal traps. TODO: add signal handling for hot-reloading the application.

# File lib/protobuf/cli.rb, line 142
def configure_traps
  debug_say('Configuring traps')

  exit_signals = [:INT, :TERM]
  exit_signals << :QUIT unless defined?(JRUBY_VERSION)

  exit_signals.each do |signal|
    debug_say("Registering trap for exit signal #{signal}", :blue)

    trap(signal) do
      self.exit_requested = true
      shutdown_server
    end
  end
end
create_extension_server_runner() click to toggle source
# File lib/protobuf/cli.rb, line 214
def create_extension_server_runner
  classified = mode.classify
  extension_server_class = classified.constantize

  self.runner = extension_server_class.new(runner_options)
end
create_runner() click to toggle source

Create the runner for the configured mode

# File lib/protobuf/cli.rb, line 159
def create_runner
  debug_say("Creating #{mode} runner")
  self.runner = case mode
                when :zmq
                  create_zmq_runner
                when :socket
                  create_socket_runner
                else
                  say("Extension runner mode: #{mode}")
                  create_extension_server_runner
                end
end
create_socket_runner() click to toggle source
# File lib/protobuf/cli.rb, line 221
def create_socket_runner
  require 'protobuf/socket'

  self.runner = ::Protobuf::Rpc::SocketRunner.new(runner_options)
end
create_zmq_runner() click to toggle source
# File lib/protobuf/cli.rb, line 227
def create_zmq_runner
  require 'protobuf/zmq'

  self.runner = ::Protobuf::Rpc::ZmqRunner.new(runner_options)
end
debug_say(message, color = :yellow) click to toggle source

Say something if we’re in debug mode.

# File lib/protobuf/cli.rb, line 173
def debug_say(message, color = :yellow)
  say(message, color) if options.debug?
end
multi_mode?() click to toggle source

Internal helper to determine if the modes are multi-set which is not valid.

# File lib/protobuf/cli.rb, line 178
def multi_mode?
  options.zmq? && options.socket?
end
require_application(app_file) click to toggle source

Require the application file given, exiting if the file doesn’t exist.

# File lib/protobuf/cli.rb, line 183
def require_application(app_file)
  debug_say('Requiring app file')
  require app_file
rescue LoadError => e
  say_and_exit("Failed to load application file #{app_file}", e)
end
runner_options() click to toggle source
# File lib/protobuf/cli.rb, line 190
def runner_options
  opt = options.to_hash.symbolize_keys

  opt[:workers_only] = (!!ENV['PB_WORKERS_ONLY']) || options.workers_only

  opt
end
say_and_exit(message, exception = nil) click to toggle source
# File lib/protobuf/cli.rb, line 198
def say_and_exit(message, exception = nil)
  message = set_color(message, :red) if options.log == STDOUT

  logger.error { message }

  if exception
    $stderr.puts "[#{exception.class.name}] #{exception.message}"
    $stderr.puts exception.backtrace.join("\n")

    logger.error { "[#{exception.class.name}] #{exception.message}" }
    logger.debug { exception.backtrace.join("\n") }
  end

  exit(1)
end
shutdown_server() click to toggle source
# File lib/protobuf/cli.rb, line 233
def shutdown_server
  logger.info { 'RPC Server shutting down...' }
  runner.stop
  ::Protobuf::Rpc::ServiceDirectory.instance.stop
end
start(app_file) click to toggle source
# File lib/protobuf/cli.rb, line 49
def start(app_file)
  debug_say('Configuring the rpc_server process')

  configure_logger
  configure_traps
  configure_runner_mode
  create_runner
  configure_process_name(app_file)
  configure_gc
  configure_deprecation_warnings

  require_application(app_file) unless exit_requested?
  start_server unless exit_requested?
rescue => e
  say_and_exit('ERROR: RPC Server failed to start.', e)
end
start_server() click to toggle source

Start the runner and log the relevant options.

# File lib/protobuf/cli.rb, line 240
def start_server
  debug_say('Running server')

  ::ActiveSupport::Notifications.instrument("before_server_bind")

  runner.run do
    logger.info do
      "pid #{::Process.pid} -- #{mode} RPC Server listening at #{options.host}:#{options.port}"
    end

    ::ActiveSupport::Notifications.instrument("after_server_bind")
  end

  logger.info { 'Shutdown complete' }
end
version() click to toggle source
# File lib/protobuf/cli.rb, line 67
def version
  say("Ruby Protobuf v#{::Protobuf::VERSION}")
end