GAE Python - How to set a cron job to launch a backend task

青春壹個敷衍的年華 提交于 2019-12-01 01:09:12

Depends if you want a persistent or dynamic backend

For a dynamic one

The plan is:

  1. A cron fires at specific time.

  2. Adds a task on a queue that will start the backend

  3. The backend starts

Example:

app.yaml:

- url: /crons/startgooglepluscrawler/
  script: crons.startgooglepluscrawler.app
  login: admin

backends.yaml:

backends: 
- name: google-plus-crawler
  class: B2
  start: backends.googlepluscrawler.app
  options: dynamic, failfast
  instances: 1

crons.yaml:

cron:
- description: get daily google plus user followers and followings
  url: /crons/startgooglepluscrawler/
  schedule: every day 09:00

queue.yaml:

total_storage_limit: 10M
queue:
- name: google-plus-daily-crawling
  rate: 1/s
  retry_parameters:
    task_retry_limit: 0
    task_age_limit: 1s

On the startgooglepluscrawler.app you need to start the backend with a taskqueue:

class StartGooglePlusCrawlerHandler(webapp2.RequestHandler):

    def get(self):
        logging.info("Running daily Cron")
        taskqueue.add(queue_name = "google-plus-daily-crawling",
                    url="/_ah/start",
                    method='GET',
                    target=(None if self.is_dev_server() else 'google-plus-crawler'),
                    headers={"X-AppEngine-FailFast":"true"}
                    )
        logging.info("Daily Cron finished")

    def is_dev_server(self):
        return os.environ['SERVER_SOFTWARE'].startswith('Dev')


app = webapp2.WSGIApplication([
        ("/crons/startgooglepluscrawler/",StartGooglePlusCrawlerHandler)

    ],debug=True)

And at the backends/googlepluscrawler.py just normally like an app, and a handler to /_ah/start:

app = webapp2.WSGIApplication(
            [('/_ah/start', StartHandler)],
            debug=True,
            config=config.config)

The above example will fire up the backend instance.

An easier way to do this is by migrating the app to modules. Explained here: https://developers.google.com/appengine/docs/python/modules/

After doing so, you can just add following line in the cron.yaml:

target: yourmodule

This allows the cron job to run on the instance defined in yourmodule.yaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!