2021-02-05 05:29:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.db.models.signals import (
|
2023-02-09 12:48:25 +00:00
|
|
|
post_save, post_delete, m2m_changed
|
2021-02-05 05:29:29 +00:00
|
|
|
)
|
|
|
|
from django.dispatch import receiver
|
2023-02-09 12:48:25 +00:00
|
|
|
from django.utils.functional import lazy
|
2021-02-05 05:29:29 +00:00
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
from assets.models import Node, Asset
|
|
|
|
from common.decorators import merge_delay_run
|
2021-02-05 05:29:29 +00:00
|
|
|
from common.signals import django_ready
|
|
|
|
from common.utils import get_logger
|
2022-11-17 05:44:50 +00:00
|
|
|
from common.utils.connection import RedisPubSub
|
2021-04-12 08:35:03 +00:00
|
|
|
from orgs.models import Organization
|
2021-02-05 05:29:29 +00:00
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
logger = get_logger(__name__)
|
2022-11-17 05:44:50 +00:00
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
# clear node assets mapping for memory
|
|
|
|
# ------------------------------------
|
2023-02-09 12:48:25 +00:00
|
|
|
node_assets_mapping_pub_sub = lazy(lambda: RedisPubSub('fm.node_asset_mapping'), RedisPubSub)()
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
2024-01-02 08:11:56 +00:00
|
|
|
@merge_delay_run(ttl=30)
|
2023-02-13 11:14:00 +00:00
|
|
|
def expire_node_assets_mapping(org_ids=()):
|
2023-02-13 12:04:17 +00:00
|
|
|
logger.debug("Recv asset nodes changed signal, expire memery node asset mapping")
|
2021-02-05 05:29:29 +00:00
|
|
|
# 所有进程清除(自己的 memory 数据)
|
2021-04-12 08:35:03 +00:00
|
|
|
root_org_id = Organization.ROOT_ID
|
2023-02-09 12:48:25 +00:00
|
|
|
Node.expire_node_all_asset_ids_cache_mapping(root_org_id)
|
|
|
|
for org_id in set(org_ids):
|
|
|
|
org_id = str(org_id)
|
|
|
|
# 当前进程清除(cache 数据)
|
|
|
|
Node.expire_node_all_asset_ids_cache_mapping(org_id)
|
|
|
|
node_assets_mapping_pub_sub.publish(org_id)
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Node)
|
|
|
|
def on_node_post_create(sender, instance, created, update_fields, **kwargs):
|
|
|
|
if created:
|
|
|
|
need_expire = True
|
|
|
|
elif update_fields and 'key' in update_fields:
|
|
|
|
need_expire = True
|
|
|
|
else:
|
|
|
|
need_expire = False
|
|
|
|
|
|
|
|
if need_expire:
|
2024-01-23 08:45:59 +00:00
|
|
|
expire_node_assets_mapping.delay(org_ids=(instance.org_id,))
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Node)
|
|
|
|
def on_node_post_delete(sender, instance, **kwargs):
|
2024-01-23 08:45:59 +00:00
|
|
|
expire_node_assets_mapping.delay(org_ids=(instance.org_id,))
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Asset.nodes.through)
|
2024-01-02 08:11:56 +00:00
|
|
|
def on_node_asset_change(sender, instance, action='pre_remove', **kwargs):
|
|
|
|
if action.startswith('post'):
|
2024-01-23 08:45:59 +00:00
|
|
|
expire_node_assets_mapping.delay(org_ids=(instance.org_id,))
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
def subscribe_node_assets_mapping_expire(sender, **kwargs):
|
|
|
|
logger.debug("Start subscribe for expire node assets id mapping from memory")
|
|
|
|
|
2021-11-26 03:12:53 +00:00
|
|
|
def handle_node_relation_change(org_id):
|
|
|
|
root_org_id = Organization.ROOT_ID
|
2023-02-09 12:48:25 +00:00
|
|
|
Node.expire_node_all_asset_ids_memory_mapping(org_id)
|
|
|
|
Node.expire_node_all_asset_ids_memory_mapping(root_org_id)
|
2021-11-26 03:12:53 +00:00
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
node_assets_mapping_pub_sub.subscribe(handle_node_relation_change)
|