From 4424c4bde2bea4630faf69203366924a180f25dd Mon Sep 17 00:00:00 2001 From: xinwen Date: Thu, 10 Dec 2020 17:47:35 +0800 Subject: [PATCH] =?UTF-8?q?perf(asset):=20=E6=89=8B=E5=8A=A8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E8=8A=82=E7=82=B9=E8=B5=84=E4=BA=A7=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E8=87=AA=E6=A3=80=E7=A8=8B=E5=BA=8F=E6=97=B6=E5=8C=BA=E5=88=86?= =?UTF-8?q?=E7=BB=84=E7=BB=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 5 +++-- apps/assets/tasks/nodes_amount.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index b1e01c9c1..924529ce1 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -21,7 +21,8 @@ from common.const.distributed_lock_key import UPDATE_NODE_TREE_LOCK_KEY from orgs.mixins.api import OrgModelViewSet from orgs.mixins import generics from orgs.lock import org_level_transaction_lock -from assets.tasks import check_node_assets_amount_period_task +from orgs.utils import current_org +from assets.tasks import check_node_assets_amount_task from ..hands import IsOrgAdmin from ..models import Node from ..tasks import ( @@ -51,7 +52,7 @@ class NodeViewSet(OrgModelViewSet): @action(methods=[POST], detail=False, url_name='launch-check-assets-amount-task') def launch_check_assets_amount_task(self, request): - task = check_node_assets_amount_period_task.delay() + task = check_node_assets_amount_task.delay(current_org.id) return Response(data={'task': task.id}) # 仅支持根节点指直接创建,子节点下的节点需要通过children接口创建 diff --git a/apps/assets/tasks/nodes_amount.py b/apps/assets/tasks/nodes_amount.py index cd929d131..e1e437797 100644 --- a/apps/assets/tasks/nodes_amount.py +++ b/apps/assets/tasks/nodes_amount.py @@ -1,6 +1,8 @@ from celery import shared_task from django.utils.translation import gettext_lazy as _ +from orgs.models import Organization +from orgs.utils import tmp_to_org from ops.celery.decorator import register_as_period_task from assets.utils import check_node_assets_amount @@ -11,9 +13,15 @@ logger = get_logger(__file__) @shared_task(queue='celery_heavy_tasks') -@register_as_period_task(crontab='0 2 * * *') -def check_node_assets_amount_period_task(): +def check_node_assets_amount_task(org_id=Organization.ROOT_ID): try: - check_node_assets_amount() + with tmp_to_org(Organization.get_instance(org_id)): + check_node_assets_amount() except AcquireFailed: logger.error(_('The task of self-checking is already running and cannot be started repeatedly')) + + +@register_as_period_task(crontab='0 2 * * *') +@shared_task(queue='celery_heavy_tasks') +def check_node_assets_amount_period_task(): + check_node_assets_amount_task()