Skip to content

Commit

Permalink
use gunicorn for run_server
Browse files Browse the repository at this point in the history
  • Loading branch information
houqp authored and jrgp committed May 5, 2017
1 parent bbce90f commit 469a3a7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 80 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
all: serve

serve:
gunicorn --reload --access-logfile=- -b '0.0.0.0:16649' --worker-class gevent \
-e CONFIG=./configs/config.dev.yaml \
iris.wrappers.gunicorn:application
iris-server ./configs/config.dev.yaml

sender:
iris-sender configs/config.dev.yaml
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
entry_points={
'console_scripts': [
'iris-api = iris.bin.run_server:main',
'iris-server = iris.bin.run_server:main',
'iris = iris.bin.run_server:main',
'iris-sender = iris.bin.sender:main',
'iris-sync-targets = iris.bin.sync_targets:main',
Expand Down
95 changes: 27 additions & 68 deletions src/iris/bin/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,42 @@
# Copyright (c) LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
# See LICENSE in the project root for license information.

# -*- coding:utf-8 -*-

import gevent
from gevent import monkey
monkey.patch_all() # noqa
from gevent.pywsgi import WSGIServer
from gevent.subprocess import call

import time
import fnmatch
import signal
import sys
import os

import logging
logging.basicConfig()
import multiprocessing
import gunicorn.app.base
from gunicorn.six import iteritems
from iris.config import load_config
from iris.api import get_api

try:
import gevent_inotifyx as inotify
except ImportError:
HAS_INOTIFY = False
else:
HAS_INOTIFY = True

class StandaloneApplication(gunicorn.app.base.BaseApplication):

def event_producer(fd, server):
while True:
restart = False
events = inotify.get_events(fd, None)
for event in events:
ignore = False
if call(['git', 'check-ignore', event.name]) == 0 or fnmatch.fnmatch(event.name, '.git/*'):
ignore = True
break
if not ignore:
print "File changed:", event.name, event.get_mask_description()
restart = True
if restart:
print 'Restarting %s ...\n' % sys.argv
server.stop(timeout=1)
server.close()
print 'Waiting...'
time.sleep(3)
print 'Restart.'
os.execvp(sys.argv[0], sys.argv)
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super(StandaloneApplication, self).__init__()

def load_config(self):
config = {key: value for key, value in iteritems(self.options)
if key in self.cfg.settings and value is not None}
for key, value in iteritems(config):
self.cfg.set(key.lower(), value)

def sigint_handler(sig, frame):
print 'Caught sigint. Quitting.'
sys.exit(0)
def load(self):
return self.application


def main():
signal.signal(signal.SIGINT, sigint_handler)

from iris.api import get_api, load_config

config = load_config()
app = get_api(config)

config = load_config(sys.argv[1])
server = config['server']
print 'LISTENING: %(host)s:%(port)d' % server
server = WSGIServer((server['host'], server['port']), app)

if HAS_INOTIFY:
fd = inotify.init()

for dirname, subfolders, _ in os.walk('.'):
if '.git' in subfolders:
subfolders.remove('.git')
inotify.add_watch(fd, dirname, inotify.IN_MODIFY)

gevent.spawn(event_producer, fd, server)
else:
print 'Missing inotify, disable watch support.'
server.serve_forever()

options = {
'reload': True,
'bind': '%s:%s' % (server['host'], server['port']),
'worker_class': 'gevent',
'accesslog': '-',
'workers': (multiprocessing.cpu_count() * 2) + 1
}

if __name__ == '__main__':
main()
gunicorn_server = StandaloneApplication(get_api(config), options)
gunicorn_server.run()
Empty file removed src/iris/wrappers/__init__.py
Empty file.
8 changes: 0 additions & 8 deletions src/iris/wrappers/gunicorn.py

This file was deleted.

0 comments on commit 469a3a7

Please sign in to comment.