annotate piecrust/wsgiutil/__init__.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents d0f86d9a9d40
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
1 import logging
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
2 from piecrust.serving.wrappers import get_piecrust_server
379
d40b744a9d99 serve: Add a generic WSGI app factory.
Ludovic Chabant <ludovic@chabant.com>
parents: 376
diff changeset
3
d40b744a9d99 serve: Add a generic WSGI app factory.
Ludovic Chabant <ludovic@chabant.com>
parents: 376
diff changeset
4
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
5 def _setup_logging(log_file, log_level, max_log_bytes, log_backup_count):
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
6 if log_file:
1175
d0f86d9a9d40 wsgi: Better logging for the admin app.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
7 from logging import Formatter
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
8 from logging.handlers import RotatingFileHandler
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
9 handler = RotatingFileHandler(log_file, maxBytes=max_log_bytes,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
10 backupCount=log_backup_count)
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
11 handler.setLevel(log_level)
1175
d0f86d9a9d40 wsgi: Better logging for the admin app.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
12 handler.setFormatter(Formatter(
d0f86d9a9d40 wsgi: Better logging for the admin app.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
13 fmt='%(asctime)s %(levelname)s %(name)s: %(message)s'))
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
14 logging.getLogger().addHandler(handler)
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
15
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
16
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
17 def get_app(root_dir, *,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
18 cache_key='prod',
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
19 serve_admin=False,
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
20 log_file=None,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
21 log_level=logging.INFO,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
22 log_backup_count=0,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
23 max_log_bytes=4096):
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
24 _setup_logging(log_file, log_level, max_log_bytes, log_backup_count)
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
25 app = get_piecrust_server(root_dir,
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
26 serve_site=True,
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
27 serve_admin=serve_admin,
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
28 cache_key=cache_key)
379
d40b744a9d99 serve: Add a generic WSGI app factory.
Ludovic Chabant <ludovic@chabant.com>
parents: 376
diff changeset
29 return app
d40b744a9d99 serve: Add a generic WSGI app factory.
Ludovic Chabant <ludovic@chabant.com>
parents: 376
diff changeset
30
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
31
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
32 def get_admin_app(root_dir, *,
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
33 cache_key='prod',
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
34 log_file=None,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
35 log_level=logging.INFO,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
36 log_backup_count=0,
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
37 max_log_bytes=4096):
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
38 _setup_logging(log_file, log_level, max_log_bytes, log_backup_count)
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
39 app = get_piecrust_server(root_dir,
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
40 serve_site=False,
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
41 serve_admin=True,
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 897
diff changeset
42 cache_key=cache_key)
897
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
43 return app
b4156f5d4368 wsgi: Add admin panel to WSGI helpers.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
44