__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import multiprocessing
  2. import os
  3. import yaml
  4. # DO NOT load env_setup here. It must be loaded after workers have been forked.
  5. from lakesuperior.config_parser import (
  6. config as main_config, default_config_dir)
  7. __doc__ = """
  8. GUnicorn WSGI configuration.
  9. GUnicorn reads configuration options from this file by importing it::
  10. gunicorn -c python:lakesuperior.wsgi lakesuperior.server:fcrepo
  11. This module reads the ``gunicorn.yml`` configuration and overrides defaults
  12. set here. Only some of the GUnicorn optionscan be changed: others have to be
  13. set to specific values in order for Lakesuperior to work properly.
  14. """
  15. __all__ = [
  16. 'bind',
  17. 'workers',
  18. 'worker_class',
  19. 'max_requests',
  20. 'user',
  21. 'group',
  22. 'raw_env',
  23. 'preload_app',
  24. 'daemon',
  25. 'reload',
  26. 'pidfile',
  27. 'accesslog',
  28. 'errorlog',
  29. ]
  30. """
  31. Variables to export to GUnicorn.
  32. Not sure if this does anything—GUnicorn doesn't seem to use ``import *``—but
  33. at least good for maintainers of this code.
  34. """
  35. class __Defaults:
  36. """
  37. Gather default values for WSGI config.
  38. """
  39. config_file = os.path.join(default_config_dir, 'gunicorn.yml')
  40. listen_addr = '0.0.0.0'
  41. listen_port = 8000
  42. preload_app = True
  43. app_mode = 'prod'
  44. worker_class = 'gevent'
  45. max_requests = 0
  46. def __init__(self):
  47. with open(self.config_file, 'r') as fh:
  48. self.config = yaml.load(fh, yaml.SafeLoader)
  49. oldwd = os.getcwd()
  50. os.chdir(main_config['application']['data_dir'])
  51. # Set data directory relatively to startup script.
  52. self.data_dir = os.path.realpath(self.config.get('data_dir'))
  53. os.chdir(oldwd)
  54. self.run_dir = os.path.join(self.data_dir, 'run')
  55. self.log_dir = os.path.join(self.data_dir, 'log')
  56. os.makedirs(self.log_dir, exist_ok=True)
  57. os.makedirs(self.run_dir, exist_ok=True)
  58. self.workers = (multiprocessing.cpu_count() * 2) + 1
  59. __def = __Defaults()
  60. __app_mode = __def.config.get('app_mode', __def.app_mode)
  61. # Exposed Gunicorn parameters begin here.
  62. bind = '{}:{}'.format(
  63. __def.config.get('listen_addr', __def.listen_addr),
  64. __def.config.get('listen_port', __def.listen_port))
  65. workers = __def.config.get('workers', __def.workers)
  66. worker_class = __def.config.get('worker_class', __def.worker_class)
  67. max_requests = __def.config.get('max_requests', __def.max_requests)
  68. user = __def.config.get('user')
  69. group = __def.config.get('group')
  70. raw_env = 'APP_MODE={}'.format(__app_mode)
  71. preload_app = __def.config.get('preload_app', __def.preload_app)
  72. daemon = __app_mode == 'prod'
  73. reload = __app_mode == 'dev' and not preload_app
  74. pidfile = os.path.join(__def.run_dir, 'fcrepo.pid')
  75. accesslog = os.path.join(__def.log_dir, 'gunicorn-access.log')
  76. errorlog = os.path.join(__def.log_dir, 'gunicorn-error.log')