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 |
# 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 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.
74 75 76 |
# File 'lib/functions_framework/server.rb', line 74 def config @config end |
#function ⇒ FunctionsFramework::Function (readonly)
The function to execute.
68 69 70 |
# File 'lib/functions_framework/server.rb', line 68 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.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/functions_framework/server.rb', line 148 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 # rubocop:disable Lint/HandleExceptions # Not available on all systems end @signals_installed = true end self end |
#running? ⇒ Boolean
Determine if the web server is currently running
138 139 140 |
# File 'lib/functions_framework/server.rb', line 138 def running? @server&.thread&.alive? end |
#start ⇒ self
Start the web server in the background. Does nothing if the web server is already running.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/functions_framework/server.rb', line 82 def start synchronize do unless running? @server = ::Puma::Server.new @app @server.min_threads = @config.min_threads @server.max_threads = @config.max_threads @server.leak_stack_on_error = @config.show_error_details? @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.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/functions_framework/server.rb', line 106 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.
128 129 130 131 |
# File 'lib/functions_framework/server.rb', line 128 def wait_until_stopped timeout: nil @server&.thread&.join timeout self end |