Skip to content

Commit

Permalink
Merge pull request jprorama#164 from rtripath89/add_gunicorn_applicat…
Browse files Browse the repository at this point in the history
…ion_server

Add gunicorn application server
  • Loading branch information
jprorama authored Jul 17, 2020
2 parents dd1dba1 + 432eeca commit 6054afa
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 25 deletions.
8 changes: 4 additions & 4 deletions roles/ood_user_reg_cloud/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
virtualenv_command: /usr/bin/python3 -m venv
chdir: "{{ user_register_app_path }}"

- name: Install uwsgi in virtualenv
- name: Install gunicorn in virtualenv
pip:
name: uwsgi
name: gunicorn
virtualenv: venv
virtualenv_command: /usr/bin/python3 -m venv
chdir: "{{ user_register_app_path }}"
Expand Down Expand Up @@ -69,12 +69,12 @@
src: wsgi.j2
dest: "{{ user_register_app_path }}/wsgi.py"

- name: Put uwsgi config file in place
- name: Put gunicorn config file in place
template:
src: ini.j2
dest: "{{ user_register_app_path }}/{{ user_register_app }}.ini"

- name: Create uwsgi system service
- name: Create gunicorn system service
template:
src: service.j2
dest: "/etc/systemd/system/{{ user_register_app }}.service"
Expand Down
229 changes: 212 additions & 17 deletions roles/ood_user_reg_cloud/templates/ini.j2
Original file line number Diff line number Diff line change
@@ -1,23 +1,218 @@
[uwsgi]
mount = /register=wsgi.py
# Sample Gunicorn configuration file.
# Location of https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py
#
# Server socket
#
# bind - The socket to bind.
#
# A string of the form: 'HOST', 'HOST:PORT', 'unix:PATH'.
# An IP is a valid HOST.
#
# backlog - The number of pending connections. This refers
# to the number of clients that can be waiting to be
# served. Exceeding this number results in the client
# getting an error when attempting to connect. It should
# only affect servers under significant load.
#
# Must be a positive integer. Generally set in the 64-2048
# range.
#

uid = {{ RegUser_app_user }}
req-logger = file:{{ user_register_app_path }}/log/req
logger = file:{{ user_register_app_path }}/log/err
bind = '127.0.0.1:8000'
backlog = 2048

manage-script-name = true
single-interpreter = true
http-websockets = true
enable-threads = true
#
# Worker processes
#
# workers - The number of worker processes that this server
# should keep alive for handling requests.
#
# A positive integer generally in the 2-4 x $(NUM_CORES)
# range. You'll want to vary this a bit to find the best
# for your particular application's work load.
#
# worker_class - The type of workers to use. The default
# sync class should handle most 'normal' types of work
# loads. You'll want to read
# http://docs.gunicorn.org/en/latest/design.html#choosing-a-worker-type
# for information on when you might want to choose one
# of the other worker classes.
#
# A string referring to a Python path to a subclass of
# gunicorn.workers.base.Worker. The default provided values
# can be seen at
# http://docs.gunicorn.org/en/latest/settings.html#worker-class
#
# worker_connections - For the eventlet and gevent worker classes
# this limits the maximum number of simultaneous clients that
# a single process can handle.
#
# A positive integer generally set to around 1000.
#
# timeout - If a worker does not notify the master process in this
# number of seconds it is killed and a new worker is spawned
# to replace it.
#
# Generally set to thirty seconds. Only set this noticeably
# higher if you're sure of the repercussions for sync workers.
# For the non sync workers it just means that the worker
# process is still communicating and is not tied to the length
# of time required to handle a single request.
#
# keepalive - The number of seconds to wait for the next request
# on a Keep-Alive HTTP connection.
#
# A positive integer. Generally set in the 1-5 seconds range.
#

gevent = 1000
workers = 1
worker_class = 'sync'
worker_connections = 1000
timeout = 30
keepalive = 2

master = true
processes = 5
#
# spew - Install a trace function that spews every line of Python
# that is executed when running the server. This is the
# nuclear option.
#
# True or False
#

http-socket = :8000
chmod-socket = 660
vacuum = true
spew = False

