Created
September 14, 2016 15:41
-
-
Save jmvrbanac/2deb9fa15e03ecbb315638d86313167e to your computer and use it in GitHub Desktop.
Example Custom Gunicorn Application
This file contains hidden or 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
""" | |
Sample Application | |
Usage: | |
sample [options] | |
Options: | |
-h --help Show this screen. | |
""" | |
import aumbry | |
from docopt import docopt | |
from gunicorn.app.base import BaseApplication | |
from gunicorn.workers.sync import SyncWorker | |
from sample.app import SampleApp | |
from sample.config import AppConfig | |
class CustomWorker(SyncWorker): | |
def handle_quit(self, sig, frame): | |
self.app.application.stop(sig) | |
super().handle_quit(sig, frame) | |
def run(self): | |
self.app.application.start() | |
super().run() | |
class GunicornApp(BaseApplication): | |
""" Custom Gunicorn application | |
This allows for us to load gunicorn settings from either consul or disk. | |
""" | |
def __init__(self, app, options=None): | |
self.options = options or {} | |
self.application = app | |
super().__init__() | |
def load_config(self): | |
for key, value in self.options.items(): | |
self.cfg.set(key.lower(), value) | |
self.cfg.set('worker_class', 'sample.__main__.CustomWorker') | |
def load(self): | |
return self.application | |
def main(): | |
docopt(__doc__) | |
cfg = aumbry.load( | |
aumbry.FILE, | |
AppConfig, | |
{ | |
'CONFIG_FILE_PATH': './etc/sample/config.yml' | |
} | |
) | |
sample_app = SampleApp(cfg) | |
gunicorn_app = GunicornApp(sample_app, cfg.gunicorn) | |
gunicorn_app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment