mirror of https://github.com/jumpserver/jumpserver
feat: celery 任务api
parent
2509b801c6
commit
e7a114a31d
|
@ -4,7 +4,7 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
from celery import current_app
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import ugettext as _
|
||||
from rest_framework import viewsets
|
||||
from celery.result import AsyncResult
|
||||
|
@ -98,13 +98,20 @@ class CeleryPeriodTaskViewSet(CommonApiMixin, viewsets.ModelViewSet):
|
|||
return queryset
|
||||
|
||||
|
||||
class CeleryTaskViewSet(CommonApiMixin, viewsets.ModelViewSet):
|
||||
class CeleryTaskViewSet(CommonApiMixin, viewsets.ReadOnlyModelViewSet):
|
||||
queryset = CeleryTask.objects.all()
|
||||
serializer_class = CeleryTaskSerializer
|
||||
http_method_names = ('get',)
|
||||
http_method_names = ('get', 'head', 'options',)
|
||||
|
||||
|
||||
class CeleryTaskExecutionViewSet(CommonApiMixin, viewsets.ModelViewSet):
|
||||
queryset = CeleryTaskExecution.objects.all()
|
||||
class CeleryTaskExecutionViewSet(CommonApiMixin, viewsets.ReadOnlyModelViewSet):
|
||||
serializer_class = CeleryTaskExecutionSerializer
|
||||
http_method_names = ('get',)
|
||||
http_method_names = ('get', 'head', 'options',)
|
||||
|
||||
def get_queryset(self):
|
||||
task_id = self.kwargs.get("task_pk")
|
||||
if task_id:
|
||||
task = CeleryTask.objects.get(pk=task_id)
|
||||
return CeleryTaskExecution.objects.filter(name=task.name)
|
||||
else:
|
||||
return CeleryTaskExecution.objects.none()
|
||||
|
|
|
@ -31,7 +31,7 @@ class CeleryTaskSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = CeleryTask
|
||||
fields = [
|
||||
'name', 'verbose_name', 'description',
|
||||
'id', 'name', 'verbose_name', 'description',
|
||||
]
|
||||
|
||||
|
||||
|
@ -39,5 +39,5 @@ class CeleryTaskExecutionSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = CeleryTaskExecution
|
||||
fields = [
|
||||
"name", "args", "kwargs", "state", "is_finished", "date_published", "date_start", "date_finished"
|
||||
"id", "name", "args", "kwargs", "state", "is_finished", "date_published", "date_start", "date_finished"
|
||||
]
|
||||
|
|
|
@ -20,9 +20,16 @@ TASK_LANG_CACHE_TTL = 1800
|
|||
@receiver(django_ready)
|
||||
def sync_registered_tasks(*args, **kwargs):
|
||||
with transaction.atomic():
|
||||
CeleryTask.objects.all().delete()
|
||||
for key in app.tasks:
|
||||
CeleryTask(name=key).save()
|
||||
db_tasks = CeleryTask.objects.all()
|
||||
celery_task_names = [key for key in app.tasks]
|
||||
db_task_names = [task.name for task in db_tasks]
|
||||
|
||||
for task in db_tasks:
|
||||
if task.name not in celery_task_names:
|
||||
task.delete()
|
||||
for task in celery_task_names:
|
||||
if task not in db_task_names:
|
||||
CeleryTask(name=task).save()
|
||||
|
||||
|
||||
@signals.before_task_publish.connect
|
||||
|
|
|
@ -4,6 +4,8 @@ from __future__ import unicode_literals
|
|||
from django.urls import path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from rest_framework_bulk.routes import BulkRouter
|
||||
from rest_framework_nested import routers
|
||||
|
||||
from .. import api
|
||||
|
||||
app_name = "ops"
|
||||
|
@ -14,15 +16,20 @@ bulk_router = BulkRouter()
|
|||
router.register(r'adhoc', api.AdHocViewSet, 'adhoc')
|
||||
router.register(r'adhoc-executions', api.AdHocExecutionViewSet, 'execution')
|
||||
router.register(r'celery/period-tasks', api.CeleryPeriodTaskViewSet, 'celery-period-task')
|
||||
router.register(r'celery/tasks', api.CeleryTaskViewSet, 'celery-task')
|
||||
router.register(r'celery/task-executions', api.CeleryTaskExecutionViewSet, 'task-execution')
|
||||
|
||||
router.register(r'tasks', api.CeleryTaskViewSet, 'task')
|
||||
|
||||
task_router = routers.NestedDefaultRouter(router, r'tasks', lookup='task')
|
||||
task_router.register(r'executions', api.CeleryTaskExecutionViewSet, 'task-execution')
|
||||
|
||||
urlpatterns = [
|
||||
path('celery/task/<uuid:pk>/log/', api.CeleryTaskExecutionLogApi.as_view(), name='celery-task-log'),
|
||||
path('celery/task/<uuid:pk>/result/', api.CeleryResultApi.as_view(), name='celery-result'),
|
||||
|
||||
path('ansible/task/<uuid:pk>/log/', api.AnsibleTaskLogApi.as_view(), name='ansible-task-log'),
|
||||
path('celery/task/<uuid:name>/task-execution/<uuid:pk>/log/', api.CeleryTaskExecutionLogApi.as_view(),
|
||||
name='celery-task-execution-log'),
|
||||
path('celery/task/<uuid:name>/task-execution/<uuid:pk>/result/', api.CeleryResultApi.as_view(),
|
||||
name='celery-task-execution-result'),
|
||||
|
||||
path('ansible/task-execution/<uuid:pk>/log/', api.AnsibleTaskLogApi.as_view(), name='ansible-task-log'),
|
||||
]
|
||||
|
||||
urlpatterns += router.urls
|
||||
urlpatterns += bulk_router.urls
|
||||
urlpatterns += (router.urls + bulk_router.urls + task_router.urls)
|
||||
|
|
Loading…
Reference in New Issue