flask-production

cherrypy server for Flask + task scheduler and monitor


Keywords
cherrypy, flask, python3, tasks
License
MIT
Install
pip install flask-production==3.0.1

Documentation

flask-production

Cherrypy prod server for Flask + parallel task scheduler

Python 3.7 license pytest

Installation

Usage example

CherryFlask

Cherrypy server on top of Flask app

Parameters:

  • app (Flask): Flask application
  • scheduler (TaskScheduler): task scheduler to run in parallel with Flask app
  • silent (bool): don't print logs
    • default False

TaskScheduler

Main class to setup, run and manage jobs

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 and read back on app restart
    • default True (logs will be stored)
  • state_handler (.state.BaseStateHandler): different handler backends to store job logs
    • default .state.FileSystemState (logs will be stored in a unique data directory)

Standalone usage

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

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

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
  • can_rerun (bool): if True adds rerun button to job page
    • default True
  • can_disable (bool): if True adds disable button to job page
    • default True

Example Gist here

TODO:

scheduler - function argument validation