2017-12-29 18:29:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-09-22 10:47:16 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
|
|
|
|
|
|
|
from common.signals import django_ready
|
|
|
|
from django.dispatch import receiver
|
|
|
|
from common.utils import get_logger
|
|
|
|
from .models import Application
|
|
|
|
from .utils import db_port_manager, DBPortManager
|
|
|
|
|
|
|
|
db_port_manager: DBPortManager
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
def init_db_port_mapper(sender, **kwargs):
|
|
|
|
logger.info('Init db port mapper')
|
|
|
|
db_port_manager.init()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Application)
|
|
|
|
def on_db_app_created(sender, instance: Application, created, **kwargs):
|
|
|
|
if not instance.category_db:
|
|
|
|
return
|
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
db_port_manager.add(instance)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Application)
|
|
|
|
def on_db_app_delete(sender, instance, **kwargs):
|
|
|
|
if not instance.category_db:
|
|
|
|
return
|
|
|
|
db_port_manager.pop(instance)
|