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) {|FunctionsFramework::Server::Config| ... } ⇒ Server
constructor
Create a new web server given a function.
-
#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) {|FunctionsFramework::Server::Config| ... } ⇒ Server
Create a new web server given a function. Yields a Config object that you can use to set server configuration parameters. This block is the only opportunity to set configuration; once the server is initialized, configuration is frozen.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/functions_framework/server.rb', line 40 def initialize function super() @config = Config.new yield @config if block_given? @config.freeze @function = function @app = case function.type when :http HttpApp.new function, @config when :cloud_event EventApp.new function, @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.
69 70 71 |
# File 'lib/functions_framework/server.rb', line 69 def config @config end |
#function ⇒ FunctionsFramework::Function (readonly)
The function to execute.
63 64 65 |
# File 'lib/functions_framework/server.rb', line 63 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.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/functions_framework/server.rb', line 143 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
133 134 135 |
# File 'lib/functions_framework/server.rb', line 133 def running? @server&.thread&.alive? end |
#start ⇒ self
Start the web server in the background. Does nothing if the web server is already running.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/functions_framework/server.rb', line 77 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 @server.run true @config.logger.info "FunctionsFramework: Serving function #{@function.name.inspect}" \ " on port #{@config.port}..." 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.
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/functions_framework/server.rb', line 101 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.
123 124 125 126 |
# File 'lib/functions_framework/server.rb', line 123 def wait_until_stopped timeout: nil @server&.thread&.join timeout self end |