-
Notifications
You must be signed in to change notification settings - Fork 14
/
runner.py
97 lines (78 loc) · 3.13 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Plivo Team. See LICENSE.txt for details.
import os
import argparse
import multiprocessing
import configparser
import gunicorn.app.base
from gunicorn.six import iteritems
from sharq_server import setup_server, __version__
def number_of_workers():
return (multiprocessing.cpu_count() * 2) + 1
class SharQServerApplicationRunner(gunicorn.app.base.BaseApplication):
"""A simple SharQ Gunicorn wrapper which is used to load
config and run the application.
"""
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super(SharQServerApplicationRunner, self).__init__()
def load_config(self):
config = dict([(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 load(self):
return self.application
def run():
"""Exposes a CLI to configure the SharQ Server and runs the server."""
# create a arg parser and configure it.
parser = argparse.ArgumentParser(description='SharQ Server.')
parser.add_argument('-c', '--config', action='store', required=True,
help='Absolute path of the SharQ configuration file.',
dest='sharq_config')
parser.add_argument('-gc', '--gunicorn-config', action='store', required=False,
help='Gunicorn configuration file.',
dest='gunicorn_config')
parser.add_argument('--version', action='version', version='SharQ Server %s' % __version__)
args = parser.parse_args()
# read the configuration file and set gunicorn options.
config_parser = configparser.SafeConfigParser()
# get the full path of the config file.
sharq_config = os.path.abspath(args.sharq_config)
config_parser.read(sharq_config)
host = config_parser.get('sharq-server', 'host')
port = config_parser.get('sharq-server', 'port')
bind = '%s:%s' % (host, port)
try:
workers = config_parser.get('sharq-server', 'workers')
except configparser.NoOptionError:
workers = number_of_workers()
try:
accesslog = config_parser.get('sharq-server', 'accesslog')
except configparser.NoOptionError:
accesslog = None
options = {
'bind': bind,
'workers': workers,
'worker_class': 'gevent' # required for sharq to function.
}
if accesslog:
options.update({
'accesslog': accesslog
})
if args.gunicorn_config:
gunicorn_config = os.path.abspath(args.gunicorn_config)
options.update({
'config': gunicorn_config
})
print("""
___ _ ___ ___
/ __| |_ __ _ _ _ / _ \ / __| ___ _ ___ _____ _ _
\__ \ ' \/ _` | '_| (_) | \__ \/ -_) '_\ V / -_) '_|
|___/_||_\__,_|_| \__\_\ |___/\___|_| \_/\___|_|
Version: %s
Listening on: %s
""" % (__version__, bind))
server = setup_server(sharq_config)
SharQServerApplicationRunner(server.app, options).run()