1

I want to schedule a task (ie. a block of code) to be executed at a later time wherein the time and date of execution along with the other parameters are passed in as the task payload.

So we have a Python (Django) based Appengine application where:

The tasks are defined in tasks.py

@csrf_exempt
def task_myfunction(request):
    if request.method == 'POST':
      # Read POST parameters
      # Perform task
    return HttpResponse("Success")

Payload is added to the queue in views.py and the payload is POST-ed to the url provided, which maps to the function defined above.

from google.appengine.api import taskqueue

# ..Somewhere inside a view..
taskqueue.add(queue_name='myqueue', url='/task/myfunction', params={
    # Specify parameters
    })

Is there a way, I could specify the exact time of execution (with timezone) along with the payload data so that the task is automatically picked up from the queue and performed at that time instant.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99

4 Answers4

5

You could calculate the required time then set a countdown or eta on the task when you add it.

countdown:

Minimum time to wait before executing this task, in seconds. Defaults to zero. Do not specify a countdown if you specified an eta.

eta:

Earliest time of task execution, in seconds. It is a datetime.datetime specifying the absolute ETA or None; this may be timezone-aware or timezone-naive. If None, defaults to now. It must be less than 30 days from current date.

https://developers.google.com/appengine/docs/python/taskqueue/tasks

As you can see on the above pages there does not seem to be a way to do exactly what you want re: timezones and a specific time. You'll have to do those calculations I think.

Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99
Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • What does it mean by 'earliest time'? Can the task be executed after the eta? Further it helps to know that `eta` can be specified as a python datetime.datetime object for Push Queues. Nice answer! Thanks. I would reword it a bit to add more details, and some sample code. – Pranjal Mittal Dec 30 '13 at 12:11
  • it means that it will not execute before that time, but will execute at some time after that point. It's not guaranteed that it will execute at the exact moment the ETA is reached. Sample code would only relate to the calculation of the deadline, as it's just an argument you pass to the task creator – Paul Collingwood Dec 30 '13 at 12:14
1

You are looking for the ETA. Btw some code to accompany and explain how you can set a task in the future

taskqueue.add(
            queue_name = "playercommands",
            url="/playercommands/next/",
            method='POST',
            eta=datetime.datetime.now() + datetime.timedelta(0, player.track_duration),


            headers={"X-AppEngine-FailFast":"true"} # for now
        )
Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
0

You could get a timestamp that is timezone insensitive (like a unix timestamp) using python-dateutil

And then you could use appengine backends and use that timezone insensitive timestamp to schedule your task. https://developers.google.com/appengine/docs/python/backends/

Community
  • 1
  • 1
HSquirrel
  • 839
  • 4
  • 16
0

Google has updated this portion of their api (see here sorry, link is for PHP). You can now send in a 3rd parameter with PushTask containing the following options:

  1. 'method': string One of 'POST', 'GET', 'HEAD', 'PUT', 'DELETE'. Default value: 'POST'.
  2. 'name': string Name of the task. Defaults to '' meaning the service will generate a unique task name.
  3. 'delay_seconds': float The minimum time to wait before executing the task. Default: zero.
  4. 'header': string Additional headers to be sent when the task executes.
Joe Bergevin
  • 3,158
  • 5
  • 26
  • 34