Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
pikhovkin committed Aug 17, 2021
1 parent 7e1458f commit e7bd5f5
Show file tree
Hide file tree
Showing 25 changed files with 698 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: build

on:
push:
branches: [master]
pull_request:
branches: [master]
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, 3.10.0-beta.1]
django: ['Django>=3.1,<3.2', 'Django>=3.2,<3.3']

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }} and ${{ matrix.django }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Display Python version
run: python -c "import sys; print(sys.version)"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
pip install "${{ matrix.django }}"
- name: Test
run: |
python manage.py test
deploy:
needs: [build]

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

.idea
5 changes: 5 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
recursive-include simple_health_check *
include README.md
include LICENSE
global-exclude __pycache__
global-exclude *.py[co]
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# django-simple-health-check

[![GitHub Actions](https://github.com/pikhovkin/django-simple-health-check/workflows/build/badge.svg)](https://github.com/pikhovkin/django-simple-health-check/actions)
[![PyPI](https://img.shields.io/pypi/v/django-simple-health-check.svg)](https://pypi.org/project/django-simple-health-check/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-simple-health-check.svg)
[![framework - Django](https://img.shields.io/badge/framework-Django-0C3C26.svg)](https://www.djangoproject.com/)
![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-simple-health-check.svg)
[![PyPI - License](https://img.shields.io/pypi/l/django-simple-health-check)](./LICENSE)

Simple Django health check

Inspired by:
- [django-alive](https://github.com/lincolnloop/django-alive)
- [django-healthchecks](https://github.com/mvantellingen/django-healthchecks)
- [django-health-check](https://github.com/KristianOellegaard/django-health-check)
- [django-healthz](https://github.com/rehive/django-healthz)
- [django-watchman](https://github.com/mwarkentin/django-watchman)

### Installation

```bash
$ pip install django-simple-health-check
```

### Quick start

1. Install the package

2. Add `simple_health_check` to your INSTALLED_APPS settings like this:

```python
INSTALLED_APPS = [
...,
'simple_health_check',
]
```

3. Add `simple_health_check.urls` to main `urls.py`:

```python
from django.urls import path, include

urlpatterns = [
...,
path('', include('simple_health_check.urls')),
]
```

4. Configure the readiness checks:

```python
SIMPLE_HEALTH_CHECKS = {
'simple_health_check.checks.migrations.Migrations': [
dict(alias='db1'),
dict(alias='db2'),
],
'simple_health_check.checks.db.Databases': None,
}
```

by default

```python
SIMPLE_HEALTH_CHECKS = {
'simple_health_check.checks.migrations.Migrations': None, # check all aliases
'simple_health_check.checks.db.Databases': None, # check all aliases
}
```

## License

MIT
20 changes: 20 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python
import os
import sys


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.conf.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
41 changes: 41 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python
try:
from setuptools import setup
except ImportError:
from distutils.core import setup


setup(
name='django-simple-health-check',
version='0.1.0',
description='Simple Django health check',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author='Sergei Pikhovkin',
author_email='[email protected]',
url='https://github.com/pikhovkin/django-simple-health-check',
packages=[
'simple_health_check',
],
include_package_data=True,
install_requires=[
'Django>=3.1,<4.0',
],
python_requires='>=3.7.*, <4.0.*',
license='MIT',
zip_safe=False,
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Web Environment',
'Framework :: Django :: 3.1',
'Framework :: Django :: 3.2',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
)
1 change: 1 addition & 0 deletions simple_health_check/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'simple_health_check.apps.SimpleHealthCheckConfig'
45 changes: 45 additions & 0 deletions simple_health_check/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.apps import AppConfig
from django.conf import settings
from django.utils.module_loading import import_string


class SimpleHealthCheckConfig(AppConfig):
name = 'simple_health_check'

checks = {}

@classmethod
def register_checks(cls):
cls.checks = {}
SIMPLE_HEALTH_CHECKS = getattr(settings, 'SIMPLE_HEALTH_CHECKS', None)
if SIMPLE_HEALTH_CHECKS is None:
SIMPLE_HEALTH_CHECKS = {
'simple_health_check.checks.migrations.Migrations': None,
'simple_health_check.checks.db.Databases': None,
}
for cls_check, options in SIMPLE_HEALTH_CHECKS.items():
options = options or {}
if isinstance(options, dict):
options = [options]
elif isinstance(options, (list, set, tuple)):
...
else:
raise
try:
Check = import_string(cls_check)
except Exception:
raise
for opt in options:
identifier = hash(f'{cls_check}.{hash(frozenset((opt or {}).items()))}')
if identifier in cls.checks:
raise
check = Check(**opt) if opt else Check()
cls.checks[identifier] = check

@classmethod
def check_all(cls):
for check in cls.checks.values():
check.check()

def ready(self):
self.register_checks()
6 changes: 6 additions & 0 deletions simple_health_check/checks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BaseHealthCheck(object):
def __init__(self, **options):
...

def check(self):
raise NotImplementedError
Loading

0 comments on commit e7bd5f5

Please sign in to comment.