Class: AnyCable::Server

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

Overview

Wrapper over gRPC server.

Basic example:

# create new server listening on [::]:50051 (default host)
server = AnyCable::Server.new(host: "[::]:50051")

# run gRPC server in bakground
server.start

# stop server
server.stop

Constant Summary collapse

DEFAULT_HOST =
"0.0.0.0:50051"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: DEFAULT_HOST, logger: AnyCable.logger, **options) ⇒ Server

Returns a new instance of Server.



70
71
72
73
74
# File 'lib/anycable/server.rb', line 70

def initialize(host: DEFAULT_HOST, logger: AnyCable.logger, **options)
  @logger = logger
  @host = host
  @grpc_server = build_server(options)
end

Instance Attribute Details

#grpc_serverObject (readonly)

Returns the value of attribute grpc_server.



68
69
70
# File 'lib/anycable/server.rb', line 68

def grpc_server
  @grpc_server
end

#hostObject (readonly)

Returns the value of attribute host.



68
69
70
# File 'lib/anycable/server.rb', line 68

def host
  @host
end

Class Method Details

.start(**options) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



26
27
28
29
30
31
32
33
34
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
# File 'lib/anycable/server.rb', line 26

def start(**options)
  warn <<~DEPRECATION
    Using AnyCable::Server.start is deprecated!
    Please, use anycable CLI instead.

    See https://docs.anycable.io/#upgrade_to_0_6_0
  DEPRECATION

  AnyCable.server_callbacks.each(&:call)

  server = new(
    host: AnyCable.config.rpc_host,
    **AnyCable.config.to_grpc_params,
    interceptors: AnyCable.middleware.to_a,
    **options
  )

  AnyCable.middleware.freeze

  if AnyCable.config.http_health_port_provided?
    health_server = AnyCable::HealthServer.new(
      server,
      **AnyCable.config.to_http_health_params
    )
    health_server.start
  end

  at_exit do
    server.stop
    health_server&.stop
  end

  AnyCable.logger.info "Broadcasting Redis channel: #{AnyCable.config.redis_channel}"

  server.start
  server.wait_till_terminated
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/anycable/server.rb', line 107

def running?
  grpc_server.running_state == :running
end

#startObject

Start gRPC server in background and wait untill it ready to accept connections



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/anycable/server.rb', line 78

def start
  return if running?

  raise "Cannot re-start stopped server" if stopped?

  logger.info "RPC server is starting..."

  @start_thread = Thread.new { grpc_server.run }

  grpc_server.wait_till_running

  logger.info "RPC server is listening on #{host}"
end

#stopObject

Stop gRPC server if it’s running



99
100
101
102
103
104
105
# File 'lib/anycable/server.rb', line 99

def stop
  return unless running?

  grpc_server.stop

  logger.info "RPC server stopped"
end

#stopped?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/anycable/server.rb', line 111

def stopped?
  grpc_server.running_state == :stopped
end

#wait_till_terminatedObject



92
93
94
95
96
# File 'lib/anycable/server.rb', line 92

def wait_till_terminated
  raise "Server is not running" unless running?

  start_thread.join
end