Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 014d40b

Browse filesBrowse files
authored
[Cloud Tasks] Add Http Push Queue Sample (GoogleCloudPlatform#2046)
* Http push queue sample * Updates to requirements and license * update comments * Update app.yaml * update readme * fix versioning errors * linting error fixed
1 parent 87a29ae commit 014d40b
Copy full SHA for 014d40b

File tree

Expand file treeCollapse file tree

9 files changed

+202
-24
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+202
-24
lines changed

‎appengine/flexible/tasks/README.md

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/README.md
+26-13Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Google Cloud Tasks App Engine Queue Samples
1+
# Google Cloud Tasks Samples
22

33
[![Open in Cloud Shell][shell_img]][shell_link]
44

55
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
66
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=appengine/flexible/tasks/README.md
77

8-
Sample command-line program for interacting with the Cloud Tasks API
9-
using App Engine queues.
8+
Sample command-line programs for interacting with the Cloud Tasks API
9+
.
1010

1111
App Engine queues push tasks to an App Engine HTTP target. This directory
1212
contains both the App Engine app to deploy, as well as the snippets to run
@@ -45,9 +45,14 @@ version unless configured to do otherwise.
4545

4646
Deploy the App Engine app with gcloud:
4747

48-
```
49-
gcloud app deploy
50-
```
48+
* To deploy to the Standard environment:
49+
```
50+
gcloud app deploy app.yaml
51+
```
52+
* To deploy to the Flexible environment:
53+
```
54+
gcloud app deploy app.flexible.yaml
55+
```
5156

5257
Verify the index page is serving:
5358

@@ -89,23 +94,31 @@ location is "us-central1").
8994
```
9095
export LOCATION_ID=us-central1
9196
```
92-
93-
Running the sample will create a task, targeted at the 'example_task_handler'
97+
### Using App Engine Queues
98+
Running the sample will create a task, targeted at the `/example_task_handler`
9499
endpoint, with a payload specified:
95100

96101
```
97102
python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello
98103
```
99104

100-
Now view that the payload was received and verify the payload:
105+
### Using HTTP Push Queues
101106

107+
Set an environment variable for the endpoint to your task handler. This is an
108+
example url to send requests to the App Engine task handler:
102109
```
103-
gcloud app logs read
110+
export URL=https://<project_id>.appspot.com/example_task_handler
111+
```
112+
113+
Running the sample will create a task and send the task to the specific URL
114+
endpoint, with a payload specified:
115+
116+
```
117+
python create_http_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --url=$URL --payload=hello
104118
```
105119

106-
Create a task that will be scheduled for a time in the future using the
107-
`--in_seconds` flag:
120+
Now view that the payload was received and verify the payload:
108121

109122
```
110-
python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello --in_seconds=30
123+
gcloud app logs read
111124
```
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: python
16+
env: flex
17+
entrypoint: gunicorn -b :$PORT --threads=4 main:app
18+
19+
runtime_config:
20+
python_version: 3

‎appengine/flexible/tasks/app.yaml

Copy file name to clipboard
+2-7Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google Inc. All Rights Reserved.
1+
# Copyright 2019 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,9 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
runtime: python
16-
env: flex
17-
entrypoint: gunicorn -b :$PORT main:app
18-
19-
runtime_config:
20-
python_version: 3
15+
runtime: python37

‎appengine/flexible/tasks/create_app_engine_queue_task.py

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/create_app_engine_queue_task.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google Inc. All Rights Reserved.
1+
# Copyright 2019 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

‎appengine/flexible/tasks/create_app_engine_queue_task_test.py

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/create_app_engine_queue_task_test.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google Inc. All Rights Reserved.
1+
# Copyright 2019 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
+122Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
import argparse
18+
import datetime
19+
20+
21+
def create_http_task(project,
22+
queue,
23+
location,
24+
url,
25+
payload=None,
26+
in_seconds=None):
27+
# [START cloud_tasks_create_http_task]
28+
"""Create a task for a given queue with an arbitrary payload."""
29+
30+
from google.cloud import tasks_v2beta3
31+
from google.protobuf import timestamp_pb2
32+
33+
# Create a client.
34+
client = tasks_v2beta3.CloudTasksClient()
35+
36+
# TODO(developer): Uncomment these lines and replace with your values.
37+
# project = 'my-project-id'
38+
# queue = 'my-appengine-queue'
39+
# location = 'us-central1'
40+
# url = 'https://<project-id>.appspot.com/example_task_handler'
41+
# payload = 'hello'
42+
43+
# Construct the fully qualified queue name.
44+
parent = client.queue_path(project, location, queue)
45+
46+
# Construct the request body.
47+
task = {
48+
'http_request': { # Specify the type of request.
49+
'http_method': 'POST',
50+
'url': url # The full url path that the task will be sent to.
51+
}
52+
}
53+
if payload is not None:
54+
# The API expects a payload of type bytes.
55+
converted_payload = payload.encode()
56+
57+
# Add the payload to the request.
58+
task['http_request']['body'] = converted_payload
59+
60+
if in_seconds is not None:
61+
# Convert "seconds from now" into an rfc3339 datetime string.
62+
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
63+
64+
# Create Timestamp protobuf.
65+
timestamp = timestamp_pb2.Timestamp()
66+
timestamp.FromDatetime(d)
67+
68+
# Add the timestamp to the tasks.
69+
task['schedule_time'] = timestamp
70+
71+
# Use the client to build and send the task.
72+
response = client.create_task(parent, task)
73+
74+
print('Created task {}'.format(response.name))
75+
return response
76+
# [END cloud_tasks_create_http_task]
77+
78+
79+
if __name__ == '__main__':
80+
parser = argparse.ArgumentParser(
81+
description=create_http_task.__doc__,
82+
formatter_class=argparse.RawDescriptionHelpFormatter)
83+
84+
parser.add_argument(
85+
'--project',
86+
help='Project of the queue to add the task to.',
87+
required=True,
88+
)
89+
90+
parser.add_argument(
91+
'--queue',
92+
help='ID (short name) of the queue to add the task to.',
93+
required=True,
94+
)
95+
96+
parser.add_argument(
97+
'--location',
98+
help='Location of the queue to add the task to.',
99+
required=True,
100+
)
101+
102+
parser.add_argument(
103+
'--url',
104+
help='The full url path that the request will be sent to.',
105+
required=True,
106+
)
107+
108+
parser.add_argument(
109+
'--payload',
110+
help='Optional payload to attach to the push queue.'
111+
)
112+
113+
parser.add_argument(
114+
'--in_seconds', type=int,
115+
help='The number of seconds from now to schedule task attempt.'
116+
)
117+
118+
args = parser.parse_args()
119+
120+
create_http_task(
121+
args.project, args.queue, args.location, args.url,
122+
args.payload, args.in_seconds)
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import create_http_task
18+
19+
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
20+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
21+
TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-appengine-queue')
22+
23+
24+
def test_create_task():
25+
url = 'https://' + TEST_PROJECT_ID + '.appspot.com/example_task_handler'
26+
result = create_http_task.create_http_task(
27+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url)
28+
assert TEST_QUEUE_NAME in result.name

‎appengine/flexible/tasks/main.py

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/main.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google Inc.
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

‎appengine/flexible/tasks/main_test.py

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/main_test.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google Inc. All Rights Reserved.
1+
# Copyright 2019 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.