-
Notifications
You must be signed in to change notification settings - Fork 117
docs: Update pub/sub quickstart to use background event #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,14 +134,14 @@ response instead. | |
1. Create a `main.py` file with the following contents: | ||
|
||
```python | ||
def hello(request): | ||
return "Hello world!" | ||
def hello(event, context): | ||
print("Received", context.event_id) | ||
di marked this conversation as resolved.
Show resolved
Hide resolved
di marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
1. Start the Functions Framework on port 8080: | ||
|
||
```sh | ||
functions-framework --target=hello --debug --port=8080 | ||
functions-framework --target=hello --signature-type=event --debug --port=8080 | ||
di marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
1. In a second terminal, start the Pub/Sub emulator on port 8085. | ||
|
@@ -172,7 +172,7 @@ response instead. | |
pip install -r requirements.txt | ||
|
||
python publisher.py $PUBSUB_PROJECT_ID create $TOPIC_ID | ||
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8085 | ||
python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8080 | ||
di marked this conversation as resolved.
Show resolved
Hide resolved
di marked this conversation as resolved.
Show resolved
Hide resolved
|
||
python publisher.py $PUBSUB_PROJECT_ID publish $TOPIC_ID | ||
``` | ||
|
||
|
@@ -204,6 +204,41 @@ response instead. | |
Published messages to projects/my-project/topics/my-topic. | ||
``` | ||
|
||
And in the terminal where the Functions Framework is running: | ||
|
||
```none | ||
* Serving Flask app "hello" (lazy loading) | ||
* Environment: production | ||
WARNING: This is a development server. Do not use it in a production deployment. | ||
Use a production WSGI server instead. | ||
* Debug mode: on | ||
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit) | ||
* Restarting with fsevents reloader | ||
* Debugger is active! | ||
* Debugger PIN: 911-794-046 | ||
Received 1 | ||
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 - | ||
Received 2 | ||
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 - | ||
Received 5 | ||
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 - | ||
Received 6 | ||
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 - | ||
Received 7 | ||
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 - | ||
Received 8 | ||
127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 - | ||
Received 9 | ||
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 - | ||
Received 3 | ||
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 - | ||
Received 4 | ||
127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 - | ||
``` | ||
|
||
For more details on extracting data from a Pub/Sub event, see | ||
https://cloud.google.com/functions/docs/tutorials/pubsub#functions_helloworld_pubsub_tutorial-python | ||
|
||
### Quickstart: Build a Deployable Container | ||
|
||
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/). | ||
|
@@ -263,13 +298,13 @@ You can configure the Functions Framework using command-line flags or environmen | |
## Enable Google Cloud Functions Events | ||
|
||
The Functions Framework can unmarshall incoming | ||
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `data` and `context` objects. | ||
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `event` and `context` objects. | ||
These will be passed as arguments to your function when it receives a request. | ||
Note that your function must use the `event`-style function signature: | ||
|
||
```python | ||
def hello(data, context): | ||
print(data) | ||
def hello(event, context): | ||
print(event) | ||
print(context) | ||
``` | ||
Comment on lines
+301
to
309
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually, we refer to the whole event as an The event we refer to usually has two properties, If we want to use the term event, context, I suggest we do so in a separate PR and have a plan to update the docs at the same time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW in most other language samples we use |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,5 @@ | |
# limitations under the License. | ||
|
||
|
||
def hello(data, context): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not update this identifier in this PR. Maybe a separate PR. |
||
def hello(event, context): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would stick to
data, context
to update this Pub/Sub quickstart. See other comments for more details.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1