manager: wrap csv import in provisionning context manager (#34458)
gitea/authentic/pipeline/head Build started... Details

This commit is contained in:
Benjamin Dauvergne 2019-07-01 11:29:56 +02:00 committed by Frédéric Péters
parent b8444a0cb5
commit 060e199584
1 changed files with 22 additions and 6 deletions

View File

@ -31,6 +31,7 @@ from atomicwrites import atomic_write
from django.core.files.storage import default_storage
from django.db import connection
from django.conf import settings
from django.utils import six
from django.utils.functional import cached_property
from django.utils.timezone import utc
@ -197,7 +198,21 @@ class Report(object):
with self.data_update as data:
data['simulate'] = simulate
def target():
@contextlib.contextmanager
def publik_provisionning():
managers = []
if 'hobo.agent.authentic2' in settings.INSTALLED_APPS and not simulate:
# provisionning is initialied in hobo.agent.authentic2.provisionning.apps
from hobo.agent.authentic2.provisionning import provisionning as engine
managers.append(engine)
with contextlib.nested(*managers):
yield None
# prevent the provisionning thread from outliving the import thread
engine.wait()
else:
yield None
def thread_worker():
from authentic2.csv_import import UserCsvImporter
with self.user_import.import_file as fd:
@ -208,10 +223,11 @@ class Report(object):
data['pid'] = os.getpid()
data['tid'] = gettid()
try:
importer.run(fd,
encoding=self.data['encoding'],
ou=self.data['ou'],
simulate=simulate)
with publik_provisionning():
importer.run(fd,
encoding=self.data['encoding'],
ou=self.data['ou'],
simulate=simulate)
except Exception as e:
logger.exception('error during report %s:%s run', self.user_import.uuid, self.uuid)
state = self.STATE_ERROR
@ -234,7 +250,7 @@ class Report(object):
data['exception'] = exception
data['importer'] = importer
data['duration'] = duration
t = threading.Thread(target=target)
t = threading.Thread(target=thread_worker)
t.daemon = True
if start:
t.start()