functions_framework 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +9 -9
- data/bin/functions-framework +1 -1
- data/bin/functions-framework-ruby +1 -1
- data/docs/deploying-functions.md +29 -14
- data/docs/overview.md +4 -4
- data/docs/testing-functions.md +9 -11
- data/docs/writing-functions.md +73 -13
- data/lib/functions_framework.rb +35 -7
- data/lib/functions_framework/cli.rb +97 -22
- data/lib/functions_framework/function.rb +3 -2
- data/lib/functions_framework/legacy_event_converter.rb +28 -29
- data/lib/functions_framework/registry.rb +40 -6
- data/lib/functions_framework/server.rb +14 -10
- data/lib/functions_framework/testing.rb +20 -8
- data/lib/functions_framework/version.rb +1 -1
- metadata +22 -108
- data/lib/functions_framework/cloud_events.rb +0 -45
- data/lib/functions_framework/cloud_events/content_type.rb +0 -216
- data/lib/functions_framework/cloud_events/errors.rb +0 -42
- data/lib/functions_framework/cloud_events/event.rb +0 -84
- data/lib/functions_framework/cloud_events/event/field_interpreter.rb +0 -150
- data/lib/functions_framework/cloud_events/event/v0.rb +0 -236
- data/lib/functions_framework/cloud_events/event/v1.rb +0 -223
- data/lib/functions_framework/cloud_events/http_binding.rb +0 -310
- data/lib/functions_framework/cloud_events/json_format.rb +0 -173
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67b0d5e61bb58f49adefbff9794c960438c1577b4e3d54ee5bf072b134834b71
|
4
|
+
data.tar.gz: 9cb295fd8ba39ea9ad5e8eab1c3055eaa18dddef5a1838cc58d7dcdb2d12fc5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99e58e1511a97dc9e8643a034bc089f5bb4b71d5f842b507151d8728f058a3763b0f55dcd952ac71f212773e6087cb333fcfebb814a33f57cd2057fde09d6272
|
7
|
+
data.tar.gz: e5afdebdc20c069c247e58196d75787a8c3b3e51924b7714851006ade62dc70bce62cd9e42501528103eb41f3ffc2d6239e9dd8c6650798bdbd1693268fa431a
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,31 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
### v0.6.0 / 2020-09-17
|
4
|
+
|
5
|
+
* ADDED: You can use the --version flag to print the framework version
|
6
|
+
* ADDED: You can use the --verify flag to verify that a given function is defined
|
7
|
+
* ADDED: You can now define blocks that are executed at server startup
|
8
|
+
|
9
|
+
### v0.5.2 / 2020-09-06
|
10
|
+
|
11
|
+
* FIXED: Use global $stderr rather than STDERR for logger
|
12
|
+
* DOCS: Fix instructions for deployment to Google Cloud Functions
|
13
|
+
|
14
|
+
### v0.5.1 / 2020-07-20
|
15
|
+
|
16
|
+
* Updated some documentation links. No functional changes.
|
17
|
+
|
18
|
+
### v0.5.0 / 2020-07-09
|
19
|
+
|
20
|
+
* Removed embedded CloudEvents classes and added the official CloudEvents SDK as a dependency. A `FunctionsFramework::CloudEvents` alias provides backward compatibility.
|
21
|
+
|
22
|
+
### v0.4.1 / 2020-07-08
|
23
|
+
|
24
|
+
* Fixed unsupported signal error on Windows.
|
25
|
+
* Fixed several edge case errors in legacy event conversion.
|
26
|
+
* Generated Content-Type headers now properly quote param values if needed.
|
27
|
+
* Minor documentation updates.
|
28
|
+
|
3
29
|
### v0.4.0 / 2020-06-29
|
4
30
|
|
5
31
|
* Dropped the legacy and largely unsupported `:event` function type. All event functions should be of type `:cloud_event`.
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
# Functions Framework [](https://
|
1
|
+
# Functions Framework [](https://googlecloudplatform.github.io/functions-framework-ruby/latest) [](https://badge.fury.io/rb/functions_framework)
|
2
2
|
|
3
3
|
An open source framework for writing lightweight, portable Ruby functions that
|
4
4
|
run in a serverless environment. Functions written to this Framework will run
|
5
5
|
in many different environments, including:
|
6
6
|
|
7
|
-
* [Google Cloud Functions](https://cloud.google.com/functions) *(
|
8
|
-
* [
|
7
|
+
* [Google Cloud Functions](https://cloud.google.com/functions) *(alpha)*
|
8
|
+
* [Google Cloud Run](https://cloud.google.com/run)
|
9
9
|
* Any other [Knative](https://github.com/knative)-based environment
|
10
10
|
* Your local development machine
|
11
11
|
|
@@ -60,7 +60,7 @@ Create a `Gemfile` listing the Functions Framework as a dependency:
|
|
60
60
|
```ruby
|
61
61
|
# Gemfile
|
62
62
|
source "https://rubygems.org"
|
63
|
-
gem "functions_framework", "~> 0.
|
63
|
+
gem "functions_framework", "~> 0.5"
|
64
64
|
```
|
65
65
|
|
66
66
|
Create a file called `app.rb` and include the following code. This defines a
|
@@ -98,26 +98,26 @@ Stop the server with `CTRL+C`.
|
|
98
98
|
|
99
99
|
These guides provide additional getting-started information.
|
100
100
|
|
101
|
-
* **[Writing Functions](https://
|
101
|
+
* **[Writing Functions](https://googlecloudplatform.github.io/functions-framework-ruby/latest/file.writing-functions.html)**
|
102
102
|
: How to write functions that respond to HTTP requests, industry-standard
|
103
103
|
[CloudEvents](https://cloudevents.io), as well as events sent from Google
|
104
104
|
Cloud services such as [Pub/Sub](https://cloud.google.com/pubsub) and
|
105
105
|
[Storage](https://cloud.google.com/storage).
|
106
|
-
* **[Testing Functions](https://
|
106
|
+
* **[Testing Functions](https://googlecloudplatform.github.io/functions-framework-ruby/latest/file.testing-functions.html)**
|
107
107
|
: How to use the testing features of the Functions Framework to write local
|
108
108
|
unit tests for your functions using standard Ruby testing frameworks such
|
109
109
|
as [Minitest](https://github.com/seattlerb/minitest) and
|
110
110
|
[RSpec](https://rspec.info/).
|
111
|
-
* **[Running a Functions Server](https://
|
111
|
+
* **[Running a Functions Server](https://googlecloudplatform.github.io/functions-framework-ruby/latest/file.running-a-functions-server.html)**
|
112
112
|
: How to use the `functions-framework-ruby` executable to run a local
|
113
113
|
functions server.
|
114
|
-
* **[Deploying Functions](https://
|
114
|
+
* **[Deploying Functions](https://googlecloudplatform.github.io/functions-framework-ruby/latest/file.deploying-functions.html)**
|
115
115
|
: How to deploy functions to
|
116
116
|
[Google Cloud Functions](https://cloud.google.com/functions) or
|
117
117
|
[Google Cloud Run](https://cloud.google.com/run).
|
118
118
|
|
119
119
|
The library reference documentation can be found at:
|
120
|
-
https://
|
120
|
+
https://googlecloudplatform.github.io/functions-framework-ruby
|
121
121
|
|
122
122
|
Additional examples are available in the `examples` directory:
|
123
123
|
https://github.com/GoogleCloudPlatform/functions-framework-ruby/blob/master/examples/
|
data/bin/functions-framework
CHANGED
data/docs/deploying-functions.md
CHANGED
@@ -34,7 +34,7 @@ You can run Ruby functions on Google Cloud Functions by selecting the `ruby26`
|
|
34
34
|
runtime. This runtime uses a recent release of Ruby 2.6. Support for other
|
35
35
|
versions of Ruby may be added in the future.
|
36
36
|
|
37
|
-
> **Note:** Ruby support on Cloud Functions is currently in limited
|
37
|
+
> **Note:** Ruby support on Cloud Functions is currently in limited alpha.
|
38
38
|
> It is not yet suitable for production workloads, and support is best-effort
|
39
39
|
> only. Access is currently limited to selected early-access users.
|
40
40
|
|
@@ -46,30 +46,39 @@ is to `bundle install` or `bundle update` and run your local tests prior to
|
|
46
46
|
deploying. Cloud Functions will not accept your function unless an up-to-date
|
47
47
|
`Gemfile.lock` is present.
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
Also, make sure your source file (which defines your function) is called
|
50
|
+
`app.rb`. The Functions Framework lets you choose a function source file, but
|
51
|
+
Cloud Functions currently requires you to use `app.rb`.
|
52
|
+
|
53
|
+
Decide _which_ function in the source file to invoke, that is, the name that you
|
54
|
+
used when writing the function. This is called the **target**. (Note that if you
|
55
|
+
did not specify a name for the function, it defaults to the name `function`.)
|
56
|
+
|
57
|
+
Choose a Cloud Functions **name** for your function. The **name** identifies
|
58
|
+
this function deployment (e.g. in the cloud console) and is also part of the
|
59
|
+
function's default URL. (Note: the **name** and the **target** do not have to
|
60
|
+
be the same value.)
|
53
61
|
|
54
62
|
Then, issue the gcloud command to deploy:
|
55
63
|
|
56
64
|
```sh
|
57
|
-
gcloud functions deploy $YOUR_FUNCTION_NAME
|
58
|
-
|
59
|
-
|
65
|
+
gcloud functions deploy $YOUR_FUNCTION_NAME \
|
66
|
+
--project=$YOUR_PROJECT_ID \
|
67
|
+
--runtime=ruby26 \
|
68
|
+
--trigger-http \
|
69
|
+
--entry-point=$YOUR_FUNCTION_TARGET
|
60
70
|
```
|
61
71
|
|
62
|
-
The
|
63
|
-
|
64
|
-
|
65
|
-
`gcloud config set project`.
|
72
|
+
The `--entry-point=` flag can be omitted if the **target** has the same value
|
73
|
+
as the **name**. Additionally, the `--project` flag can be omitted if you've
|
74
|
+
set your default project using `gcloud config set project`.
|
66
75
|
|
67
76
|
If your function handles events rather than HTTP requests, you'll need to
|
68
77
|
replace `--trigger-http` with a different trigger. For details, see the
|
69
78
|
[reference documentation](https://cloud.google.com/sdk/gcloud/reference/functions/deploy)
|
70
79
|
for `gcloud functions deploy`.
|
71
80
|
|
72
|
-
To update your deployment, just redeploy using the same function name
|
81
|
+
To update your deployment, just redeploy using the same function **name**.
|
73
82
|
|
74
83
|
### Configuring Cloud Functions deployments
|
75
84
|
|
@@ -105,6 +114,12 @@ to adapt it if you have an Anthos installation.
|
|
105
114
|
|
106
115
|
### Building an image for your function
|
107
116
|
|
117
|
+
Before you can deploy to Cloud Run, make sure your bundle, and in
|
118
|
+
particular your `Gemfile.lock` file, is up to date. The easiest way to do this
|
119
|
+
is to `bundle install` or `bundle update` and run your local tests prior to
|
120
|
+
deploying. The configuration used in the Dockerfile below will not accept your
|
121
|
+
function unless an up-to-date `Gemfile.lock` is present.
|
122
|
+
|
108
123
|
First, build a Docker image containing your function. Following is a simple
|
109
124
|
Dockerfile that you can use as a starting point. Feel free to adjust it to the
|
110
125
|
needs of your project:
|
@@ -165,7 +180,7 @@ deployed function.
|
|
165
180
|
|
166
181
|
Note that our Dockerfile's entrypoint did not pass any source file or target
|
167
182
|
name to the Functions Framework. If these are not specified, the Framework will
|
168
|
-
use the source
|
183
|
+
use the source `./app.rb` and the target `function` by default. To use different
|
169
184
|
values, you need to set the appropriate environment variables when deploying, as
|
170
185
|
illustrated above with the `FUNCTION_SOURCE` and `FUNCTION_TARGET` variables.
|
171
186
|
|
data/docs/overview.md
CHANGED
@@ -8,8 +8,8 @@ The Functions Framework is an open source framework for writing lightweight,
|
|
8
8
|
portable Ruby functions that run in a serverless environment. Functions written
|
9
9
|
to this Framework will run in many different environments, including:
|
10
10
|
|
11
|
-
* [Google Cloud Functions](https://cloud.google.com/functions) *(
|
12
|
-
* [
|
11
|
+
* [Google Cloud Functions](https://cloud.google.com/functions) *(alpha)*
|
12
|
+
* [Google Cloud Run](https://cloud.google.com/run)
|
13
13
|
* Any other [Knative](https://github.com/knative)-based environment
|
14
14
|
* Your local development machine
|
15
15
|
|
@@ -64,7 +64,7 @@ Create a `Gemfile` listing the Functions Framework as a dependency:
|
|
64
64
|
```ruby
|
65
65
|
# Gemfile
|
66
66
|
source "https://rubygems.org"
|
67
|
-
gem "functions_framework", "~> 0.
|
67
|
+
gem "functions_framework", "~> 0.5"
|
68
68
|
```
|
69
69
|
|
70
70
|
Create a file called `app.rb` and include the following code. This defines a
|
@@ -121,7 +121,7 @@ These guides provide additional getting-started information.
|
|
121
121
|
[Google Cloud Run](https://cloud.google.com/run).
|
122
122
|
|
123
123
|
The library reference documentation can be found at:
|
124
|
-
https://
|
124
|
+
https://googlecloudplatform.github.io/functions-framework-ruby
|
125
125
|
|
126
126
|
Additional examples are available in the GitHub repository:
|
127
127
|
https://github.com/GoogleCloudPlatform/functions-framework-ruby/blob/master/examples/
|
data/docs/testing-functions.md
CHANGED
@@ -17,9 +17,8 @@ the output. You do not need to set up (or mock) an actual server.
|
|
17
17
|
The Functions Framework provides utility methods that streamline the process of
|
18
18
|
setting up functions and the environment for testing, constructing input
|
19
19
|
parameters, and interpreting results. These are available in the
|
20
|
-
|
21
|
-
|
22
|
-
describe block.
|
20
|
+
{FunctionsFramework::Testing} module. Generally, you can include this module in
|
21
|
+
your Minitest test class or RSpec describe block.
|
23
22
|
|
24
23
|
```ruby
|
25
24
|
require "minitest/autorun"
|
@@ -45,10 +44,10 @@ end
|
|
45
44
|
|
46
45
|
To test a function, you'll need to load the Ruby file that defines the function,
|
47
46
|
and run the function to test its results. The Testing module provides a method
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
{FunctionsFramework::Testing#load_temporary}, which loads a Ruby file, defining
|
48
|
+
functions but only for the scope of your test. This allows your test to coexist
|
49
|
+
with tests for other functions, even functions with the same name from a
|
50
|
+
different Ruby file.
|
52
51
|
|
53
52
|
```ruby
|
54
53
|
require "minitest/autorun"
|
@@ -91,8 +90,8 @@ includes helper methods that you can use to create simple requests for many
|
|
91
90
|
basic cases.
|
92
91
|
|
93
92
|
When you have constructed an input request, use
|
94
|
-
|
95
|
-
|
93
|
+
{FunctionsFramework::Testing#call_http} to call a named function, passing the
|
94
|
+
request object. This method returns a
|
96
95
|
[Rack::Response](https://rubydoc.info/gems/rack/Rack/Response) that you can
|
97
96
|
assert against.
|
98
97
|
|
@@ -142,8 +141,7 @@ end
|
|
142
141
|
|
143
142
|
Testing a CloudEvent function works similarly. The `Testing` module provides
|
144
143
|
methods to help construct example CloudEvent objects, which can then be passed
|
145
|
-
to the method
|
146
|
-
[call_event](https://rubydoc.info/gems/functions_framework/FunctionsFramework/Testing#call_event-instance_method).
|
144
|
+
to the method {FunctionsFramework::Testing#call_event}.
|
147
145
|
|
148
146
|
Unlike HTTP functions, event functions do not have a return value. Instead, you
|
149
147
|
will need to test side effects. A common approach is to test logs by capturing
|
data/docs/writing-functions.md
CHANGED
@@ -110,9 +110,8 @@ It is easy to connect an HTTP function to a Sinatra app. First, declare the
|
|
110
110
|
dependency on Sinatra in your `Gemfile`:
|
111
111
|
|
112
112
|
```ruby
|
113
|
-
# Gemfile
|
114
113
|
source "https://rubygems.org"
|
115
|
-
gem "functions_framework", "~> 0.
|
114
|
+
gem "functions_framework", "~> 0.5"
|
116
115
|
gem "sinatra", "~> 2.0"
|
117
116
|
```
|
118
117
|
|
@@ -158,9 +157,9 @@ end
|
|
158
157
|
```
|
159
158
|
|
160
159
|
The event parameter will be either a
|
161
|
-
[CloudEvents V0.3 Event](https://
|
160
|
+
[CloudEvents V0.3 Event](https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V0)
|
162
161
|
object ([see spec](https://github.com/cloudevents/spec/blob/v0.3/spec.md)) or a
|
163
|
-
[CloudEvents V1.0 Event](https://
|
162
|
+
[CloudEvents V1.0 Event](https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1)
|
164
163
|
object ([see spec](https://github.com/cloudevents/spec/blob/v1.0/spec.md)).
|
165
164
|
|
166
165
|
Some Google Cloud services send events in a legacy event format that was defined
|
@@ -198,6 +197,68 @@ FunctionsFramework.http "error_reporter" do |request|
|
|
198
197
|
end
|
199
198
|
```
|
200
199
|
|
200
|
+
## Shared resources
|
201
|
+
|
202
|
+
Generally, functions should be self-contained and stateless, and should not use
|
203
|
+
or share any global state in the Ruby VM. This is because serverless runtimes
|
204
|
+
may spin up or terminate instances of your app at any time, and because a
|
205
|
+
single instance may be running multiple functions at a time in separate threads.
|
206
|
+
|
207
|
+
However, it is sometimes useful to share a resource across multiple function
|
208
|
+
invocations that run on the same Ruby instance. For example, you might establish
|
209
|
+
a single connection to a remote database or other service, and share it across
|
210
|
+
function invocations to avoid incurring the overhead of re-establishing it
|
211
|
+
for every function invocation.
|
212
|
+
|
213
|
+
When using a shared resource, it is important to keep three things in mind:
|
214
|
+
|
215
|
+
1. **The shared resource should be thread-safe.** This is because serverless
|
216
|
+
runtimes such as Google Cloud Functions may run multiple functions at a time
|
217
|
+
in separate threads.
|
218
|
+
|
219
|
+
2. **Use `FunctionsFramework.on_startup` to initialize shared resources.**
|
220
|
+
Do not initialize a shared resource at the top level of your app. This is
|
221
|
+
because serverless runtimes may load your files (and thus execute any Ruby
|
222
|
+
code at the top level) in a build/deployment environment that may not be
|
223
|
+
equipped to support the resource. Instead, initialize resources in a
|
224
|
+
`FunctionsFramework.on_startup` block, which the Functions Framework will
|
225
|
+
call only just before starting a server.
|
226
|
+
|
227
|
+
For example:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
require "functions_framework"
|
231
|
+
|
232
|
+
# This local variable is lexically shared among all blocks.
|
233
|
+
storage_client = nil
|
234
|
+
|
235
|
+
# Do not create the storage client here. This may run during deployment
|
236
|
+
# when, e.g., your storage credentials are not accessible.
|
237
|
+
# require "google/cloud/storage"
|
238
|
+
# storage_client = Google::Cloud::Storage.new # <- may fail
|
239
|
+
|
240
|
+
# Use an on_startup block to initialize the shared client.
|
241
|
+
# This block runs only when the framework is starting an actual server,
|
242
|
+
# and is guaranteed to complete before any functions are executed.
|
243
|
+
FunctionsFramework.on_startup do
|
244
|
+
require "google/cloud/storage"
|
245
|
+
storage_client = Google::Cloud::Storage.new
|
246
|
+
end
|
247
|
+
|
248
|
+
# The storage_client is shared among all function invocations
|
249
|
+
FunctionsFramework.http "storage_example" do |request|
|
250
|
+
bucket = storage_client.bucket "my-bucket"
|
251
|
+
file = bucket.file "path/to/my-file.txt"
|
252
|
+
file.download.to_s
|
253
|
+
end
|
254
|
+
```
|
255
|
+
|
256
|
+
3. **There is no guaranteed cleanup hook.** The Functions Framework does not
|
257
|
+
provide a guaranteed way to register a cleanup task. You can register a
|
258
|
+
`Kernel.at_exit` task, but remember that it is possible for the Ruby VM to
|
259
|
+
terminate without calling it. It is strongly recommended that you use
|
260
|
+
resources that do not require "cleanup".
|
261
|
+
|
201
262
|
## Structuring a project
|
202
263
|
|
203
264
|
A Functions Framework based "project" or "application" is a typical Ruby
|
@@ -207,15 +268,14 @@ needed by the function. It must include at least one Ruby source file that
|
|
207
268
|
defines functions, and can also include additional Ruby files defining classes
|
208
269
|
and methods that assist in the function implementation.
|
209
270
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
any name. Projects can also have multiple source files that apply to different
|
216
|
-
cases.
|
271
|
+
By convention, the "main" Ruby file that defines functions should be called
|
272
|
+
`app.rb` and be located at the root of the project. The path to this file is
|
273
|
+
sometimes known as the **function source**. The Functions Framework allows you
|
274
|
+
to specify an arbitrary source, but suome hosting environments (such as Google
|
275
|
+
Cloud Functions) require it to be `./app.rb`.
|
217
276
|
|
218
|
-
A
|
277
|
+
A source file can define any number of functions (with distinct names). Each of
|
278
|
+
the names is known as a **function target**.
|
219
279
|
|
220
280
|
```
|
221
281
|
(project directory)
|
@@ -236,7 +296,7 @@ A simple project might look like this:
|
|
236
296
|
```ruby
|
237
297
|
# Gemfile
|
238
298
|
source "https://rubygems.org"
|
239
|
-
gem "functions_framework", "~> 0.
|
299
|
+
gem "functions_framework", "~> 0.5"
|
240
300
|
```
|
241
301
|
|
242
302
|
```ruby
|
data/lib/functions_framework.rb
CHANGED
@@ -14,7 +14,8 @@
|
|
14
14
|
|
15
15
|
require "logger"
|
16
16
|
|
17
|
-
require "
|
17
|
+
require "cloud_events"
|
18
|
+
|
18
19
|
require "functions_framework/function"
|
19
20
|
require "functions_framework/legacy_event_converter"
|
20
21
|
require "functions_framework/registry"
|
@@ -44,10 +45,6 @@ require "functions_framework/version"
|
|
44
45
|
#
|
45
46
|
# Here is a roadmap to the internal modules in the Ruby functions framework.
|
46
47
|
#
|
47
|
-
# * {FunctionsFramework::CloudEvents} provides an implementation of the
|
48
|
-
# [CloudEvents](https://cloudevents.io) specification. In particular, if
|
49
|
-
# you define an event function, you will receive the event as a
|
50
|
-
# {FunctionsFramework::CloudEvents::Event} object.
|
51
48
|
# * {FunctionsFramework::CLI} is the implementation of the
|
52
49
|
# `functions-framework-ruby` executable. Most apps will not need to interact
|
53
50
|
# with this class directly.
|
@@ -74,7 +71,7 @@ require "functions_framework/version"
|
|
74
71
|
#
|
75
72
|
module FunctionsFramework
|
76
73
|
@global_registry = Registry.new
|
77
|
-
@logger = ::Logger.new
|
74
|
+
@logger = ::Logger.new $stderr
|
78
75
|
@logger.level = ::Logger::INFO
|
79
76
|
|
80
77
|
##
|
@@ -94,6 +91,12 @@ module FunctionsFramework
|
|
94
91
|
#
|
95
92
|
DEFAULT_SOURCE = "./app.rb".freeze
|
96
93
|
|
94
|
+
##
|
95
|
+
# The CloudEvents implementation was extracted to become the official
|
96
|
+
# CloudEvents SDK. This alias is left here for backward compatibility.
|
97
|
+
#
|
98
|
+
CloudEvents = ::CloudEvents
|
99
|
+
|
97
100
|
class << self
|
98
101
|
##
|
99
102
|
# The "global" registry that holds events defined by the
|
@@ -144,7 +147,8 @@ module FunctionsFramework
|
|
144
147
|
#
|
145
148
|
# You must provide a name for the function, and a block that implemets the
|
146
149
|
# function. The block should take one argument: the event object of type
|
147
|
-
#
|
150
|
+
# [`CloudEvents::Event`](https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event).
|
151
|
+
# Any return value is ignored.
|
148
152
|
#
|
149
153
|
# ## Example
|
150
154
|
#
|
@@ -161,6 +165,29 @@ module FunctionsFramework
|
|
161
165
|
self
|
162
166
|
end
|
163
167
|
|
168
|
+
##
|
169
|
+
# Define a server startup task. This is useful for initializing shared
|
170
|
+
# resources that should be accessible across all function invocations in
|
171
|
+
# this Ruby VM.
|
172
|
+
#
|
173
|
+
# Startup tasks are run just before a server starts. All startup tasks are
|
174
|
+
# guaranteed to complete before any function executes. However, they are
|
175
|
+
# run only when preparing to run functions. They are not run, for example,
|
176
|
+
# if an app is loaded to verify its integrity during deployment.
|
177
|
+
#
|
178
|
+
# Startup tasks are passed two arguments: the {FunctionsFramework::Function}
|
179
|
+
# identifying the function to execute, and the
|
180
|
+
# {FunctionsFramework::Server::Config} specifying the (frozen) server
|
181
|
+
# configuration. Tasks have no return value.
|
182
|
+
#
|
183
|
+
# @param block [Proc] The startup task
|
184
|
+
# @return [self]
|
185
|
+
#
|
186
|
+
def on_startup &block
|
187
|
+
global_registry.add_startup_task(&block)
|
188
|
+
self
|
189
|
+
end
|
190
|
+
|
164
191
|
##
|
165
192
|
# Start the functions framework server in the background. The server will
|
166
193
|
# look up the given target function name in the global registry.
|
@@ -180,6 +207,7 @@ module FunctionsFramework
|
|
180
207
|
raise ::ArgumentError, "Undefined function: #{target.inspect}" if function.nil?
|
181
208
|
end
|
182
209
|
server = Server.new function, &block
|
210
|
+
global_registry.run_startup_tasks server
|
183
211
|
server.respond_to_signals
|
184
212
|
server.start
|
185
213
|
end
|