55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
# hobo - portal to configure and deploy applications
|
|
# Copyright (C) 2019 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import threading
|
|
|
|
|
|
_Thread_bootstrap_inner = threading.Thread._bootstrap_inner
|
|
_Thread__init__ = threading.Thread.__init__
|
|
|
|
|
|
def _new__init__(self, *args, **kwargs):
|
|
from django.db import connection
|
|
try:
|
|
if hasattr(connection, 'get_tenant'):
|
|
self.tenant = connection.get_tenant()
|
|
else:
|
|
self.tenant = None
|
|
except RuntimeError:
|
|
# this happens when ImportError is raised at startup; ignore
|
|
# the error to let the real one be displayed.
|
|
self.tenant = None
|
|
_Thread__init__(self, *args, **kwargs)
|
|
|
|
|
|
def _new_bootstrap_inner(self):
|
|
if self.tenant is not None:
|
|
from django.db import connection
|
|
old_tenant = connection.get_tenant()
|
|
connection.set_tenant(self.tenant)
|
|
try:
|
|
_Thread_bootstrap_inner(self)
|
|
finally:
|
|
connection.set_tenant(old_tenant)
|
|
connection.close()
|
|
else:
|
|
_Thread_bootstrap_inner(self)
|
|
|
|
|
|
def install_tenant_aware_threads():
|
|
threading.Thread.__init__ = _new__init__
|
|
threading.Thread._bootstrap_inner = _new_bootstrap_inner
|