Class: FunctionsFramework::Registry

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



23
24
25
26
27
# File 'lib/functions_framework/registry.rb', line 23

def initialize
  @mutex = ::Mutex.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:



36
37
38
# File 'lib/functions_framework/registry.rb', line 36

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)


96
97
98
99
100
101
102
103
# File 'lib/functions_framework/registry.rb', line 96

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.cloud_event name, &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)


75
76
77
78
79
80
81
82
# File 'lib/functions_framework/registry.rb', line 75

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.http name, &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 the Function identifying the function to execute, and have no return value.

Parameters:

  • block (Proc)

    The startup task

Returns:

  • (self)


115
116
117
118
119
120
# File 'lib/functions_framework/registry.rb', line 115

def add_startup_task &block
  @mutex.synchronize do
    @start_tasks << Function.startup_task(&block)
  end
  self
end

#namesArray<String>

Returns the list of defined names

Returns:

  • (Array<String>)


45
46
47
# File 'lib/functions_framework/registry.rb', line 45

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

#startup_tasksArray<FunctionsFramework::Function>

Return an array of startup tasks.

Returns:



54
55
56
# File 'lib/functions_framework/registry.rb', line 54

def startup_tasks
  @mutex.synchronize { @start_tasks.dup }
end