Last active
September 9, 2019 12:57
-
-
Save zeekay/11556588 to your computer and use it in GitHub Desktop.
Management command for Flask + uWSGI , restarts uWSGI on file changes.
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
from flask import Flask | |
def configure_uwsgi(app): | |
# Setup debugging, logging for uwsgi. | |
try: | |
import uwsgi | |
except ImportError: | |
return | |
from time import strftime | |
if app.debug: | |
from werkzeug.debug import DebuggedApplication | |
app.wsgi_app = DebuggedApplication(app.wsgi_app, True) | |
@app.before_request | |
def logging(): | |
uwsgi.set_logvar('iso_time', strftime('%Y-%m-%d %H:%M:%S')) | |
def create_app(): | |
''' | |
App factory. | |
''' | |
app = Flask(__name__) | |
configure_uwsgi(app) | |
# do any other configuration | |
return app |
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
from flask.ext.script import Manager | |
from app import create_app | |
manager = Manager(create_app) | |
@manager.command | |
def watch(): | |
import sys | |
import subprocess | |
from optimus.utils import bytes_fmt | |
from watchdog.events import PatternMatchingEventHandler | |
from watchdog.observers import Observer | |
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) | |
def colorize(line): | |
try: | |
date, time, method, uri, status, msecs, size = line.strip().split(' ') | |
message = '\033[30m{0} {1} \033[32m{2} \033[30m{3} - {4}\033[0m\n'.format(method, uri, status, msecs, bytes_fmt(int(size.strip()))) | |
except ValueError: | |
message = line | |
return message | |
class EventHandler(PatternMatchingEventHandler): | |
def on_any_event(self, event): | |
os.system('touch .reload') | |
f = subprocess.Popen(['tail', '-F', 'logs/uwsgi-dev.log'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
event_handler = EventHandler(patterns=['*.py']) | |
observer = Observer() | |
observer.schedule(event_handler, path='.', recursive=True) | |
observer.start() | |
try: | |
while True: | |
sys.stdout.write(colorize(f.stdout.readline())) | |
except KeyboardInterrupt, SystemExit: | |
observer.stop() | |
observer.join() |
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
[uwsgi] | |
; path to file which will be used to trigger reload | |
touch-reload = /dir/to/your/app/.reload | |
; I add a custom iso_time specifier for nicer log output | |
log-format = %(iso_time) %(method) %(uri) %(status) %(msecs)ms %(size) | |
; And log some place where we can tail it | |
logto = /dir/to/your/app/log/uwsgi.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment