mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Merge pull request #740 from tshlabs/735-envvars-preload
Move setting of environmental variables (configured via the raw_env sett...
This commit is contained in:
commit
d9c15dcde5
@ -106,6 +106,11 @@ class Arbiter(object):
|
||||
in sorted(self.cfg.settings.items(),
|
||||
key=lambda setting: setting[1]))))
|
||||
|
||||
# set enviroment' variables
|
||||
if self.cfg.env:
|
||||
for k, v in self.cfg.env.items():
|
||||
os.environ[k] = v
|
||||
|
||||
if self.cfg.preload_app:
|
||||
self.app.wsgi()
|
||||
|
||||
@ -121,11 +126,6 @@ class Arbiter(object):
|
||||
self.pidfile.create(self.pid)
|
||||
self.cfg.on_starting(self)
|
||||
|
||||
# set enviroment' variables
|
||||
if self.cfg.env:
|
||||
for k, v in self.cfg.env.items():
|
||||
os.environ[k] = v
|
||||
|
||||
self.init_signals()
|
||||
if not self.LISTENERS:
|
||||
self.LISTENERS = create_sockets(self.cfg, self.log)
|
||||
|
||||
57
tests/test_008-arbiter-env.py
Normal file
57
tests/test_008-arbiter-env.py
Normal file
@ -0,0 +1,57 @@
|
||||
# -*- coding: utf-8 -
|
||||
#
|
||||
# This file is part of gunicorn released under the MIT license.
|
||||
# See the NOTICE for more information.
|
||||
|
||||
import t
|
||||
import os
|
||||
from gunicorn.app.base import BaseApplication
|
||||
import gunicorn.arbiter
|
||||
|
||||
|
||||
class PreloadedAppWithEnvSettings(BaseApplication):
|
||||
"""
|
||||
Simple application that makes use of the 'preload' feature to
|
||||
start the application before spawning worker processes and sets
|
||||
environmental variable configuration settings.
|
||||
"""
|
||||
|
||||
def init(self, parser, opts, args):
|
||||
"""No-op"""
|
||||
pass
|
||||
|
||||
def load(self):
|
||||
"""No-op"""
|
||||
pass
|
||||
|
||||
def load_config(self):
|
||||
"""Set the 'preload_app' and 'raw_env' settings in order to verify their
|
||||
interaction below.
|
||||
"""
|
||||
self.cfg.set('raw_env', [
|
||||
'SOME_PATH=/tmp/something', 'OTHER_PATH=/tmp/something/else'])
|
||||
self.cfg.set('preload_app', True)
|
||||
|
||||
def wsgi(self):
|
||||
"""Assert that the expected environmental variables are set when
|
||||
the main entry point of this application is called as part of a
|
||||
'preloaded' application.
|
||||
"""
|
||||
verify_env_vars()
|
||||
return super(PreloadedAppWithEnvSettings, self).wsgi()
|
||||
|
||||
|
||||
def verify_env_vars():
|
||||
t.eq(os.getenv('SOME_PATH'), '/tmp/something')
|
||||
t.eq(os.getenv('OTHER_PATH'), '/tmp/something/else')
|
||||
|
||||
|
||||
def test_env_vars_available_during_preload():
|
||||
"""Ensure that configured environmental variables are set during the
|
||||
initial set up of the application (called from the .setup() method of
|
||||
the Arbiter) such that they are available during the initial loading
|
||||
of the WSGI application.
|
||||
"""
|
||||
# Note that we aren't making any assertions here, they are made in the
|
||||
# dummy application object being loaded here instead.
|
||||
gunicorn.arbiter.Arbiter(PreloadedAppWithEnvSettings())
|
||||
Loading…
x
Reference in New Issue
Block a user