2018-12-10 02:11:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
import os
|
2019-02-13 07:55:11 +00:00
|
|
|
import re
|
|
|
|
|
2019-02-20 09:51:53 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2019-12-05 07:09:25 +00:00
|
|
|
from rest_framework import viewsets
|
2018-12-10 02:11:54 +00:00
|
|
|
from celery.result import AsyncResult
|
|
|
|
from rest_framework import generics
|
2019-12-05 07:09:25 +00:00
|
|
|
from django_celery_beat.models import PeriodicTask
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
from common.permissions import IsValidUser
|
2019-01-15 02:23:30 +00:00
|
|
|
from common.api import LogTailApi
|
2018-12-10 02:11:54 +00:00
|
|
|
from ..models import CeleryTask
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..serializers import CeleryResultSerializer, CeleryPeriodTaskSerializer
|
2019-01-15 02:23:30 +00:00
|
|
|
from ..celery.utils import get_celery_task_log_path
|
2020-12-29 13:05:18 +00:00
|
|
|
from ..ansible.utils import get_ansible_task_log_path
|
2019-12-05 07:09:25 +00:00
|
|
|
from common.mixins.api import CommonApiMixin
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
__all__ = [
|
|
|
|
'CeleryTaskLogApi', 'CeleryResultApi', 'CeleryPeriodTaskViewSet',
|
|
|
|
'AnsibleTaskLogApi',
|
|
|
|
]
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
class CeleryTaskLogApi(LogTailApi):
|
2018-12-10 02:11:54 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
2019-01-15 02:23:30 +00:00
|
|
|
task = None
|
|
|
|
task_id = ''
|
2019-02-13 07:55:11 +00:00
|
|
|
pattern = re.compile(r'Task .* succeeded in \d+\.\d+s.*')
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-01-15 02:23:30 +00:00
|
|
|
self.task_id = str(kwargs.get('pk'))
|
|
|
|
self.task = AsyncResult(self.task_id)
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
2019-02-13 07:55:11 +00:00
|
|
|
def filter_line(self, line):
|
|
|
|
if self.pattern.match(line):
|
|
|
|
line = self.pattern.sub(line, '')
|
|
|
|
return line
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
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 = CeleryTask.objects.get(id=self.task_id)
|
|
|
|
except CeleryTask.DoesNotExist:
|
|
|
|
return None
|
|
|
|
return task.full_log_path
|
|
|
|
|
|
|
|
def is_file_finish_write(self):
|
|
|
|
return self.task.ready()
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2019-02-20 09:51:53 +00:00
|
|
|
def get_no_file_message(self, request):
|
|
|
|
if self.mark == 'undefined':
|
|
|
|
return '.'
|
|
|
|
else:
|
|
|
|
return _('Waiting task start')
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
class CeleryResultApi(generics.RetrieveAPIView):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
serializer_class = CeleryResultSerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
return AsyncResult(pk)
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
class CeleryPeriodTaskViewSet(CommonApiMixin, viewsets.ModelViewSet):
|
|
|
|
queryset = PeriodicTask.objects.all()
|
|
|
|
serializer_class = CeleryPeriodTaskSerializer
|
|
|
|
http_method_names = ('get', 'head', 'options', 'patch')
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.exclude(description='')
|
|
|
|
return queryset
|