class FunctionsFramework::Server

A web server that wraps a function.

Attributes

config[R]

The final configuration. This is a frozen object that cannot be modified. @return [FunctionsFramework::Server::Config]

function[R]

The function to execute. @return [FunctionsFramework::Function]

Public Class Methods

new(function, globals) { |config| ... } click to toggle source

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 {FunctionsFramework::Server::Config} object as the parameter. This block is the only opportunity to modify the configuration; once the server is initialized, configuration is frozen.

@param function [FunctionsFramework::Function] The function to execute. @param globals [Hash] Globals to pass to invocations. This hash should

normally be frozen so separate function invocations cannot interfere
with one another's globals.

@yield [FunctionsFramework::Server::Config] A config object that can be

manipulated to configure this server.
Calls superclass method
# 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
signal_enqueue(signal, logger, server) click to toggle source

@private

# File lib/functions_framework/server.rb, line 183
def signal_enqueue signal, logger, server
  @signal_queue << [signal, logger, server]
end
start_signal_queue() click to toggle source

@private

# File lib/functions_framework/server.rb, line 171
def start_signal_queue
  @signal_queue = ::Queue.new
  ::Thread.start do
    loop do
      signal, logger, server = @signal_queue.pop
      logger.info "FunctionsFramework: Caught #{signal}; shutting down server..."
      server&.stop
    end
  end
end

Public Instance Methods

respond_to_signals() click to toggle source

Cause this server to respond to SIGTERM, SIGINT, and SIGHUP by shutting down gracefully.

@return [self]

# 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
      # Not available on all systems
    end
    @signals_installed = true
  end
  self
end
running?() click to toggle source

Determine if the web server is currently running

@return [Boolean]

# File lib/functions_framework/server.rb, line 138
def running?
  @server&.thread&.alive?
end
start() click to toggle source

Start the web server in the background. Does nothing if the web server is already running.

@return [self]

# 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) click to toggle source

Stop the web server in the background. Does nothing if the web server is not running.

@param force [Boolean] Use a forced halt instead of a graceful shutdown @param wait [Boolean] Block until shutdown is complete @return [self]

# 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) click to toggle source

Wait for the server to stop. Returns immediately if the server is not running.

@param timeout [nil,Numeric] The timeout. If `nil` (the default), waits

indefinitely, otherwise times out after the given number of seconds.

@return [self]

# File lib/functions_framework/server.rb, line 128
def wait_until_stopped timeout: nil
  @server&.thread&.join timeout
  self
end