Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Documentation on scheduling a app_engine_http_request seems incorrect #62

Closed
Nathanmalnoury opened this issue Dec 31, 2020 · 7 comments
Closed
Assignees
Labels
api: cloudtasks Issues related to the googleapis/python-tasks API.

Comments

@Nathanmalnoury
Copy link

Hi,

I'm having an issue creating scheduled Cloud Tasks, using python 2. 7 : they won't work throwing a TypeError.
It's probably worth noting that as long as I dont use the in_seconds every thing work as expected.

Am I missing something ?

Thanks

Environment details

  • OS type and version: Linux / Ubuntu 20.10
  • Python version: 2.7
  • pip version: 20.3.3
  • google-cloud-tasks version: 1.5.0

Steps to reproduce

  1. Follow the code sample to create a task using an app_engine_http_request
  2. Create a task using in_seconds

Code example

-> https://cloud.google.com/tasks/docs/samples/cloud-tasks-appengine-create-task

I also tried different combinations for 'schedule_time' :

task['schedule_time'] = date.isoformat('T')
task['schedule_time'] = ' 2021-01-07T10:20:50.52Z'  # future date, inferior to 30 days from now 

# Using future.backports.datetime
task['schedule_time'] = date.isoformat('T')

Nothing worked.

Stack trace

ERROR    2020-12-31 11:28:56,442 handlers.py:120] Traceback (most recent call last):
  File "/home/nathan/myproject/lib/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/nathan/myproject/lib/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/nathan/myproject/app/main/admin/migrations.py", line 34, in reindex_enterprise
    migrate_by_pieces(400, _in_seconds=5 * 60)
  File "/home/nathan/myproject/app/deferredv3.py", line 70, in wrapper_defer
    path=u'/function-handler/{}'.format(f.__name__))
  File "/home/nathan/myproject/app/deferredv3.py", line 113, in send_deferred_task
    task = client.create_task(parent=parent, task=task)
  File "/home/nathan/myproject/lib/google/cloud/tasks_v2/gapic/cloud_tasks_client.py", line 1492, in create_task
    parent=parent, task=task, response_view=response_view
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Timestamp got datetime.datetime.
@product-auto-label product-auto-label bot added the api: cloudtasks Issues related to the googleapis/python-tasks API. label Dec 31, 2020
@Nathanmalnoury
Copy link
Author

I just tried the same thing using python3.8 and google-cloud-tasks 2.0.0 (using the code example from the documentation), and obtained the same error.

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Jan 1, 2021
@Nathanmalnoury
Copy link
Author

It would seem that this is an issue of documentation, rather than an issue of the API in it-self.
By modifying the snippet found in the doc by the following, I was able to schedule a task as expected:

"""Create a task for a given queue with an arbitrary payload."""

from google.cloud import tasks_v2
from time import time
from google.cloud.tasks_v2.proto.target_pb2 import HttpMethod

# Create a client.
client = tasks_v2.CloudTasksClient()

# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# queue = 'my-appengine-queue'
# location = 'us-central1'
# payload = 'hello'

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

# Construct the request body.
task = {
        'app_engine_http_request': {  # Specify the type of request.
            'http_method': HttpMethod.POST,
            'relative_uri': '/example_task_handler'
        }
}
if payload is not None:
    # The API expects a payload of type bytes.
    converted_payload = payload.encode()

    # Add the payload to the request.
    task['app_engine_http_request']['body'] = converted_payload

if in_seconds is not None:
    # Time of requested execution, in seconds
    seconds = seconds=int(time() + in_seconds)

    # Add the timestamp to the tasks.
    task['schedule_time'] = tasks_v2.types.Timestamp(seconds=seconds)

# Use the client to build and send the task.
response = client.create_task(parent=parent, task=task)

print('Created task {}'.format(response.name))
return response

@Nathanmalnoury Nathanmalnoury changed the title Cannot schedule an app_engine_http_request using schedule_time Documentation on scheduling a app_engine_http_request seems incorrect Jan 4, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Jan 5, 2021
@engelke engelke assigned engelke and busunkim96 and unassigned engelke Jan 22, 2021
@busunkim96 busunkim96 assigned averikitsch and unassigned busunkim96 Jan 22, 2021
@averikitsch
Copy link
Contributor

Please update to the newest library version for the sample to work as is: 2.1.0

@averikitsch
Copy link
Contributor

Please re-open if you are still experiencing this issue.

@grayside grayside removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Jan 25, 2021
@pduggi-cf
Copy link

pduggi-cf commented Jun 4, 2021

Hi,

I am seeing this issue in version 2.3.0

I am running the below

timestamp = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)

task['schedule_time'] = timestamp

client.create_task(parent = parent, task=task)

I get Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Timestamp got datetime.datetime.

@averikitsch
Copy link
Contributor

Hi @pduggi-cf , please see the sample for converting datetime to Timestamp:

d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
# Create Timestamp protobuf.
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(d)

@pduggi-cf
Copy link

The documentation at https://cloud.google.com/tasks/docs/creating-appengine-tasks seems to be the problem

Thanks for pointing me in the right direction ☺️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: cloudtasks Issues related to the googleapis/python-tasks API.
Projects
None yet
Development

No branches or pull requests

7 participants