Book Image

Python for Google App Engine

By : Massimiliano Pippi
Book Image

Python for Google App Engine

By: Massimiliano Pippi

Overview of this book

Table of Contents (15 chapters)
Python for Google App Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Scheduling tasks with Cron


We have designed the shrink operation as an optional functionality triggered by users, but we could run it at a determined time interval for every user in order to lower the costs of Cloud Storage. App Engine supports the scheduled execution of jobs with the Cron service; every application has a limited number of Cron jobs available, depending on our billing plan. Cron jobs have the same restrictions as tasks in a task queue, so the request can last up to 10 minutes.

We first prepare a request handler that will implement the job:

class ShrinkCronJob(ShrinkHandler):
    def post(self):
        self.abort(405, headers=[('Allow', 'GET')])

    def get(self):
        if 'X-AppEngine-Cron' not in self.request.headers:
            self.error(403)

        notes = Note.query().fetch()
        for note in notes:
            self._shrink_note(note)

We derive the ShrinkCronJob class from the ShrinkHandler class to inherit the _shrink_note() method. The cron service performs...