Projet

Général

Profil

0001-misc-add-uwsgi-spooler-43153.patch

Valentin Deniaud, 31 mars 2021 16:24

Télécharger (6,55 ko)

Voir les différences:

Subject: [PATCH 1/3] misc: add uwsgi spooler (#43153)

 debian/authentic2-multitenant-uwsgi.ini |  3 +
 debian/authentic2-uwsgi.ini             |  3 +
 debian/authentic2.dirs                  |  1 +
 debian/authentic2.init                  |  1 +
 debian/authentic2.postinst              |  1 +
 debian/authentic2.service               |  3 +-
 debian/control                          |  1 +
 src/authentic2/utils/spooler.py         | 86 +++++++++++++++++++++++++
 8 files changed, 98 insertions(+), 1 deletion(-)
 create mode 100644 src/authentic2/utils/spooler.py
debian/authentic2-multitenant-uwsgi.ini
12 12
chmod-socket = 666
13 13
vacuum = true
14 14

  
15
spooler-processes = 3
16
spooler-python-import = authentic2.utils.spooler
17

  
15 18
master = true
16 19
enable-threads = true
17 20
harakiri = 120
debian/authentic2-uwsgi.ini
12 12
chmod-socket = 666
13 13
vacuum = true
14 14

  
15
spooler-processes = 3
16
spooler-python-import = authentic2.utils.spooler
17

  
15 18
master = true
16 19
enable-threads = true
17 20
harakiri = 120
debian/authentic2.dirs
6 6
var/lib/authentic2/collectstatic
7 7
var/lib/authentic2/locale
8 8
var/lib/authentic2/templates
9
var/lib/authentic2/spooler
9 10
var/log/authentic2
debian/authentic2.init
38 38
DAEMON_ARGS=${DAEMON_ARGS:-"--pidfile=$PIDFILE
39 39
--uid $USER --gid $GROUP
40 40
--ini /etc/$NAME/$NAME-uwsgi.ini
41
--spooler /var/lib/$NAME/spooler/
41 42
--daemonize /var/log/uwsgi.$NAME.log"}
42 43

  
43 44
# Load the VERBOSE setting and other rcS variables
debian/authentic2.postinst
98 98
            $AUTHENTIC_HOME/locale \
99 99
            $AUTHENTIC_HOME/media \
100 100
            $AUTHENTIC_HOME/templates \
101
            $AUTHENTIC_HOME/spooler \
101 102
            /var/log/$NAME \
102 103
        ;;
103 104

  
debian/authentic2.service
10 10
Group=authentic2
11 11
ExecStartPre=/usr/bin/authentic2-manage migrate --noinput
12 12
ExecStartPre=/usr/bin/authentic2-manage collectstatic --noinput
13
ExecStart=/usr/lib/authentic2/launch-authentic2.sh --ini /etc/%p/%p-uwsgi.ini
13
ExecStartPre=/bin/mkdir -p /var/lib/authentic2/spooler/%m/
14
ExecStart=/usr/lib/authentic2/launch-authentic2.sh --ini /etc/%p/%p-uwsgi.ini --spooler /var/lib/authentic2/spooler/%m/
14 15
ExecReload=/bin/kill -HUP $MAINPID
15 16
KillSignal=SIGQUIT
16 17
TimeoutStartSec=0
debian/control
57 57
    python3-psycopg2,
58 58
    uwsgi,
59 59
    uwsgi-plugin-python3,
60
    python3-uwsgidecorators,
60 61
    dbconfig-common,
61 62
    debconf | debconf-2.0, ucf
62 63
Recommends: postgresql-client
src/authentic2/utils/spooler.py
1
# authentic2 - versatile identity manager
2
# Copyright (C) 2010-2021 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import contextlib
18
import logging
19
import sys
20
from functools import wraps
21

  
22
from django.db import close_old_connections, connection
23

  
24
USE_UWSGI = 'uwsgi' in sys.modules
25

  
26

  
27
logger = logging.getLogger(__name__)
28

  
29

  
30
def ensure_db(func):
31
    """Emulate Django"s setup/teardown of database connections before/after
32
    each request"""
33

  
34
    @wraps(func)
35
    def f(*args, **kwargs):
36
        close_old_connections()
37
        try:
38
            return func(*args, **kwargs)
39
        finally:
40
            close_old_connections()
41

  
42
    return f
43

  
44

  
45
@contextlib.contextmanager
46
def tenant_context(domain):
47
    from hobo.multitenant.middleware import TenantMiddleware
48
    from tenant_schemas.utils import tenant_context
49

  
50
    tenant = TenantMiddleware.get_tenant_by_hostname(domain)
51
    with tenant_context(tenant):
52
        yield
53

  
54

  
55
def tenantspool(func):
56
    """Wrap a function with uwsgidecorators.spool storing and restoring the
57
    current tenant."""
58
    if not USE_UWSGI:
59
        return func
60

  
61
    from uwsgidecorators import spool
62

  
63
    @ensure_db
64
    @wraps(func)
65
    def spooler_func(*args, **kwargs):
66
        with contextlib.ExitStack() as stack:
67
            if 'domain' in kwargs:
68
                stack.enter_context(tenant_context(kwargs.pop('domain')))
69
            try:
70
                func(*args, **kwargs)
71
            except Exception:
72
                logger.exception('spooler: exception during %s(%s, %s)' % (func.__name__, args, kwargs))
73
            else:
74
                logger.info('spooler: success of %s(%s, %s)' % (func.__name__, args, kwargs))
75

  
76
    # pass arguments as pickles
77
    base_spooler = spool(pass_arguments=True)(spooler_func)
78

  
79
    @wraps(func)
80
    def spooler(*args, **kwargs):
81
        domain = getattr(getattr(connection, 'tenant', None), 'domain_url', None)
82
        if domain is not None:
83
            kwargs['domain'] = domain
84
        return base_spooler(*args, **kwargs)
85

  
86
    return spooler
0
-