From 0753afbd04601bec92a185ed133acb747d654dcb Mon Sep 17 00:00:00 2001 From: "Crane.z" <1481445951@qq.com> Date: Thu, 2 Jul 2026 16:43:19 +0800 Subject: [PATCH] fix:remove unused task log APIs --- apps/common/management/commands/check_api.py | 3 - apps/ops/api/celery.py | 73 ++------------------ apps/ops/serializers/celery.py | 10 +-- apps/ops/urls/api_urls.py | 7 -- 4 files changed, 6 insertions(+), 87 deletions(-) diff --git a/apps/common/management/commands/check_api.py b/apps/common/management/commands/check_api.py index 2d8d227b7..2c3667716 100644 --- a/apps/common/management/commands/check_api.py +++ b/apps/common/management/commands/check_api.py @@ -104,7 +104,6 @@ user_accessible_urls = known_unauth_urls + [ "/api/v1/authentication/passkeys/", "/api/v1/orgs/orgs/current/", "/api/v1/tickets/apply-asset-tickets/", - "/api/v1/ops/celery/task/00000000-0000-0000-0000-000000000000/task-execution/00000000-0000-0000-0000-000000000000/log/", "/api/v1/assets/favorite-assets/", "/api/v1/authentication/connection-token/", "/api/v1/ops/jobs/", @@ -116,13 +115,11 @@ user_accessible_urls = known_unauth_urls + [ "/api/v1/users/profile/permissions/", "/api/v1/tickets/apply-login-asset-tickets/", "/api/v1/resources/", - "/api/v1/ops/celery/task/00000000-0000-0000-0000-000000000000/task-execution/00000000-0000-0000-0000-000000000000/result/", "/api/v1/notifications/site-messages/", "/api/v1/notifications/site-messages/unread-total/", "/api/v1/assets/assets/suggestions/", "/api/v1/search/", "/api/v1/notifications/user-msg-subscription/", - "/api/v1/ops/ansible/job-execution/00000000-0000-0000-0000-000000000000/log/", "/api/v1/tickets/apply-login-tickets/", "/api/v1/ops/variables/form-data/", "/api/v1/ops/variables/help/", diff --git a/apps/ops/api/celery.py b/apps/ops/api/celery.py index fcb4d333f..65b972ce0 100644 --- a/apps/ops/api/celery.py +++ b/apps/ops/api/celery.py @@ -1,10 +1,8 @@ # -*- coding: utf-8 -*- # -import os import re from collections import defaultdict -from celery.result import AsyncResult from django.shortcuts import get_object_or_404 from django.utils.translation import gettext as _ from django_celery_beat.models import PeriodicTask @@ -12,84 +10,21 @@ from django_filters import rest_framework as drf_filters from rest_framework import generics, viewsets, mixins, status from rest_framework.response import Response -from common.api import LogTailApi, CommonApiMixin +from common.api import CommonApiMixin from common.drf.filters import BaseFilterSet from common.exceptions import JMSException -from common.permissions import IsValidUser from common.utils.timezone import local_now from ops.celery import app -from ..ansible.utils import get_ansible_task_log_path -from ..celery.utils import get_celery_task_log_path from ..models import CeleryTaskExecution, CeleryTask -from ..serializers import CeleryResultSerializer, CeleryPeriodTaskSerializer +from ..serializers import CeleryPeriodTaskSerializer from ..serializers.celery import CeleryTaskSerializer, CeleryTaskExecutionSerializer __all__ = [ - 'CeleryTaskExecutionLogApi', 'CeleryResultApi', 'CeleryPeriodTaskViewSet', - 'AnsibleTaskLogApi', 'CeleryTaskViewSet', 'CeleryTaskExecutionViewSet' + 'CeleryPeriodTaskViewSet', 'CeleryTaskViewSet', + 'CeleryTaskExecutionViewSet' ] -class CeleryTaskExecutionLogApi(LogTailApi): - permission_classes = (IsValidUser,) - task = None - task_id = '' - pattern = re.compile(r'Task .* succeeded in \d+\.\d+s.*') - - def get(self, request, *args, **kwargs): - self.task_id = str(kwargs.get('pk')) - self.task = AsyncResult(self.task_id) - return super().get(request, *args, **kwargs) - - def filter_line(self, line): - if self.pattern.match(line): - line = self.pattern.sub(line, '') - return line - - def get_log_path(self): - new_path = get_celery_task_log_path(self.task_id) - if new_path and os.path.isfile(new_path): - return new_path - try: - task = CeleryTaskExecution.objects.get(id=self.task_id) - except CeleryTaskExecution.DoesNotExist: - return None - return task.full_log_path - - def is_file_finish_write(self): - return self.task.ready() - - def get_no_file_message(self, request): - if self.mark == 'undefined': - return '.' - else: - return _('Waiting task start') - - -class AnsibleTaskLogApi(LogTailApi): - permission_classes = (IsValidUser,) - - def get_log_path(self): - new_path = get_ansible_task_log_path(self.kwargs.get('pk')) - if new_path and os.path.isfile(new_path): - return new_path - - def get_no_file_message(self, request): - if self.mark == 'undefined': - return '.' - else: - return _('Waiting task start') - - -class CeleryResultApi(generics.RetrieveAPIView): - permission_classes = (IsValidUser,) - serializer_class = CeleryResultSerializer - - def get_object(self): - pk = self.kwargs.get('pk') - return AsyncResult(str(pk)) - - class CeleryPeriodTaskViewSet(CommonApiMixin, viewsets.ModelViewSet): queryset = PeriodicTask.objects.all() serializer_class = CeleryPeriodTaskSerializer diff --git a/apps/ops/serializers/celery.py b/apps/ops/serializers/celery.py index 1ed7a2bc7..dabcaeed2 100644 --- a/apps/ops/serializers/celery.py +++ b/apps/ops/serializers/celery.py @@ -9,17 +9,11 @@ from ops.celery import app from ops.models import CeleryTask, CeleryTaskExecution __all__ = [ - 'CeleryResultSerializer', 'CeleryTaskExecutionSerializer', - 'CeleryPeriodTaskSerializer', 'CeleryTaskSerializer' + 'CeleryTaskExecutionSerializer', 'CeleryPeriodTaskSerializer', + 'CeleryTaskSerializer' ] -class CeleryResultSerializer(serializers.Serializer): - id = serializers.UUIDField() - result = serializers.JSONField() - state = serializers.CharField(max_length=16) - - class CeleryPeriodTaskSerializer(serializers.ModelSerializer): class Meta: model = PeriodicTask diff --git a/apps/ops/urls/api_urls.py b/apps/ops/urls/api_urls.py index 4cd182f4e..259f878d8 100644 --- a/apps/ops/urls/api_urls.py +++ b/apps/ops/urls/api_urls.py @@ -27,14 +27,7 @@ urlpatterns = [ path('variables/help/', api.JobRunVariableHelpAPIView.as_view(), name='variable-help'), path('job-execution/task-detail//', api.JobExecutionTaskDetail.as_view(), name='task-detail'), path('username-hints/', api.UsernameHintsAPI.as_view(), name='username-hints'), - path('ansible/job-execution//log/', api.AnsibleTaskLogApi.as_view(), name='job-execution-log'), path('classified-hosts/', api.ClassifiedHostsAPI.as_view(), name='classified-hosts'), - - path('celery/task//task-execution//log/', api.CeleryTaskExecutionLogApi.as_view(), - name='celery-task-execution-log'), - path('celery/task//task-execution//result/', api.CeleryResultApi.as_view(), - name='celery-task-execution-result'), - ] urlpatterns += (router.urls + bulk_router.urls)