{ "info": { "author": "Tyson Holub", "author_email": "tholub@mobiusworks.com", "bugtrack_url": null, "classifiers": [], "description": "# Flask-CronDecorator\n\nSecurely decorates Google Cloud Cron Endpoints via convention and `X-Appengine-Cron` header.\n\nPer [the docs](https://cloud.google.com/appengine/docs/flexible/python/scheduling-jobs-with-cron-yaml#validating_cron_requests):\n\n>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.\n\n## Installation\n\nAdd this line to your application's requirements.txt\n\n```python\nFlask-CronDecorator\n```\n\nAnd then execute:\n\n $ pip install -r requirements.txt\n\nOr install it yourself as:\n\n $ pip install Flask-CronDecorator\n\n## Usage\n\nThe following snippet should get you coding\n```python\nfrom flask import Flask, Blueprint\nfrom CronDecorator import CronDecorator\nimport logging\nfrom datetime import datetime, timedelta\nfrom models import Task, TaskRequest\n\n\napp = Flask(__name__)\napp.cron = CronDecorator(app)\n\n# blueprint can optionally be passed in for registering cron task endpoints in a blueprint\nadmin = Blueprint('admin', __name__, template_folder='templates', url_prefix='/admin')\nblueprint.cron = CronDecorator(app, blueprint)\n\napp.register_blueprint(admin)\n_logger = logging.getLogger(__name__)\n\n\n@admin.cron.task('/purge_tasks', methods=['GET']) # creates /cron/admin/purge_tasks endpoint\ndef purge_tasks():\n _logger.info('Purging first 100 Tasks older than 1 year')\n year_ago = datetime.utcnow() - timedelta(days=365)\n tasks = Task.query.filter(Task.created <= year_ago).order_by(Task.id.asc()).limit(100).all()\n for task in tasks:\n db.session.delete(task)\n\n _logger.info('Purging first 1000 TaskRequests older than 2 weeks')\n two_weeks_ago = datetime.utcnow() - timedelta(days=14)\n task_requests = TaskRequest.query.join(Task).filter(\n models.Task.created <= two_weeks_ago\n ).order_by(Task.id.desc()).limit(1000).all()\n\n for task_request in task_requests:\n db.session.delete(task_request)\n\n db.session.commit()\n\n return '', 200\n```\n\n## Google Cloud Settings\n\nGiven the above snippet, you'll need to update your Google Cloud cron.yaml\n\n```yaml\ncron:\n- description: \"Purges Tasks older than 1 year and TaskRequests older than 2 weeks\"\n url: /cron/admin/purge_tasks\n schedule: every 30 minutes\n```\n\nBe 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.\n\n```yaml\nruntime: python\nruntime_config:\n python_version: 2\nthreadsafe: true\nenv: flex\nhandlers:\n- script: flask.app\n secure: always\n url: /cron/.*\n```\n\n## Deploy\n\nDeploy app and cron.yaml to Google Cloud\n\n $ gcloud app deploy\n $ gcloud app deploy cron.yaml\n\n# Testing\n\n $ pytest -s tests.py", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/MobiusWorksLLC/Flask-CronDecorator.git", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "Flask-CronDecorator", "package_url": "https://pypi.org/project/Flask-CronDecorator/", "platform": "", "project_url": "https://pypi.org/project/Flask-CronDecorator/", "project_urls": { "Homepage": "https://github.com/MobiusWorksLLC/Flask-CronDecorator.git" }, "release_url": "https://pypi.org/project/Flask-CronDecorator/0.0.3/", "requires_dist": null, "requires_python": "", "summary": "Securely decorates Google Cloud Cron Endpoints via convention and X-Appengine-Cron header", "version": "0.0.3" }, "last_serial": 4513591, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "76a4936fb7dffd4929f40db833657a4e", "sha256": "69e933e9fc0e66789b76c735513b431c3539a43c6c72c350a11f569c83364915" }, "downloads": -1, "filename": "Flask-CronDecorator-0.0.1.tar.gz", "has_sig": false, "md5_digest": "76a4936fb7dffd4929f40db833657a4e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2723, "upload_time": "2018-04-27T16:23:41", "url": "https://files.pythonhosted.org/packages/ec/ea/4c5db2041414c44db8aaa877e0cc326793855bec4cd951e6b9f755c072b8/Flask-CronDecorator-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "b9d13ac86812a3ce3f87ef09c6d7e4fc", "sha256": "2f6b2eae717faf4999ecb258f29ffdda60fac01c42017ecfb03e7da90469575b" }, "downloads": -1, "filename": "Flask-CronDecorator-0.0.2.tar.gz", "has_sig": false, "md5_digest": "b9d13ac86812a3ce3f87ef09c6d7e4fc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3191, "upload_time": "2018-05-15T15:23:58", "url": "https://files.pythonhosted.org/packages/70/2b/432e3d6cb569911c0a54b3784a5b2acd80c17d8f224576d9550deaf64db9/Flask-CronDecorator-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "c7fb5c4976dcaccb1b1197223e1d9648", "sha256": "177694682cccf747ccc121775540679f92c954cd600ea662c74c5b5cd6583334" }, "downloads": -1, "filename": "Flask-CronDecorator-0.0.3.tar.gz", "has_sig": false, "md5_digest": "c7fb5c4976dcaccb1b1197223e1d9648", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3185, "upload_time": "2018-11-21T17:23:57", "url": "https://files.pythonhosted.org/packages/fb/9b/ed019aa71c40e276085cd7d735fa52f47de26255a774607f6fc4d0ebe96e/Flask-CronDecorator-0.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c7fb5c4976dcaccb1b1197223e1d9648", "sha256": "177694682cccf747ccc121775540679f92c954cd600ea662c74c5b5cd6583334" }, "downloads": -1, "filename": "Flask-CronDecorator-0.0.3.tar.gz", "has_sig": false, "md5_digest": "c7fb5c4976dcaccb1b1197223e1d9648", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3185, "upload_time": "2018-11-21T17:23:57", "url": "https://files.pythonhosted.org/packages/fb/9b/ed019aa71c40e276085cd7d735fa52f47de26255a774607f6fc4d0ebe96e/Flask-CronDecorator-0.0.3.tar.gz" } ] }