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, 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.

Parameters:

  • function (FunctionsFramework::Function)

    The function to execute.

  • globals (Hash)

    Globals to pass to invocations. This hash should normally be frozen so separate function invocations cannot interfere with one another's globals.

Yields:



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

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

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

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

Returns:

  • (self)


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
      # Not available on all systems
    end
    @signals_installed = true
  end
  self
end

#running?Boolean

Determine if the web server is currently running

Returns:

  • (Boolean)


138
139
140
# File 'lib/functions_framework/server.rb', line 138

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)


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.

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)


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.

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)


128
129
130
131
# File 'lib/functions_framework/server.rb', line 128

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