Skip to main content

cherrypy server for Flask + task scheduler and monitor

Project description

Cherrypy prod server for Flask + parallel task scheduler

Python 3.6 license pytest

Installation

pip install flask_production

Usage example

CherryFlask

Cherrypy server on top of Flask app

CherryFlask(app, scheduler=None, silent=False)

Parameters:

  • app (Flask): Flask application

  • scheduler (TaskScheduler): task scheduler to run in parallel with Flask app

  • silent (bool): don’t print logs
    • default False

from flask import Flask
from flask_production import CherryFlask

app = Flask(__name__)
...

cherry = CherryFlask(app)
cherry.run(host="0.0.0.0", port=8080, threads=5, debug=False)

TaskScheduler

Main class to setup, run and manage jobs
TaskScheduler(check_interval=5,
   holidays_calendar=None,
   tzname=None,
   on_job_error=None,
   log_filepath=None,
   log_maxsize=5*1024*1024, # 5 MB
   log_backups=1,
   startup_grace_mins=0, # minutes
   persist_states=True)

Parameters:

  • check_interval (int): how often to check for pending jobs
    • default 5 seconds

  • holidays_calendar (holidays.HolidayBase): calendar to use for intervals like businessday
    • default US holidays

  • tzname (str): name of timezone as supported by dateutil.tz

  • on_job_error (func(e)): function to call if any job fails

  • log_filepath (path): file to write logs to

  • log_maxsize (int): byte limit per log file
    • default 5 mb (only effective if log_filepath is provided)

  • log_backups (int): number of backups of logs to retain
    • default 1 (only effective if log_filepath is provided)

  • startup_grace_mins (int): grace period for tasks in case a schedule was missed because of app restart
    • default 0

  • persist_states (bool): store job logs on disk so that they can be read back on app restart
    • default True (logs will be stored in a unique data directory)

from flask_production import TaskScheduler

sched = TaskScheduler(check_interval=2)

# Run every minute
sched.every(60).do(foo)

# Run on end of every month (with strict_date False)
sched.every("31st").strict_date(False).at("08:00").do(foo)

# Run every weekday
sched.every("weekday").at("08:00").do(lambda:bar())
sched.every("weekday").at("08:00").timezone("Europe/London").do(lambda:bar())

# catch() will run on job error
example_job = sched.every("weekday").at("09:00").do(lambda:failing()).catch(lambda e: print(e))

# access job information and status as dict
print(example_job.to_dict())
print(sched.jobs[-1].to_dict()) # same job

sched.start() # starts the task scheduler and blocks

Instead of sched.start(), TaskScheduler can be run in parallel with a Flask application using CherryFlask

from flask import Flask
from flask_production import TaskScheduler, CherryFlask

app = Flask(__name__)
...

sched = TaskScheduler()
...

cherry = CherryFlask(app, scheduler=sched)
cherry.run(host="0.0.0.0", port=8080, threads=5, debug=False)

TaskMonitor

The TaskScheduler exposes a list of Job objects through the .jobs attribute
Job information and logs from the last execution are available using the .to_dict() method
TaskMonitor uses these features to provide a web interface to view and rerun tasks
TaskMonitor(
   app,
   sched,
   display_name=None,
   endpoint="@taskmonitor",
   homepage_refresh=30,
   taskpage_refresh=5)

Parameters:

  • app (int): Flask application

  • sched (TaskScheduler): task scheduler with task definitions

  • display_name (str): name of the application to be displayed
    • default app.name

  • endpoint (str): URL endpoint where the taskmonitor can be viewed
    • default “@taskmonitor”

  • homepage_refresh (int): home page auto refresh interval (in seconds)
    • default 30

  • taskpage_refresh (int): task page auto refresh interval (in seconds)
    • default 5

from flask import Flask
from flask_production import CherryFlask, TaskScheduler
from flask_production.plugins import TaskMonitor

app = Flask(__name__)
sched = TaskScheduler(check_interval=2)

monitor = TaskMonitor(app, sched)
print(monitor._endpoint) # /@taskmonitor

# Run every minute
sched.every(60).do(foo)

cherry = CherryFlask(app, scheduler=sched)
cherry.run(host="0.0.0.0", port=8080) # localhost:8080/@taskmonitor

Example Gist here

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_production-2.8.3.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

flask_production-2.8.3-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file flask_production-2.8.3.tar.gz.

File metadata

  • Download URL: flask_production-2.8.3.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for flask_production-2.8.3.tar.gz
Algorithm Hash digest
SHA256 51a26d0a12df9bc7e6f8b21d6f8ea40a126eda1ce3e5d0f7ec31f591a762d885
MD5 04bce707ffb4cbb41df4ca68936cbef7
BLAKE2b-256 0e78854c6d714499479f09016345d867b6f5e1251a95c2d68c4d5f7325133c72

See more details on using hashes here.

File details

Details for the file flask_production-2.8.3-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_production-2.8.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6aaca2ecb7fb0581b1907108053b3d4f603132d9df243088fa5e82a3c0942a56
MD5 466a6dfcabbdb6967a9b0ae650f07d59
BLAKE2b-256 21ba075e6f2cdc31157a203924da428d5eb1851cbdb0b53b41415d8a6ecaa6a2

See more details on using hashes here.

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