dvadmin3-celery 3.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_celery_results/__init__.py +36 -0
- django_celery_results/admin.py +83 -0
- django_celery_results/apps.py +15 -0
- django_celery_results/backends/__init__.py +4 -0
- django_celery_results/backends/cache.py +39 -0
- django_celery_results/backends/database.py +308 -0
- django_celery_results/managers.py +214 -0
- django_celery_results/migrations/__init__.py +0 -0
- django_celery_results/models.py +240 -0
- django_celery_results/urls.py +86 -0
- django_celery_results/utils.py +17 -0
- django_celery_results/views.py +53 -0
- dvadmin3_celery/__init__.py +0 -0
- dvadmin3_celery/admin.py +1 -0
- dvadmin3_celery/apps.py +7 -0
- dvadmin3_celery/fixtures/__init__.py +0 -0
- dvadmin3_celery/fixtures/init_menu.json +52 -0
- dvadmin3_celery/fixtures/initialize.py +29 -0
- dvadmin3_celery/settings.py +66 -0
- dvadmin3_celery/tasks.py +23 -0
- dvadmin3_celery/urls.py +27 -0
- dvadmin3_celery/views/__init__.py +0 -0
- dvadmin3_celery/views/crontab_schedule.py +24 -0
- dvadmin3_celery/views/interval_schedule.py +35 -0
- dvadmin3_celery/views/periodic_task.py +81 -0
- dvadmin3_celery/views/task.py +140 -0
- dvadmin3_celery/views/task_detail.py +48 -0
- dvadmin3_celery-3.1.3.dist-info/LICENSE +201 -0
- dvadmin3_celery-3.1.3.dist-info/METADATA +139 -0
- dvadmin3_celery-3.1.3.dist-info/RECORD +32 -0
- dvadmin3_celery-3.1.3.dist-info/WHEEL +5 -0
- dvadmin3_celery-3.1.3.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# 初始化
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import django
|
|
5
|
+
|
|
6
|
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "application.settings")
|
|
7
|
+
django.setup()
|
|
8
|
+
|
|
9
|
+
from dvadmin.system.fixtures.initSerializer import (
|
|
10
|
+
MenuInitSerializer
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
from dvadmin.utils.core_initialize import CoreInitialize
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Initialize(CoreInitialize):
|
|
17
|
+
|
|
18
|
+
def init_menu(self):
|
|
19
|
+
"""
|
|
20
|
+
初始化菜单信息
|
|
21
|
+
"""
|
|
22
|
+
self.init_base(MenuInitSerializer, unique_fields=['name', 'web_path', 'component', 'component_name'])
|
|
23
|
+
|
|
24
|
+
def run(self):
|
|
25
|
+
self.init_menu()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __name__ == "__main__":
|
|
29
|
+
Initialize(app='dvadmin3_celery').run()
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from application import settings
|
|
2
|
+
|
|
3
|
+
# ================================================= #
|
|
4
|
+
# ***************** 插件配置区开始 *******************
|
|
5
|
+
# ================================================= #
|
|
6
|
+
# 路由配置
|
|
7
|
+
plugins_url_patterns = [
|
|
8
|
+
{"re_path": r'api/dvadmin_celery/', "include": "dvadmin3_celery.urls"}
|
|
9
|
+
]
|
|
10
|
+
# app 配置
|
|
11
|
+
apps = ['django_celery_beat', 'django_celery_results', 'dvadmin3_celery']
|
|
12
|
+
# 租户模式中,public模式共享app配置
|
|
13
|
+
tenant_shared_apps = []
|
|
14
|
+
# ================================================= #
|
|
15
|
+
# ******************* 插件配置区结束 *****************
|
|
16
|
+
# ================================================= #
|
|
17
|
+
|
|
18
|
+
if not hasattr(settings, 'CACHES'):
|
|
19
|
+
_DEFAULT_CACHES = {
|
|
20
|
+
"default": {
|
|
21
|
+
"BACKEND": "django_redis.cache.RedisCache",
|
|
22
|
+
"LOCATION": f'{settings.REDIS_URL}/{getattr(settings, "REDIS_DB") or 1}',
|
|
23
|
+
"OPTIONS": {
|
|
24
|
+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
else:
|
|
29
|
+
_DEFAULT_CACHES = settings.CACHES
|
|
30
|
+
|
|
31
|
+
if not hasattr(settings, 'REDIS_URL'):
|
|
32
|
+
raise Exception("请配置redis地址,否则celery无法使用!")
|
|
33
|
+
|
|
34
|
+
# ********** 赋值到 settings 中 **********
|
|
35
|
+
settings.CACHES = _DEFAULT_CACHES
|
|
36
|
+
settings.INSTALLED_APPS += [app for app in apps if app not in settings.INSTALLED_APPS]
|
|
37
|
+
settings.TENANT_SHARED_APPS += tenant_shared_apps
|
|
38
|
+
# ********** celery 配置 **********
|
|
39
|
+
if not hasattr(settings, 'CELERY_BROKER_URL'):
|
|
40
|
+
settings.CELERY_BROKER_URL = f'{settings.REDIS_URL}/{getattr(settings, "CELERY_BROKER_DB") or 2}'
|
|
41
|
+
|
|
42
|
+
# ********** 执行结果保存位置 **********
|
|
43
|
+
if not hasattr(settings, 'CELERY_RESULT_BACKEND'):
|
|
44
|
+
settings.CELERY_RESULT_BACKEND = 'django-db'
|
|
45
|
+
if not hasattr(settings, 'CELERY_RESULT_EXTENDED'):
|
|
46
|
+
settings.CELERY_RESULT_EXTENDED = True
|
|
47
|
+
|
|
48
|
+
# ********** Backend数据库 **********
|
|
49
|
+
if not hasattr(settings, 'CELERY_BEAT_SCHEDULER'):
|
|
50
|
+
settings.CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
|
|
51
|
+
|
|
52
|
+
# ********** 注册路由 **********
|
|
53
|
+
settings.PLUGINS_URL_PATTERNS += plugins_url_patterns
|
|
54
|
+
|
|
55
|
+
# ********** Backend数据库 **********
|
|
56
|
+
if not hasattr(settings, 'CELERYBEAT_SCHEDULER'):
|
|
57
|
+
settings.CELERYBEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
|
|
58
|
+
# 避免时区的问题
|
|
59
|
+
CELERY_ENABLE_UTC = False
|
|
60
|
+
DJANGO_CELERY_BEAT_TZ_AWARE = False
|
|
61
|
+
# 避免celery启动时,连接redis失败
|
|
62
|
+
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True
|
|
63
|
+
# celery4版本的 默认使用 JSON 作为 serializer ,而 celery3 版本的默认使用 pickle。
|
|
64
|
+
CELERY_TASK_SERIALIZER = 'json'
|
|
65
|
+
CELERY_RESULT_SERIALIZER = 'json'
|
|
66
|
+
CELERY_ACCEPT_CONTENT = ['pickle', 'json']
|
dvadmin3_celery/tasks.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from celery import shared_task
|
|
2
|
+
|
|
3
|
+
from application.celery import app
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@app.task(acks_late=False)
|
|
7
|
+
def task__one(*args, **kwargs):
|
|
8
|
+
print(11111)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@app.task
|
|
12
|
+
def task__two(*args, **kwargs):
|
|
13
|
+
print(22222)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task
|
|
17
|
+
def task__three(*args, **kwargs):
|
|
18
|
+
print(33333)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@app.task
|
|
22
|
+
def task__four(*args, **kwargs):
|
|
23
|
+
print(44444)
|
dvadmin3_celery/urls.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from django.urls import path
|
|
3
|
+
from rest_framework import routers
|
|
4
|
+
|
|
5
|
+
from .views.crontab_schedule import CrontabScheduleModelViewSet
|
|
6
|
+
from .views.interval_schedule import IntervalScheduleModelViewSet
|
|
7
|
+
from .views.periodic_task import PeriodicTaskModelViewSet
|
|
8
|
+
from .views.task import CeleryTaskModelViewSet
|
|
9
|
+
from .views.task_detail import CeleryTaskDetailViewSet
|
|
10
|
+
|
|
11
|
+
router = routers.SimpleRouter()
|
|
12
|
+
# 调度间隔
|
|
13
|
+
router.register('intervalschedule', IntervalScheduleModelViewSet)
|
|
14
|
+
router.register('crontabschedule', CrontabScheduleModelViewSet)
|
|
15
|
+
router.register('periodictask', PeriodicTaskModelViewSet, basename='periodictasks')
|
|
16
|
+
router.register('task', CeleryTaskModelViewSet)
|
|
17
|
+
urlpatterns = [
|
|
18
|
+
path('tasks_as_choices/', PeriodicTaskModelViewSet.as_view({'get': 'tasks_as_choices'})),
|
|
19
|
+
path('operate_celery/', PeriodicTaskModelViewSet.as_view({'post': 'operate_celery'})),
|
|
20
|
+
|
|
21
|
+
path('task/job_list/',CeleryTaskModelViewSet.as_view({'get':'job_list'})),
|
|
22
|
+
path('task/update_status/<str:pk>/',CeleryTaskModelViewSet.as_view({'post':'update_status'})),
|
|
23
|
+
path('task/<str:pk>/delete_task/',CeleryTaskModelViewSet.as_view({'delete':'destroy'})), #注释编号: django-vue3-admin-urls202416:添加删除定时任务的URL
|
|
24
|
+
|
|
25
|
+
path('task_detail/',CeleryTaskDetailViewSet.as_view({'get':'list'})),
|
|
26
|
+
]
|
|
27
|
+
urlpatterns += router.urls
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from django_celery_beat.models import CrontabSchedule
|
|
2
|
+
|
|
3
|
+
from dvadmin.utils.serializers import CustomModelSerializer
|
|
4
|
+
from dvadmin.utils.viewset import CustomModelViewSet
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CrontabScheduleSerializer(CustomModelSerializer):
|
|
8
|
+
class Meta:
|
|
9
|
+
model = CrontabSchedule
|
|
10
|
+
exclude = ['timezone']
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CrontabScheduleModelViewSet(CustomModelViewSet):
|
|
14
|
+
"""
|
|
15
|
+
CrontabSchedule crontab调度模型 周期性时间定时器
|
|
16
|
+
minute 分钟
|
|
17
|
+
hour 小时
|
|
18
|
+
day_of_week 每周的周几
|
|
19
|
+
day_of_month 每月的某一天
|
|
20
|
+
month_of_year 每年的某一个月
|
|
21
|
+
|
|
22
|
+
"""
|
|
23
|
+
queryset = CrontabSchedule.objects.all()
|
|
24
|
+
serializer_class = CrontabScheduleSerializer
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from django_celery_beat.models import IntervalSchedule
|
|
2
|
+
from rest_framework import serializers
|
|
3
|
+
|
|
4
|
+
from dvadmin.utils.serializers import CustomModelSerializer
|
|
5
|
+
from dvadmin.utils.viewset import CustomModelViewSet
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class IntervalScheduleSerializer(CustomModelSerializer):
|
|
9
|
+
described = serializers.SerializerMethodField(read_only=True)
|
|
10
|
+
|
|
11
|
+
def get_described(self, instance):
|
|
12
|
+
period_dict = {
|
|
13
|
+
'days': '天',
|
|
14
|
+
'hours': '小时',
|
|
15
|
+
'minutes': '分钟',
|
|
16
|
+
'seconds': '秒',
|
|
17
|
+
'microseconds': '毫秒'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return f'每 {instance.every} {period_dict[instance.period]}执行一次'
|
|
21
|
+
|
|
22
|
+
class Meta:
|
|
23
|
+
model = IntervalSchedule
|
|
24
|
+
fields = '__all__'
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class IntervalScheduleModelViewSet(CustomModelViewSet):
|
|
28
|
+
"""
|
|
29
|
+
IntervalSchedule 调度间隔模型 间隔性时间定时器
|
|
30
|
+
every 次数
|
|
31
|
+
period 时间(天,小时,分钟,秒.毫秒)
|
|
32
|
+
"""
|
|
33
|
+
queryset = IntervalSchedule.objects.all()
|
|
34
|
+
serializer_class = IntervalScheduleSerializer
|
|
35
|
+
ordering = 'every' # 默认排序
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from django_celery_beat.models import PeriodicTask
|
|
2
|
+
from rest_framework import serializers
|
|
3
|
+
|
|
4
|
+
from dvadmin.utils.json_response import SuccessResponse, ErrorResponse
|
|
5
|
+
from dvadmin.utils.serializers import CustomModelSerializer
|
|
6
|
+
from dvadmin.utils.viewset import CustomModelViewSet
|
|
7
|
+
from ..views.crontab_schedule import CrontabScheduleSerializer
|
|
8
|
+
from ..views.interval_schedule import IntervalScheduleSerializer
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PeriodicTaskSerializer(CustomModelSerializer):
|
|
12
|
+
interval_list = serializers.SerializerMethodField(read_only=True)
|
|
13
|
+
crontab_list = serializers.SerializerMethodField(read_only=True)
|
|
14
|
+
|
|
15
|
+
def get_interval_list(self, obj):
|
|
16
|
+
return IntervalScheduleSerializer(obj.interval).data if obj.interval else {}
|
|
17
|
+
|
|
18
|
+
def get_crontab_list(self, obj):
|
|
19
|
+
return CrontabScheduleSerializer(obj.crontab).data if obj.crontab else {}
|
|
20
|
+
|
|
21
|
+
class Meta:
|
|
22
|
+
model = PeriodicTask
|
|
23
|
+
fields = '__all__'
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class PeriodicTaskModelViewSet(CustomModelViewSet):
|
|
27
|
+
"""
|
|
28
|
+
PeriodicTask celery 任务数据模型
|
|
29
|
+
name 名称
|
|
30
|
+
task celery任务名称
|
|
31
|
+
interval 频率
|
|
32
|
+
crontab 任务编排
|
|
33
|
+
args 形式参数
|
|
34
|
+
kwargs 位置参数
|
|
35
|
+
queue 队列名称
|
|
36
|
+
exchange 交换
|
|
37
|
+
routing_key 路由密钥
|
|
38
|
+
expires 过期时间
|
|
39
|
+
enabled 是否开启
|
|
40
|
+
|
|
41
|
+
"""
|
|
42
|
+
queryset = PeriodicTask.objects.exclude(name="celery.backend_cleanup")
|
|
43
|
+
serializer_class = PeriodicTaskSerializer
|
|
44
|
+
ordering = 'date_changed' # 默认排序
|
|
45
|
+
|
|
46
|
+
def tasks_as_choices(self, request, *args, **kwargs):
|
|
47
|
+
"""
|
|
48
|
+
获取所有 tasks 名称
|
|
49
|
+
:param request:
|
|
50
|
+
:param args:
|
|
51
|
+
:param kwargs:
|
|
52
|
+
:return:
|
|
53
|
+
"""
|
|
54
|
+
lis = []
|
|
55
|
+
|
|
56
|
+
def get_data(datas):
|
|
57
|
+
for item in datas:
|
|
58
|
+
if isinstance(item, (str, int)) and item:
|
|
59
|
+
lis.append(item)
|
|
60
|
+
else:
|
|
61
|
+
get_data(item)
|
|
62
|
+
|
|
63
|
+
from celery import current_app
|
|
64
|
+
tasks = list(sorted(name for name in current_app.tasks
|
|
65
|
+
if not name.startswith('celery.')))
|
|
66
|
+
get_data((('', ''),) + tuple(zip(tasks, tasks)))
|
|
67
|
+
return SuccessResponse(list(set(lis)))
|
|
68
|
+
|
|
69
|
+
def operate_celery(self, request, *args, **kwargs):
|
|
70
|
+
task_name = request.data.get('celery_name', '')
|
|
71
|
+
data = {
|
|
72
|
+
'task': None
|
|
73
|
+
}
|
|
74
|
+
test = f"""
|
|
75
|
+
from {'.'.join(task_name.split('.')[:-1])} import {task_name.split('.')[-1]}
|
|
76
|
+
task = {task_name.split('.')[-1]}.delay()
|
|
77
|
+
"""
|
|
78
|
+
exec(test, data)
|
|
79
|
+
if not data["task"]:
|
|
80
|
+
ErrorResponse(msg="执行失败")
|
|
81
|
+
return SuccessResponse({'task_id': data.get('task', ).id})
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from celery import current_app
|
|
4
|
+
from django_celery_beat.models import PeriodicTask, CrontabSchedule, cronexp
|
|
5
|
+
from django_celery_results.models import TaskResult
|
|
6
|
+
from rest_framework import serializers
|
|
7
|
+
from rest_framework.decorators import action
|
|
8
|
+
|
|
9
|
+
from dvadmin.utils.json_response import SuccessResponse
|
|
10
|
+
|
|
11
|
+
from dvadmin.utils.serializers import CustomModelSerializer
|
|
12
|
+
|
|
13
|
+
from dvadmin.utils.viewset import CustomModelViewSet
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_job_list():
|
|
17
|
+
from application import settings
|
|
18
|
+
task_list = []
|
|
19
|
+
task_dict_list = []
|
|
20
|
+
for app in settings.INSTALLED_APPS:
|
|
21
|
+
try:
|
|
22
|
+
exec(f"""
|
|
23
|
+
from {app} import tasks
|
|
24
|
+
for ele in [i for i in dir(tasks) if i.startswith('task__')]:
|
|
25
|
+
task_dict = dict()
|
|
26
|
+
task_dict['label'] = '{app}.tasks.' + ele
|
|
27
|
+
task_dict['value'] = '{app}.tasks.' + ele
|
|
28
|
+
task_list.append('{app}.tasks.' + ele)
|
|
29
|
+
task_dict_list.append(task_dict)
|
|
30
|
+
""")
|
|
31
|
+
except ImportError:
|
|
32
|
+
pass
|
|
33
|
+
return {'task_list': task_list, 'task_dict_list': task_dict_list}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class CeleryCrontabScheduleSerializer(CustomModelSerializer):
|
|
37
|
+
class Meta:
|
|
38
|
+
model = CrontabSchedule
|
|
39
|
+
exclude = ('timezone',)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class PeriodicTasksSerializer(CustomModelSerializer):
|
|
43
|
+
kwargs = serializers.SerializerMethodField(read_only=True)
|
|
44
|
+
cron = serializers.SerializerMethodField(read_only=True)
|
|
45
|
+
|
|
46
|
+
def get_kwargs(self, instance):
|
|
47
|
+
if not instance.kwargs:
|
|
48
|
+
return {}
|
|
49
|
+
return json.loads(instance.kwargs)
|
|
50
|
+
|
|
51
|
+
def get_cron(self, instance: PeriodicTask):
|
|
52
|
+
if not instance.crontab:
|
|
53
|
+
return ""
|
|
54
|
+
crontab = instance.crontab
|
|
55
|
+
return '{} {} {} {} {}'.format(
|
|
56
|
+
cronexp(crontab.minute), cronexp(crontab.hour),
|
|
57
|
+
cronexp(crontab.day_of_month), cronexp(crontab.month_of_year),
|
|
58
|
+
cronexp(crontab.day_of_week)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
class Meta:
|
|
62
|
+
model = PeriodicTask
|
|
63
|
+
fields = '__all__'
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class PeriodicTasksCreateSerializer(CustomModelSerializer):
|
|
67
|
+
|
|
68
|
+
def save(self, **kwargs):
|
|
69
|
+
cron = self.initial_data.get('cron', None)
|
|
70
|
+
description = self.initial_data.get('description', None)
|
|
71
|
+
cron_list = cron.split(' ')
|
|
72
|
+
if description:
|
|
73
|
+
crontab_schedule_obj = CrontabSchedule.objects.filter(id=description).first()
|
|
74
|
+
else:
|
|
75
|
+
crontab_schedule_obj = CrontabSchedule()
|
|
76
|
+
crontab_schedule_obj.minute = cron_list[0]
|
|
77
|
+
crontab_schedule_obj.hour = cron_list[1]
|
|
78
|
+
crontab_schedule_obj.day_of_month = cron_list[2]
|
|
79
|
+
crontab_schedule_obj.month_of_year = cron_list[3]
|
|
80
|
+
crontab_schedule_obj.day_of_week = cron_list[4]
|
|
81
|
+
crontab_schedule_obj.timezone = 'Asia/Shanghai'
|
|
82
|
+
crontab_schedule_obj.save()
|
|
83
|
+
try:
|
|
84
|
+
self.validated_data["kwargs"] = json.loads(self.request.data.get('kwargs', {}))
|
|
85
|
+
except:
|
|
86
|
+
pass
|
|
87
|
+
self.validated_data["crontab"] = crontab_schedule_obj
|
|
88
|
+
self.validated_data["description"] = crontab_schedule_obj.id
|
|
89
|
+
return super().save(**kwargs)
|
|
90
|
+
|
|
91
|
+
class Meta:
|
|
92
|
+
model = PeriodicTask
|
|
93
|
+
fields = '__all__'
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class CeleryTaskModelViewSet(CustomModelViewSet):
|
|
97
|
+
"""
|
|
98
|
+
CeleryTask 添加任务调度
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
queryset = PeriodicTask.objects.exclude(name="celery.backend_cleanup").order_by('-id')
|
|
102
|
+
serializer_class = PeriodicTasksSerializer
|
|
103
|
+
create_serializer_class = PeriodicTasksCreateSerializer
|
|
104
|
+
update_serializer_class = PeriodicTasksCreateSerializer
|
|
105
|
+
filter_fields = ['name', 'task', 'enabled']
|
|
106
|
+
extra_filter_class = []
|
|
107
|
+
|
|
108
|
+
def job_list(self, request, *args, **kwargs):
|
|
109
|
+
"""获取所有任务"""
|
|
110
|
+
result = get_job_list()
|
|
111
|
+
task_list = result.get('task_dict_list')
|
|
112
|
+
return SuccessResponse(msg='获取成功', data=task_list, total=len(task_list))
|
|
113
|
+
|
|
114
|
+
def destroy(self, request, *args, **kwargs):
|
|
115
|
+
"""删除定时任务"""
|
|
116
|
+
instance = self.get_object()
|
|
117
|
+
TaskResult.objects.filter(periodic_task_name=instance.name).delete()
|
|
118
|
+
# 删除任务 Crontab
|
|
119
|
+
if instance.description:
|
|
120
|
+
CrontabSchedule.objects.filter(id=instance.description).delete()
|
|
121
|
+
self.perform_destroy(instance)
|
|
122
|
+
return SuccessResponse(data=[], msg="删除成功")
|
|
123
|
+
|
|
124
|
+
@action(detail=True, methods=['post'])
|
|
125
|
+
def update_status(self, request, *args, **kwargs):
|
|
126
|
+
"""开始/暂停任务"""
|
|
127
|
+
instance = self.get_object()
|
|
128
|
+
body_data = request.data
|
|
129
|
+
instance.enabled = body_data.get('enabled')
|
|
130
|
+
instance.save()
|
|
131
|
+
return SuccessResponse(msg="修改成功", data=None)
|
|
132
|
+
|
|
133
|
+
@action(detail=True, methods=['post'])
|
|
134
|
+
def run_task(self, request, *args, **kwargs):
|
|
135
|
+
"""执行任务"""
|
|
136
|
+
instance = self.get_object()
|
|
137
|
+
task_kwargs = json.loads(instance.kwargs)
|
|
138
|
+
task_kwargs["periodic_task_name"] = instance.name
|
|
139
|
+
current_app.send_task(instance.task, kwargs=task_kwargs)
|
|
140
|
+
return SuccessResponse(msg="运行成功", data=None)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from django_celery_results.models import TaskResult
|
|
5
|
+
import django_filters
|
|
6
|
+
from rest_framework import serializers
|
|
7
|
+
|
|
8
|
+
from dvadmin.utils.serializers import CustomModelSerializer
|
|
9
|
+
from dvadmin.utils.viewset import CustomModelViewSet
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CeleryTaskDetailSerializer(CustomModelSerializer):
|
|
13
|
+
"""定时任务详情 序列化器"""
|
|
14
|
+
name = serializers.SerializerMethodField(read_only=True)
|
|
15
|
+
task_kwargs = serializers.SerializerMethodField(read_only=True)
|
|
16
|
+
result = serializers.SerializerMethodField(read_only=True)
|
|
17
|
+
|
|
18
|
+
def get_name(self, instance):
|
|
19
|
+
return instance.task_name
|
|
20
|
+
|
|
21
|
+
def get_task_kwargs(self, instance):
|
|
22
|
+
task_kwargs = instance.task_kwargs
|
|
23
|
+
if task_kwargs:
|
|
24
|
+
try:
|
|
25
|
+
task_kwargs = json.loads(json.loads(task_kwargs).replace("'", '"'))
|
|
26
|
+
except Exception as e:
|
|
27
|
+
pass
|
|
28
|
+
return task_kwargs
|
|
29
|
+
def get_result(self, instance):
|
|
30
|
+
result = instance.result
|
|
31
|
+
if result:
|
|
32
|
+
try:
|
|
33
|
+
result = json.loads(result)
|
|
34
|
+
except Exception as e:
|
|
35
|
+
pass
|
|
36
|
+
return result
|
|
37
|
+
|
|
38
|
+
class Meta:
|
|
39
|
+
model = TaskResult
|
|
40
|
+
fields = '__all__'
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class CeleryTaskDetailViewSet(CustomModelViewSet):
|
|
44
|
+
"""
|
|
45
|
+
定时任务详情
|
|
46
|
+
"""
|
|
47
|
+
queryset = TaskResult.objects.all()
|
|
48
|
+
serializer_class = CeleryTaskDetailSerializer
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|