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()