die-on-term = true
venv = venv
#
# Server mechanics
#
# daemon - Detach the main Gunicorn process from the controlling
# terminal with a standard fork/fork sequence.
#
# True or False
#
# raw_env - Pass environment variables to the execution environment.
#
# pidfile - The path to a pid file to write
#
# A path string or None to not write a pid file.
#
# user - Switch worker processes to run as this user.
#
# A valid user id (as an integer) or the name of a user that
# can be retrieved with a call to pwd.getpwnam(value) or None
# to not change the worker process user.
#
# group - Switch worker process to run as this group.
#
# A valid group id (as an integer) or the name of a user that
# can be retrieved with a call to pwd.getgrnam(value) or None
# to change the worker processes group.
#
# umask - A mask for file permissions written by Gunicorn. Note that
# this affects unix socket permissions.
#
# A valid value for the os.umask(mode) call or a string
# compatible with int(value, 0) (0 means Python guesses
# the base, so values like "0", "0xFF", "0022" are valid
# for decimal, hex, and octal representations)
#
# tmp_upload_dir - A directory to store temporary request data when
# requests are read. This will most likely be disappearing soon.
#
# A path to a directory where the process owner can write. Or
# None to signal that Python should choose one on its own.
#

#daemon = False
#raw_env = [
# 'DJANGO_SECRET_KEY=something',
# 'SPAM=eggs',
#]
#pidfile = None
#umask = 0
#user = None
#group = None
#tmp_upload_dir = None

#
# Logging
#
# logfile - The path to a log file to write to.
#
# A path string. "-" means log to stdout.
#
# loglevel - The granularity of log output
#
# A string of "debug", "info", "warning", "error", "critical"
#

#errorlog = '-'
errorlog = '{{ user_register_app_path }}/log/gunicorn_err.log'
loglevel = 'info'
#accesslog = '-'
accesslog = '{{ user_register_app_path }}/log/gunicorn_access.log'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'

#
# Process naming
#
# proc_name - A base to use with setproctitle to change the way
# that Gunicorn processes are reported in the system process
# table. This affects things like 'ps' and 'top'. If you're
# going to be running more than one instance of Gunicorn you'll
# probably want to set a name to tell them apart. This requires
# that you install the setproctitle module.
#
# A string or None to choose a default of something like 'gunicorn'.
#

#proc_name = flask_user

#
# Server hooks
#
# post_fork - Called just after a worker has been forked.
#
# A callable that takes a server and worker instance
# as arguments.
#
# pre_fork - Called just prior to forking the worker subprocess.
#
# A callable that accepts the same arguments as after_fork
#
# pre_exec - Called just prior to forking off a secondary
# master process during things like config reloading.
#
# A callable that takes a server instance as the sole argument.
#

def post_fork(server, worker):
server.log.info("Worker spawned (pid: %s)", worker.pid)

def pre_fork(server, worker):
pass

def pre_exec(server):
server.log.info("Forked child, re-executing.")

def when_ready(server):
server.log.info("Server is ready. Spawning workers")

def worker_int(worker):
worker.log.info("worker received INT or QUIT signal")

## get traceback info
import threading, sys, traceback
id2name = {th.ident: th.name for th in threading.enumerate()}
code = []
for threadId, stack in sys._current_frames().items():
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
threadId))
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename,
lineno, name))
if line:
code.append(" %s" % (line.strip()))
worker.log.debug("\n".join(code))

def worker_abort(worker):
worker.log.info("worker received SIGABRT signal")
2 changes: 1 addition & 1 deletion roles/ood_user_reg_cloud/templates/service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ User={{ RegUser_app_user }}
Group=apache
WorkingDirectory={{ user_register_app_path }}
Environment="PATH={{ user_register_app_path }}/venv/bin"
ExecStart={{ user_register_app_path }}/venv/bin/uwsgi --ini {{ user_register_app }}.ini
ExecStart={{ user_register_app_path }}/venv/bin/gunicorn -k gevent -c {{ user_register_app }}.ini wsgi:application

[Install]
WantedBy=multi-user.target
17 changes: 14 additions & 3 deletions roles/ood_user_reg_cloud/templates/user-reg_conf.j2
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so

ProxyPass /socket.io/ http://127.0.0.1:8000/socket.io/
ProxyPassReverse /socket.io/ http://127.0.0.1:8000/socket.io/

ProxyPass /register http://127.0.0.1:8000/register/
ProxyPass /register http://127.0.0.1:8000/
ProxyPassReverse /register http://127.0.0.1:8000/

ProxyPass /static http://127.0.0.1:8000/static
ProxyPassReverse /static http://127.0.0.1:8000/static

<Location /register>
AuthType Basic
AuthName "Private"
AuthUserFile "/opt/rh/httpd24/root/etc/httpd/.htpasswd"
RequestHeader unset Authorization
Require valid-user
</Location>

<Location "/static/">
AuthType Basic
AuthName "Private"
AuthUserFile "/opt/rh/httpd24/root/etc/httpd/.htpasswd"
RequestHeader unset Authorization
Require valid-user
</Location>

0 comments on commit 6054afa

Please sign in to comment.