utilise hobo.multitenant.spooler (#76542) #36
|
@ -13,7 +13,7 @@ chmod-socket = 666
|
|||
vacuum = true
|
||||
|
||||
spooler-processes = 5
|
||||
spooler-python-import = authentic2.utils.spooler
|
||||
spooler-python-import = hobo.multitenant.spooler
|
||||
spooler-max-tasks = 20
|
||||
|
||||
cron2 = minute=0,unique=1 /usr/bin/authentic2-multitenant-manage tenant_command clearsessions --all-tenants
|
||||
|
|
|
@ -13,7 +13,7 @@ chmod-socket = 666
|
|||
vacuum = true
|
||||
|
||||
spooler-processes = 3
|
||||
spooler-python-import = authentic2.utils.spooler
|
||||
spooler-python-import = hobo.multitenant.spooler
|
||||
spooler-max-tasks = 20
|
||||
|
||||
cron2 = minute=0,unique=1 /usr/bin/authentic2-manage clearsessions
|
||||
|
|
|
@ -64,7 +64,7 @@ from .forms import (
|
|||
)
|
||||
from .journal_views import BaseJournalView
|
||||
from .tables import OuUserRolesTable, UserAuthorizationsTable, UserRolesTable, UserTable
|
||||
from .user_export import UserExport
|
||||
from .user_export import UserExport, export_users_to_file
|
||||
from .utils import get_ou_count, has_show_username
|
||||
from .views import (
|
||||
Action,
|
||||
|
@ -540,7 +540,7 @@ class UsersExportView(UsersView):
|
|||
def get(self, request, *args, **kwargs):
|
||||
export = UserExport.new()
|
||||
query = self.get_table_data().query
|
||||
spooler.export_users(uuid=export.uuid, query=query)
|
||||
spooler.run(export_users_to_file, uuid=export.uuid, query=query)
|
||||
return redirect(request, 'a2-manager-users-export-progress', kwargs={'uuid': export.uuid})
|
||||
|
||||
|
||||
|
|
|
@ -14,80 +14,9 @@
|
|||
# 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 contextlib
|
||||
import logging
|
||||
import sys
|
||||
from functools import wraps
|
||||
try:
|
||||
from hobo.multitenant.spooler import run # pylint: disable=unused-import
|
||||
except ImportError:
|
||||
|
||||
from django.db import close_old_connections, connection
|
||||
|
||||
USE_UWSGI = 'uwsgi' in sys.modules
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def ensure_db(func):
|
||||
"""Emulate Django"s setup/teardown of database connections before/after
|
||||
each request"""
|
||||
|
||||
@wraps(func)
|
||||
def f(*args, **kwargs):
|
||||
close_old_connections()
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
finally:
|
||||
close_old_connections()
|
||||
|
||||
return f
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def tenant_context(domain):
|
||||
from hobo.multitenant.middleware import TenantMiddleware # pylint: disable=import-error
|
||||
from tenant_schemas.utils import tenant_context # pylint: disable=import-error
|
||||
|
||||
tenant = TenantMiddleware.get_tenant_by_hostname(domain)
|
||||
with tenant_context(tenant):
|
||||
yield
|
||||
|
||||
|
||||
def tenantspool(func):
|
||||
"""Wrap a function with uwsgidecorators.spool storing and restoring the
|
||||
current tenant."""
|
||||
if not USE_UWSGI:
|
||||
return func
|
||||
|
||||
from uwsgidecorators import spool
|
||||
|
||||
@ensure_db
|
||||
@wraps(func)
|
||||
def spooler_func(*args, **kwargs):
|
||||
with contextlib.ExitStack() as stack:
|
||||
if 'domain' in kwargs:
|
||||
stack.enter_context(tenant_context(kwargs.pop('domain')))
|
||||
try:
|
||||
func(*args, **kwargs)
|
||||
except Exception:
|
||||
logger.exception('spooler: exception during %s(%s, %s)', func.__name__, args, kwargs)
|
||||
else:
|
||||
logger.info('spooler: success of %s(%s, %s)', func.__name__, args, kwargs)
|
||||
|
||||
# pass arguments as pickles
|
||||
base_spooler = spool(pass_arguments=True)(spooler_func)
|
||||
|
||||
@wraps(func)
|
||||
def spooler(*args, **kwargs):
|
||||
domain = getattr(getattr(connection, 'tenant', None), 'domain_url', None)
|
||||
if domain is not None:
|
||||
kwargs['domain'] = domain
|
||||
return base_spooler(*args, **kwargs)
|
||||
|
||||
return spooler
|
||||
|
||||
|
||||
@tenantspool
|
||||
def export_users(uuid, query):
|
||||
from authentic2.manager.user_export import export_users_to_file
|
||||
|
||||
export_users_to_file(uuid, query)
|
||||
def run(func, *args, **kwargs):
|
||||
func(*args, **kwargs)
|
||||
|
|
Loading…
Reference in New Issue