Class: FunctionsFramework::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

Yields:



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

#configFunctionsFramework::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

#functionFunctionsFramework::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_signalsself

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

Returns:

  • (self)


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

Returns:

  • (Boolean)


133
134
135
# File 'lib/functions_framework/server.rb', line 133

def running?
  @server&.thread&.alive?
end

#startself

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

Returns:

  • (self)


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.

Parameters:

  • force (Boolean) (defaults to: false)

    Use a forced halt instead of a graceful shutdown

  • wait (Boolean) (defaults to: false)

    Block until shutdown is complete

Returns:

  • (self)


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.

Parameters:

  • timeout (nil, Numeric) (defaults to: nil)

    The timeout. If nil (the default), waits indefinitely, otherwise times out after the given number of seconds.

Returns:

  • (self)


123
124
125
126
# File 'lib/functions_framework/server.rb', line 123

def wait_until_stopped timeout: nil
  @server&.thread&.join timeout
  self
end