class FunctionsFramework::Registry
Registry
providing lookup of functions by name.
Public Class Methods
Create a new empty registry.
# File lib/functions_framework/registry.rb, line 23 def initialize @mutex = ::Mutex.new @functions = {} @start_tasks = [] end
Public Instance Methods
Look up a function definition by name.
@param name [String] The function name @return [FunctionsFramework::Function] if the function is found @return [nil] if the function is not found
# File lib/functions_framework/registry.rb, line 36 def [] name @mutex.synchronize { @functions[name.to_s] } end
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`](cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event). Any return value is ignored.
@param name [String] The function name @param block [Proc] The function code as a proc @return [self]
# 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 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.
@param name [String] The function name @param block [Proc] The function code as a proc @return [self]
# 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 a startup task.
Startup tasks are generally run just before a server starts. They are passed the {FunctionsFramework::Function} identifying the function to execute, and have no return value.
@param block [Proc] The startup task @return [self]
# 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
Returns the list of defined names
@return [Array<String>]
# File lib/functions_framework/registry.rb, line 45 def names @mutex.synchronize { @functions.keys.sort } end
Return an array of startup tasks.
@return [Array<FunctionsFramework::Function>]
# File lib/functions_framework/registry.rb, line 54 def startup_tasks @mutex.synchronize { @start_tasks.dup } end