-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to dynamically extend flask cli management commands (#201)
- Loading branch information
1 parent
cf695de
commit 721afd2
Showing
7 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Health Check Plugin | ||
|
||
This is an example plugin that demonstrates how to extend Flask CLI commands using plugins. The `health_check_plugin` adds a custom `health` command to the Flask CLI, which performs a health check of the application, including verifying database connectivity. | ||
|
||
## Overview | ||
|
||
The plugin consists of the following files: | ||
|
||
- **`__init__.py`**: Initializes the plugin by defining an `init_app` function that registers the CLI commands. | ||
- **`cli.py`**: Contains the implementation of the `health` command. | ||
- **`setup.py`**: Defines the plugin's setup configuration and registers the entry point for the CLI command. | ||
|
||
## Installation | ||
|
||
To install the plugin the App container Dockerfile | ||
|
||
``` | ||
WORKDIR /app/plugins | ||
ADD ./examples/plugins/health_check_plugin ./health_check_plugin | ||
RUN pip install ./health_check_plugin | ||
# Reset working directory | ||
WORKDIR /app | ||
``` | ||
|
||
## Usage | ||
|
||
After installing the plugin, the `health` command becomes available in the Flask CLI: | ||
|
||
```bash | ||
flask health | ||
``` | ||
|
||
This command outputs the application's health status in JSON format, indicating the database connection status and the application version. | ||
|
||
## Purpose | ||
|
||
This plugin serves as an example of how to extend Flask CLI commands using plugins and entry points. It demonstrates: | ||
|
||
- How to create a custom CLI command in a plugin. | ||
- How to register the command using entry points in `setup.py`. | ||
|
||
By following this example, you can create your own plugins to extend the functionality of your Flask application's CLI in a modular and scalable way. | ||
|
||
## Files | ||
|
||
- **[`__init__.py`](./__init__.py)**: Plugin initialization code. | ||
- **[`cli.py`](./cli.py)**: Implementation of the `health` CLI command. | ||
- **[`setup.py`](./setup.py)**: Setup script defining the plugin metadata and entry points. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from flask import Flask | ||
|
||
|
||
def init_app(app: Flask) -> None: | ||
from .cli import health_command | ||
|
||
app.cli.add_command(health_command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import logging | ||
|
||
import click | ||
from flask.cli import with_appcontext | ||
from sqlalchemy import text | ||
|
||
|
||
@click.command("health") | ||
@with_appcontext | ||
def health_command() -> None: | ||
"""Displays application database health and metrics in JSON format.""" | ||
from flask import current_app, json | ||
|
||
from api.extensions import db | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
# Perform a simple database health check using SQLAlchemy | ||
db.session.execute(text("SELECT 1")) | ||
db_status = "connected" | ||
error = None | ||
logger.info("Database connection successful.") | ||
|
||
# Retrieve all table names and their row counts | ||
tables_query = text(""" | ||
SELECT table_name | ||
FROM information_schema.tables | ||
WHERE table_schema = 'public'; | ||
""") | ||
tables = db.session.execute(tables_query).fetchall() | ||
|
||
table_sizes = {} | ||
for table in tables: | ||
table_name = table[0] | ||
row_count_query = text(f"SELECT COUNT(*) FROM {table_name}") | ||
row_count = db.session.execute(row_count_query).scalar() | ||
table_sizes[table_name] = row_count | ||
|
||
except Exception as e: | ||
db_status = "disconnected" | ||
error = str(e) | ||
table_sizes = {} | ||
logger.error(f"Database connection error: {error}") | ||
|
||
# Prepare the health status response | ||
status = { | ||
"status": "ok" if db_status == "connected" else "error", | ||
"database": db_status, | ||
"tables": table_sizes, | ||
"version": current_app.config.get("APP_VERSION", "Not Defined"), | ||
**({"error": error} if error else {}), | ||
} | ||
|
||
# Log the health status | ||
logger.info(f"Health status: {status}") | ||
|
||
# Output the health status as a JSON string | ||
click.echo(json.dumps(status)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
name="health_check_plugin", | ||
version="0.1.0", | ||
packages=["health_check_plugin"], | ||
package_dir={"health_check_plugin": "."}, # Map package to current directory | ||
install_requires=[ | ||
"Flask", | ||
], | ||
entry_points={ | ||
"flask.commands": [ | ||
"health=health_check_plugin.cli:health_command", | ||
], | ||
}, | ||
) |