Skip to content

Commit

Permalink
Merge pull request #1137 from andrewsg/tasks
Browse files Browse the repository at this point in the history
Update Task Queue samples for beta
  • Loading branch information
andrewsg authored Sep 25, 2017
2 parents b94b7f4 + aa6c226 commit e1f6883
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
17 changes: 5 additions & 12 deletions appengine/flexible/tasks/create_app_engine_queue_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import datetime
import json

from googleapiclient import discovery


def seconds_from_now_to_rfc3339_datetime(seconds):
"""Return an RFC 3339 datetime string for a number of seconds from now."""
Expand All @@ -31,16 +29,15 @@ def seconds_from_now_to_rfc3339_datetime(seconds):
def create_task(project, queue, location, payload=None, in_seconds=None):
"""Create a task for a given queue with an arbitrary payload."""

import googleapiclient.discovery

# Create a client.
DISCOVERY_URL = (
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
client = discovery.build(
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')

url = '/log_payload'
body = {
'task': {
'app_engine_task_target': {
'app_engine_http_request': {
'http_method': 'POST',
'relative_url': url
}
Expand All @@ -50,7 +47,7 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
if payload is not None:
# Payload is a string (unicode), and must be encoded for base64.
# The finished request body is JSON, which requires unicode.
body['task']['app_engine_task_target']['payload'] = base64.b64encode(
body['task']['app_engine_http_request']['payload'] = base64.b64encode(
payload.encode()).decode()

if in_seconds is not None:
Expand All @@ -65,10 +62,6 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
response = client.projects().locations().queues().tasks().create(
parent=queue_name, body=body).execute()

# By default CreateTaskRequest.responseView is BASIC, so not all
# information is retrieved by default because some data, such as payloads,
# might be desirable to return only when needed because of its large size
# or because of the sensitivity of data that it contains.
print('Created task {}'.format(response['name']))
return response

Expand Down
8 changes: 3 additions & 5 deletions appengine/flexible/tasks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

"""App Engine app to serve as an endpoint for App Engine queue samples."""

import logging

from flask import Flask, request

app = Flask(__name__)
Expand All @@ -24,9 +22,9 @@
@app.route('/log_payload', methods=['POST'])
def log_payload():
"""Log the request payload."""
payload = request.data or "empty payload"
logging.warn(payload)
return 'Logged request payload: {}'.format(payload)
payload = request.get_data(as_text=True) or '(empty payload)'
print('Received task with payload: {}'.format(payload))
return 'Printed task payload: {}'.format(payload)


@app.route('/')
Expand Down
28 changes: 13 additions & 15 deletions tasks/pull_queue_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@
import argparse
import base64

from googleapiclient import discovery


def create_task(project, queue, location):
"""Create a task for a given queue with an arbitrary payload."""

DISCOVERY_URL = (
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
client = discovery.build(
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
import googleapiclient.discovery

# Create a client.
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')

payload = 'a message for the recipient'
task = {
'task': {
'pull_task_target': {
'pull_message': {
'payload': base64.b64encode(payload.encode()).decode()
}
}
Expand All @@ -56,10 +54,10 @@ def create_task(project, queue, location):
def pull_task(project, queue, location):
"""Pull a single task from a given queue and lease it for 10 minutes."""

DISCOVERY_URL = (
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
client = discovery.build(
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
import googleapiclient.discovery

# Create a client.
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')

duration_seconds = '600s'
pull_options = {
Expand All @@ -81,10 +79,10 @@ def pull_task(project, queue, location):
def acknowledge_task(task):
"""Acknowledge a given task."""

DISCOVERY_URL = (
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
client = discovery.build(
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
import googleapiclient.discovery

# Create a client.
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')

body = {'scheduleTime': task['scheduleTime']}
client.projects().locations().queues().tasks().acknowledge(
Expand Down

0 comments on commit e1f6883

Please sign in to comment.