Commit c092acde authored by Lukas Burgey's avatar Lukas Burgey

Change RabbitMQInstance to a singleton

parent e6eb7c1a
......@@ -2,7 +2,7 @@
from rest_framework import generics, views, status
from rest_framework.authentication import TokenAuthentication
from rest_framework.response import Response
from ..models import default_rabbitmq_instance
from ..models import RabbitMQInstance
from .serializers import SiteSerializer, ServiceSerializer
# authentication class for the client api
......@@ -27,7 +27,7 @@ class ConfigurationView(generics.ListAPIView):
# we update the rabbitmq permission here, so the
# client can access all of his services, even new ones
default_rabbitmq_instance().update_site(site)
RabbitMQInstance.load().update_site(site)
return site.services.all()
......
......@@ -8,6 +8,7 @@ from requests.auth import HTTPBasicAuth
import pika
from django.conf import settings
from django.contrib.auth.models import AbstractUser, Group
from django.core.cache import cache
from django.db import models
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
......@@ -17,9 +18,34 @@ from .auth.v1.models import OIDCConfig
LOGGER = logging.getLogger(__name__)
# singleton for simple configs
# https://steelkiwi.com/blog/practical-application-singleton-design-pattern/
class SingletonModel(models.Model):
class Meta:
abstract = True
def set_cache(self):
cache.set(self.__class__.__name__, self)
# pylint: disable=invalid-name, arguments-differ
def save(self, *args, **kwargs):
self.pk = 1
super(SingletonModel, self).save(*args, **kwargs)
self.set_cache()
@classmethod
def load(cls):
if cache.get(cls.__name__) is None:
obj, created = cls.objects.get_or_create(pk=1)
if not created:
obj.set_cache()
return cache.get(cls.__name__)
# clients are registerred at rabbitmq, when they are assigned to a site
# (because we only then know what services they provide)
class RabbitMQInstance(models.Model):
class RabbitMQInstance(SingletonModel):
host = models.CharField(
max_length=150,
default='localhost',
......@@ -219,6 +245,7 @@ class RabbitMQInstance(models.Model):
if self.is_client_connected(site)]
def publish_by_service(self, service, msg):
# FIXME publish can fail -> catch error
return self.channel.basic_publish(
exchange=self.exchange,
routing_key=service.routing_key,
......@@ -229,10 +256,6 @@ class RabbitMQInstance(models.Model):
)
def default_rabbitmq_instance():
return RabbitMQInstance.objects.filter(is_active=True).first()
class User(AbstractUser):
TYPE_CHOICES = (
('apiclient', 'API-Client'),
......@@ -641,7 +664,7 @@ class DeploymentTask(models.Model):
msg = json.dumps(DeploymentTaskSerializer(self).data)
# FIXME select the rabbitmq instance more meaningful
default_rabbitmq_instance().publish_by_service(
RabbitMQInstance.load().publish_by_service(
self.service,
msg,
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment