comparison piecrust/serving/wrappers.py @ 666:81d9c3a3a0b5

internal: Get rid of the whole "sub cache" business. * Compute cache keys up front, so the cache directory is only chosen once. * Buffer up config variants to apply before loading the config. Makes it possible to cache variant-resulting configs, too. * Make a factory class to reuse the logic that creates the `PieCrust` object correctly for multi-process workers and such. * Add a test.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 03 Mar 2016 08:22:41 -0800
parents 3ceeca7bb71c
children b91fe30ae7aa
comparison
equal deleted inserted replaced
665:5dc13c816045 666:81d9c3a3a0b5
6 6
7 7
8 logger = logging.getLogger(__name__) 8 logger = logging.getLogger(__name__)
9 9
10 10
11 def run_werkzeug_server(root_dir, host, port, 11 def run_werkzeug_server(appfactory, host, port,
12 debug_piecrust=False, theme_site=False,
13 sub_cache_dir=None,
14 use_debugger=False, use_reloader=False): 12 use_debugger=False, use_reloader=False):
15 from werkzeug.serving import run_simple 13 from werkzeug.serving import run_simple
16 14
17 def _run_sse_check(): 15 def _run_sse_check():
18 # We don't want to run the processing loop here if this isn't 16 # We don't want to run the processing loop here if this isn't
22 # the second time, when the reloading process is spawned, with the 20 # the second time, when the reloading process is spawned, with the
23 # `WERKZEUG_RUN_MAIN` variable set. 21 # `WERKZEUG_RUN_MAIN` variable set.
24 return (not use_reloader or 22 return (not use_reloader or
25 os.environ.get('WERKZEUG_RUN_MAIN') == 'true') 23 os.environ.get('WERKZEUG_RUN_MAIN') == 'true')
26 24
27 app = _get_piecrust_server(root_dir, 25 app = _get_piecrust_server(appfactory,
28 debug=debug_piecrust,
29 theme_site=theme_site,
30 sub_cache_dir=sub_cache_dir,
31 run_sse_check=_run_sse_check) 26 run_sse_check=_run_sse_check)
32 27
33 # We need to do a few things to get Werkzeug to properly shutdown or 28 # We need to do a few things to get Werkzeug to properly shutdown or
34 # restart while SSE responses are running. This is because Werkzeug runs 29 # restart while SSE responses are running. This is because Werkzeug runs
35 # them in background threads (because we tell it to), but those threads 30 # them in background threads (because we tell it to), but those threads
77 # not exit. 72 # not exit.
78 _shutdown_server() 73 _shutdown_server()
79 raise 74 raise
80 75
81 76
82 def run_gunicorn_server(root_dir, 77 def run_gunicorn_server(appfactory, gunicorn_options=None):
83 debug_piecrust=False, theme_site=False,
84 sub_cache_dir=None,
85 gunicorn_options=None):
86 from gunicorn.app.base import BaseApplication 78 from gunicorn.app.base import BaseApplication
87 79
88 class PieCrustGunicornApplication(BaseApplication): 80 class PieCrustGunicornApplication(BaseApplication):
89 def __init__(self, app, options): 81 def __init__(self, app, options):
90 self.app = app 82 self.app = app
97 self.cfg.set(k, v) 89 self.cfg.set(k, v)
98 90
99 def load(self): 91 def load(self):
100 return self.app 92 return self.app
101 93
102 app = _get_piecrust_server(root_dir, 94 app = _get_piecrust_server(appfactory)
103 debug=debug_piecrust,
104 theme_site=theme_site,
105 sub_cache_dir=sub_cache_dir)
106 95
107 gunicorn_options = gunicorn_options or {} 96 gunicorn_options = gunicorn_options or {}
108 app_wrapper = PieCrustGunicornApplication(app, gunicorn_options) 97 app_wrapper = PieCrustGunicornApplication(app, gunicorn_options)
109 app_wrapper.run() 98 app_wrapper.run()
110 99
111 100
112 def _get_piecrust_server( 101 def _get_piecrust_server(appfactory, run_sse_check=None):
113 root_dir, debug=False, theme_site=False,
114 sub_cache_dir=None, run_sse_check=None):
115 from piecrust.serving.middlewares import ( 102 from piecrust.serving.middlewares import (
116 StaticResourcesMiddleware, PieCrustDebugMiddleware) 103 StaticResourcesMiddleware, PieCrustDebugMiddleware)
117 from piecrust.serving.server import WsgiServer 104 from piecrust.serving.server import WsgiServer
118 app = WsgiServer(root_dir, debug=debug, theme_site=theme_site, 105 app = WsgiServer(appfactory)
119 sub_cache_dir=sub_cache_dir)
120 app = StaticResourcesMiddleware(app) 106 app = StaticResourcesMiddleware(app)
121 app = PieCrustDebugMiddleware(app, root_dir, 107 app = PieCrustDebugMiddleware(
122 theme_site=theme_site, 108 app, appfactory, run_sse_check=run_sse_check)
123 sub_cache_dir=sub_cache_dir,
124 run_sse_check=run_sse_check)
125 return app 109 return app
126 110