Class: AnyCable::CLI

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

Overview

Command-line interface for running AnyCable RPC server

Constant Summary collapse

APP_CANDIDATES =

(not-so-big) List of common boot files for different applications

%w[
  ./config/anycable.rb
  ./config/environment.rb
].freeze
WAIT_PROCESS =

Wait for external process termination (s)

2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(embedded: false) ⇒ CLI

Returns a new instance of CLI.



33
34
35
# File 'lib/anycable/cli.rb', line 33

def initialize(embedded: false)
  @embedded = embedded
end

Instance Attribute Details

#embeddedObject (readonly) Also known as: embedded?

Returns the value of attribute embedded.



30
31
32
# File 'lib/anycable/cli.rb', line 30

def embedded
  @embedded
end

#health_serverObject (readonly)

Returns the value of attribute health_server.



30
31
32
# File 'lib/anycable/cli.rb', line 30

def health_server
  @health_server
end

#serverObject (readonly)

Returns the value of attribute server.



30
31
32
# File 'lib/anycable/cli.rb', line 30

def server
  @server
end

Class Method Details

.embed!(args = []) ⇒ Object

Run CLI inside the current process and shutdown at exit



23
24
25
26
27
28
# File 'lib/anycable/cli.rb', line 23

def self.embed!(args = [])
  new(embedded: true).tap do |cli|
    cli.run(args)
    at_exit { cli.shutdown }
  end
end

Instance Method Details

#run(args = []) ⇒ Object



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
# File 'lib/anycable/cli.rb', line 37

def run(args = [])
  @at_stop = []

  extra_options = parse_cli_options!(args)

  # Boot app first, 'cause it might change
  # configuration, loggin settings, etc.
  boot_app! unless embedded?

  # Make sure Rails extensions for Anyway Config are loaded
  # See https://github.com/anycable/anycable-rails/issues/63
  require "anyway/rails" if defined?(::Rails::VERSION)

  parse_gem_options!(extra_options)

  configure_server!

  logger.info "Starting AnyCable RPC server (pid: #{Process.pid})"

  print_version!

  logger.info "Serving #{defined?(::Rails) ? "Rails " : ""}application from #{boot_file}" unless embedded?

  verify_connection_factory!

  log_errors!

  verify_server_builder!

  check_version!

  configure_middleware!

  @server = AnyCable.server_builder.call(config)

  # Make sure middlewares are not adding after server has started
  AnyCable.middleware.freeze

  start_health_server! if config.http_health_port_provided?
  start_pubsub!

  server.start

  run_custom_server_command! unless server_command.nil?

  return if embedded?

  begin
    wait_till_terminated
  rescue Interrupt => e
    logger.info "Stopping... #{e.message}"

    shutdown

    logger.info "Stopped. Good-bye!"
    exit(0)
  end
end

#shutdownObject



96
97
98
99
# File 'lib/anycable/cli.rb', line 96

def shutdown
  at_stop.each(&:call)
  server&.stop
end