Class: FunctionsFramework::Server
- Inherits:
-
Object
- Object
- FunctionsFramework::Server
- Includes:
- MonitorMixin
- Defined in:
- lib/functions_framework/server.rb
Overview
A web server that wraps a function.
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#config ⇒ FunctionsFramework::Server::Config
readonly
The final configuration.
-
#function ⇒ FunctionsFramework::Function
readonly
The function to execute.
Instance Method Summary collapse
-
#initialize(function, globals) {|FunctionsFramework::Server::Config| ... } ⇒ Server
constructor
Create a new web server given a function definition, a set of application globals, and server configuration.
-
#respond_to_signals ⇒ self
Cause this server to respond to SIGTERM, SIGINT, and SIGHUP by shutting down gracefully.
-
#running? ⇒ Boolean
Determine if the web server is currently running.
-
#start ⇒ self
Start the web server in the background.
-
#stop(force: false, wait: false) ⇒ self
Stop the web server in the background.
-
#wait_until_stopped(timeout: nil) ⇒ self
Wait for the server to stop.
Constructor Details
#initialize(function, globals) {|FunctionsFramework::Server::Config| ... } ⇒ Server
Create a new web server given a function definition, a set of application globals, and server configuration.
To configure the server, pass a block that takes a Config object as the parameter. This block is the only opportunity to modify the configuration; once the server is initialized, configuration is frozen.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/functions_framework/server.rb', line 45 def initialize function, globals super() @config = Config.new yield @config if block_given? @config.freeze @function = function @app = case function.type when :http HttpApp.new function, globals, @config when :cloud_event EventApp.new function, globals, @config when :typed TypedApp.new function, globals, @config else raise "Unrecognized function type: #{function.type}" end @server = nil @signals_installed = false end |
Instance Attribute Details
#config ⇒ FunctionsFramework::Server::Config (readonly)
The final configuration. This is a frozen object that cannot be modified.
76 77 78 |
# File 'lib/functions_framework/server.rb', line 76 def config @config end |
#function ⇒ FunctionsFramework::Function (readonly)
The function to execute.
70 71 72 |
# File 'lib/functions_framework/server.rb', line 70 def function @function end |
Instance Method Details
#respond_to_signals ⇒ self
Cause this server to respond to SIGTERM, SIGINT, and SIGHUP by shutting down gracefully.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/functions_framework/server.rb', line 161 def respond_to_signals synchronize do return self if @signals_installed ::Signal.trap "SIGTERM" do Server.signal_enqueue "SIGTERM", @config.logger, @server end ::Signal.trap "SIGINT" do Server.signal_enqueue "SIGINT", @config.logger, @server end begin ::Signal.trap "SIGHUP" do Server.signal_enqueue "SIGHUP", @config.logger, @server end rescue ::ArgumentError # Not available on all systems end @signals_installed = true end self end |
#running? ⇒ Boolean
Determine if the web server is currently running
151 152 153 |
# File 'lib/functions_framework/server.rb', line 151 def running? @server&.thread&.alive? end |
#start ⇒ self
Start the web server in the background. Does nothing if the web server is already running.
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 |
# File 'lib/functions_framework/server.rb', line 84 def start synchronize do unless running? # Puma >= 6.0 interprets these settings from options = { min_threads: @config.min_threads, max_threads: @config.max_threads, environment: @config.show_error_details? ? "development" : "production" } # Puma::Events.stdio for Puma < 6.0; otherwise nil for Puma >= 6.0 events = ::Puma::Events.stdio if ::Puma::Events.respond_to? :stdio @server = ::Puma::Server.new @app, events, if @server.respond_to? :min_threads= # Puma < 6.0 sets server attributes for these settings @server.min_threads = @config.min_threads @server.max_threads = @config.max_threads @server.leak_stack_on_error = @config.show_error_details? end @server.binder.add_tcp_listener @config.bind_addr, @config.port @config.logger.info "FunctionsFramework: Serving function #{@function.name.inspect} " \ "on port #{@config.port}..." @server.run true end end self end |
#stop(force: false, wait: false) ⇒ self
Stop the web server in the background. Does nothing if the web server is not running.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/functions_framework/server.rb', line 119 def stop force: false, wait: false synchronize do if running? @config.logger.info "FunctionsFramework: Shutting down server..." if force @server.halt wait else @server.stop wait end end end self end |
#wait_until_stopped(timeout: nil) ⇒ self
Wait for the server to stop. Returns immediately if the server is not running.
141 142 143 144 |
# File 'lib/functions_framework/server.rb', line 141 def wait_until_stopped timeout: nil @server&.thread&.join timeout self end |