0
votes

Problem

  • I want to get a lot of game data at 9 o'clock every morning. Therefore I use App Engine & cron job. However I would like to add Cloud Tasks, I don't know how to do.

Question

  • How can I execute asynchronous tasks in the background as scheduled in Google Cloud Platform?
  • Which is natural to implement (Cloud Scheduler + Cloud Tasks) or (cron job+ Cloud Tasks)?

Development Environment

  • App Engine Python (Flexible environment).
  • Python 3.6

Best regards,

1

1 Answers

3
votes

Cloud Tasks are asynchronous by design. As you mentioned, the best way would be to pair them with Cloud Scheduler.

First of all, since cloud Scheduler needs either a Pub/Sub or an HTTP endpoint, to call once it runs the jobs, I recommend to you to create an App Engine handler, to which the Cloud Scheduler will call, that creates and sends the task.

You can do so by following this documentation. First of all you will have to create a queue, and afterwards I recommend you to deploy simple application that has a handler to create the tasks. A small example:

from google.cloud import tasks_v2beta3
from flask import Flask, request

app = Flask(__name__)

@app.route('/createTask', methods=['POST'])
def create_task_handler():
    client = tasks_v2beta3.CloudTasksClient()
    parent = client.queue_path('YOUR_PROJECT', 'PROJECT_LOCATION', 'YOUR_QUEUE_NAME')

    task = {
        'app_engine_http_request': {
            'http_method': 'POST',
            'relative_uri': '/handler_to_call'
            }
    }

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

Where the 'relative_uri' is the handler that the task will call, and processes your data.

Once that is done, follow the Cloud Scheduler documentation to create jobs, and specify the target to be App Engine HTTP, set the URL to '/createTask', the service to whichever is handling the URL, and the HTTP method to POST. Fill the rest of parameters as required, and you can set the Frequency to 'every monday 09:00'.