Skip to main content

Securely decorates Google Cloud Cron Endpoints via convention and X-Appengine-Cron header

Project description


Securely decorates Google Cloud Cron Endpoints via convention and X-Appengine-Cron header.

Per the docs:

The X-Appengine-Cron header is set internally by Google App Engine. If your request handler finds this header it can trust that the request is a cron request. The X- headers are stripped by App Engine when they originate from external sources so that you can trust this header.


Add this line to your application's requirements.txt


And then execute:

$ pip install -r requirements.txt

Or install it yourself as:

$ pip install Flask-CronDecorator


The following snippet should get you coding

from flask import Flask, Blueprint
from CronDecorator import CronDecorator
import logging
from datetime import datetime, timedelta
from models import Task, TaskRequest

app = Flask(__name__)
app.cron = CronDecorator(app)

# blueprint can optionally be passed in for registering cron task endpoints in a blueprint
admin = Blueprint('admin', __name__, template_folder='templates', url_prefix='/admin')
blueprint.cron = CronDecorator(app, blueprint)

_logger = logging.getLogger(__name__)

@admin.cron.task('/purge_tasks', methods=['GET'])  # creates /cron/admin/purge_tasks endpoint
def purge_tasks():'Purging first 100 Tasks older than 1 year')
    year_ago = datetime.utcnow() - timedelta(days=365)
    tasks = Task.query.filter(Task.created <= year_ago).order_by(
    for task in tasks:
        db.session.delete(task)'Purging first 1000 TaskRequests older than 2 weeks')
    two_weeks_ago = datetime.utcnow() - timedelta(days=14)
    task_requests = TaskRequest.query.join(Task).filter(
        models.Task.created <= two_weeks_ago

    for task_request in task_requests:


    return '', 200

Google Cloud Settings

Given the above snippet, you'll need to update your Google Cloud cron.yaml

- description: "Purges Tasks older than 1 year and TaskRequests older than 2 weeks"
  url: /cron/admin/purge_tasks
  schedule: every 30 minutes

Be sure your /cron/* endpoints are covered in Google Cloud app.yaml. Note: the handlers:script must be a wsgi path to your flask app instantiation relative to where the process is started, not necessarily where app.yaml lives.

runtime: python
  python_version: 2
threadsafe: true
env: flex
- script:
  secure: always
  url: /cron/.*


Deploy app and cron.yaml to Google Cloud

$ gcloud app deploy
$ gcloud app deploy cron.yaml


$ pytest -s

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-CronDecorator-0.0.3.tar.gz (3.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page