Class: FunctionsFramework::Registry

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/functions_framework/registry.rb

Overview

Registry providing lookup of functions by name.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Create a new empty registry.



27
28
29
30
31
# File 'lib/functions_framework/registry.rb', line 27

def initialize
  @mutex = ::Monitor.new
  @functions = {}
  @start_tasks = []
end

Instance Method Details

#[](name) ⇒ FunctionsFramework::Function?

Look up a function definition by name.

Parameters:

  • name (String)

    The function name

Returns:



40
41
42
# File 'lib/functions_framework/registry.rb', line 40

def [] name
  @mutex.synchronize { @functions[name.to_s] }
end

#add_cloud_event(name, &block) ⇒ self

Add a CloudEvent function to the registry.

You must provide a name for the function, and a block that implemets the function. The block should take one argument: the event object of type CloudEvents::Event. Any return value is ignored.

Parameters:

  • name (String)

    The function name

  • block (Proc)

    The function code as a proc

Returns:

  • (self)


105
106
107
108
109
110
111
112
# File 'lib/functions_framework/registry.rb', line 105

def add_cloud_event name, &block
  name = name.to_s
  @mutex.synchronize do
    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
    @functions[name] = Function.new name, :cloud_event, &block
  end
  self
end

#add_http(name, &block) ⇒ self

Add an HTTP function to the registry.

You must provide a name for the function, and a block that implemets the function. The block should take a single Rack::Request argument. It should return one of the following:

  • A standard 3-element Rack response array. See https://github.com/rack/rack/blob/master/SPEC
  • A Rack::Response object.
  • A simple String that will be sent as the response body.
  • A Hash object that will be encoded as JSON and sent as the response body.

Parameters:

  • name (String)

    The function name

  • block (Proc)

    The function code as a proc

Returns:

  • (self)


84
85
86
87
88
89
90
91
# File 'lib/functions_framework/registry.rb', line 84

def add_http name, &block
  name = name.to_s
  @mutex.synchronize do
    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
    @functions[name] = Function.new name, :http, &block
  end
  self
end

#add_startup_task(&block) ⇒ self

Add a startup task.

Startup tasks are generally run just before a server starts. They are passed two arguments: the Function identifying the function to execute, and the Server::Config specifying the (frozen) server configuration. Tasks have no return value.

Parameters:

  • block (Proc)

    The startup task

Returns:

  • (self)


125
126
127
128
129
130
# File 'lib/functions_framework/registry.rb', line 125

def add_startup_task &block
  @mutex.synchronize do
    @start_tasks << block
  end
  self
end

#namesArray<String>

Returns the list of defined names

Returns:

  • (Array<String>)


49
50
51
# File 'lib/functions_framework/registry.rb', line 49

def names
  @mutex.synchronize { @functions.keys.sort }
end

#run_startup_tasks(server) ⇒ self

Run all startup tasks.

Parameters:

Returns:

  • (self)


59
60
61
62
63
64
65
# File 'lib/functions_framework/registry.rb', line 59

def run_startup_tasks server
  tasks = @mutex.synchronize { @start_tasks.dup }
  tasks.each do |task|
    task.call server.function, server.config
  end
  self
end