Class: AnyCable::Server
- Inherits:
-
Object
- Object
- AnyCable::Server
- Defined in:
- lib/anycable/server.rb
Overview
Wrapper over gRPC server.
Basic example:
# create new server listening on the loopback interface with 50051 port
server = AnyCable::Server.new(host: "127.0.0.1:50051")
# run gRPC server in bakground
server.start
# stop server
server.stop
Instance Attribute Summary collapse
-
#grpc_server ⇒ Object
readonly
Returns the value of attribute grpc_server.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Class Method Summary collapse
-
.start(**options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
Instance Method Summary collapse
-
#initialize(host:, logger: AnyCable.logger, **options) ⇒ Server
constructor
A new instance of Server.
- #running? ⇒ Boolean
-
#start ⇒ Object
Start gRPC server in background and wait untill it ready to accept connections.
-
#stop ⇒ Object
Stop gRPC server if it’s running.
- #stopped? ⇒ Boolean
- #wait_till_terminated ⇒ Object
Constructor Details
#initialize(host:, logger: AnyCable.logger, **options) ⇒ Server
Returns a new instance of Server.
68 69 70 71 72 |
# File 'lib/anycable/server.rb', line 68 def initialize(host:, logger: AnyCable.logger, **) @logger = logger @host = host @grpc_server = build_server() end |
Instance Attribute Details
#grpc_server ⇒ Object (readonly)
Returns the value of attribute grpc_server.
66 67 68 |
# File 'lib/anycable/server.rb', line 66 def grpc_server @grpc_server end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
66 67 68 |
# File 'lib/anycable/server.rb', line 66 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(**) warn " DEPRECATION WARNING: Using AnyCable::Server.start is deprecated!\n Please, use anycable CLI instead.\n\n See https://docs.anycable.io/#upgrade_to_0_6_0\n DEPRECATION\n\n AnyCable.server_callbacks.each(&:call)\n\n server = new(\n host: AnyCable.config.rpc_host,\n **AnyCable.config.to_grpc_params,\n interceptors: AnyCable.middleware.to_a,\n **options\n )\n\n AnyCable.middleware.freeze\n\n if AnyCable.config.http_health_port_provided?\n health_server = AnyCable::HealthServer.new(\n server,\n **AnyCable.config.to_http_health_params\n )\n health_server.start\n end\n\n at_exit do\n server.stop\n health_server&.stop\n end\n\n AnyCable.logger.info \"Broadcasting Redis channel: \#{AnyCable.config.redis_channel}\"\n\n server.start\n server.wait_till_terminated\nend\n" |
Instance Method Details
#running? ⇒ Boolean
107 108 109 |
# File 'lib/anycable/server.rb', line 107 def running? grpc_server.running_state == :running end |
#start ⇒ Object
Start gRPC server in background and wait untill it ready to accept connections
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/anycable/server.rb', line 76 def start return if running? raise "Cannot re-start stopped server" if stopped? check_default_host 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 |
#stop ⇒ Object
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
111 112 113 |
# File 'lib/anycable/server.rb', line 111 def stopped? grpc_server.running_state == :stopped end |
#wait_till_terminated ⇒ Object
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 |