provisionning: use uwsgi spooler (#55092)
gitea-wip/hobo/pipeline/head There was a failure building this commit Details
gitea/hobo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Emmanuel Cazenave 2021-06-29 11:35:00 +02:00
parent 739bffb78b
commit fc2886a583
3 changed files with 57 additions and 8 deletions

View File

@ -15,8 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import sys
from django.conf import settings
from django.db import connection
from django.http import HttpResponseBadRequest, HttpResponseForbidden, JsonResponse
from django.utils.deprecation import MiddlewareMixin
from django.utils.encoding import force_text
@ -50,15 +52,23 @@ class ProvisionningMiddleware(MiddlewareMixin, NotificationProcessing):
if not (object_type and action):
return HttpResponseBadRequest()
full = notification['full'] if 'full' in notification else False
data = notification['objects']['data']
for i in range(20):
try:
getattr(self, 'provision_' + object_type)(
issuer, action, notification['objects']['data'], full=full
)
except TryAgain:
continue
break
if 'uwsgi' in sys.modules:
from hobo.provisionning.spooler import provision
tenant = getattr(connection, 'tenant', None)
domain = getattr(tenant, 'domain_url', None)
provision.spool(
object_type=object_type,
domain=domain,
issuer=issuer,
action=action,
data=data,
full=full,
)
else:
self.provision(object_type=object_type, issuer=issuer, action=action, data=data, full=full)
return JsonResponse({'err': 0})

View File

@ -0,0 +1,30 @@
import logging
from django.db import connection
from uwsgidecorators import spool
import hobo.multitenant.settings_loaders # this will get imported via importlib but fail form some reason in a uwsgi job
from hobo.provisionning.utils import NotificationProcessing
def set_connection(domain):
from hobo.multitenant.middleware import TenantMiddleware
tenant = TenantMiddleware.get_tenant_by_hostname(domain)
connection.set_tenant(tenant)
@spool(pass_arguments=True)
def provision(*args, **kwargs):
try:
set_connection(kwargs['domain'])
NotificationProcessing.provision(
object_type=kwargs['object_type'],
issuer=kwargs['issuer'],
action=kwargs['action'],
data=kwargs['data'],
full=kwargs['full'],
)
except Exception:
# we nedd to catch every exceptions otherwise the task will be re scheduled for ever and ever
logging.getLogger(__name__).exception('provisionning failed')

View File

@ -184,3 +184,12 @@ class NotificationProcessing:
elif action == 'deprovision':
for role in Role.objects.filter(uuid__in=uuids):
role.delete()
@classmethod
def provision(cls, object_type, issuer, action, data, full):
for i in range(20):
try:
getattr(cls, 'provision_' + object_type)(issuer=issuer, action=action, data=data, full=full)
except TryAgain:
continue
break