provisionning: use uwsgi spooler (#55092)
This commit is contained in:
parent
78eea8656a
commit
4d2d6a05d1
|
@ -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})
|
||||
|
||||
|
|
|
@ -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 for 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 need to catch every exceptions otherwise the task will be re scheduled for ever and ever
|
||||
logging.getLogger(__name__).exception('provisionning failed')
|
|
@ -187,3 +187,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
|
||||
|
|
Loading…
Reference in New Issue