增加定时任务功能

main
RogerWork 3 days ago
parent c2d11a6d94
commit 6a43412406
  1. 3
      command.txt
  2. 4
      dingxin_toolbox_drf/__init__.py
  3. 23
      dingxin_toolbox_drf/celery.py
  4. 17
      dingxin_toolbox_drf/settings.py
  5. BIN
      reqirement.txt
  6. BIN
      reqirement_linux.txt
  7. 13
      update/tasks.py
  8. 2
      update/utils/client_util_custom.py
  9. 8
      update/views.py

@ -1 +1,4 @@
py .\manage.py runserver 0.0.0.0:8000 py .\manage.py runserver 0.0.0.0:8000
celery -A dingxin_toolbox_drf worker -l info -P solo # windows
celery -A dingxin_toolbox_drf worker -l info # linux
celery -A dingxin_toolbox_drf beat -l info

@ -1,2 +1,6 @@
import pymysql import pymysql
from .celery import app as celery_app
pymysql.install_as_MySQLdb() pymysql.install_as_MySQLdb()
__all__ = ('celery_app',)

@ -0,0 +1,23 @@
import os
from celery import Celery
from celery.schedules import crontab
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dingxin_toolbox_drf.settings')
# 创建 Celery实例
app = Celery('dingxin_toolbox_drf')
# 加载配置文件中的Celery配置
app.config_from_object('django.conf:settings', namespace='CELERY')
# 自动发现并添加任务
app.autodiscover_tasks()
# app.conf.beat_schedule = {
# 'update_client_task': {
# 'task': 'update.tasks.update_client',
# # 'schedule': crontab(hour='*'),
# 'schedule': 30,
# },
# }

@ -145,7 +145,9 @@ INSTALLED_APPS = [
'mock', 'mock',
'dspt_api', 'dspt_api',
'product', 'product',
'config' 'config',
'django_celery_beat', # 定时任务
'django_celery_results', # 定时任务
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -328,3 +330,16 @@ CHANNEL_LAYERS = {
} }
} }
} }
# Celery 定时任务
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERY_BROKER_URL = f"redis://{CONFIG[ENV]['REDIS_IP']}:6379/0"
CELERY_RESULT_BACKEND = "django-db"
CELERY_TIMEZONE = "Asia/Shanghai"
CELERY_ENABLE_UTC = False
CELERY_RESULT_EXTENDED = True # 启用后才会记录 task_name、date_started 等字段
CELERY_TASK_TRACK_STARTED = True # 记录任务开始时间
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_RESULT_EXPIRES = 3600

Binary file not shown.

Binary file not shown.

@ -0,0 +1,13 @@
from celery import shared_task
import datetime
from update.utils.client_util_custom import ClientUtilCustom
@shared_task
def update_client():
ClientUtilCustom().sync_client_db()
# with open(r'D:\Code\Work\Python\dingxin_toolbox_drf\update\tasks.txt', 'w', encoding='utf-8') as f:
# f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# f.close()
print("task success")
return True

@ -33,7 +33,7 @@ class ClientUtilCustom:
def smb_disconnect(self): def smb_disconnect(self):
self.smb_conn.close() self.smb_conn.close()
async def sync_client_db(self): def sync_client_db(self):
# 获取本地数据库最大的同步ID # 获取本地数据库最大的同步ID
self.deploy_id = ClientRelease.objects.all().aggregate(Max('deploy_id'))['deploy_id__max'] self.deploy_id = ClientRelease.objects.all().aggregate(Max('deploy_id'))['deploy_id__max']
print('deploy_id', self.deploy_id) print('deploy_id', self.deploy_id)

@ -53,13 +53,13 @@ class CinemaViewSet(CacheResponseMixin, viewsets.ModelViewSet):
# filter_fields = ('ip',) # filter_fields = ('ip',)
filterset_fields = ('ip',) filterset_fields = ('ip',)
GetVersion().main_process() # 修改数据model时需要注释调 GetVersion().main_process() # 修改数据model时需要注释调
ClientUtilCustom().sync_client_db() # ClientUtilCustom().sync_client_db()
@action(methods=['get'], detail=False) @action(methods=['get'], detail=False)
@method_decorator(cache_page(60 * 1)) @method_decorator(cache_page(60 * 1))
def refresh(self, request, *args, **kwargs): def refresh(self, request, *args, **kwargs):
GetVersion().main_process() GetVersion().main_process()
ClientUtilCustom().sync_client_db() # ClientUtilCustom().sync_client_db()
queryset = Cinema.objects.filter(is_delete=False).all().order_by('ip') queryset = Cinema.objects.filter(is_delete=False).all().order_by('ip')
serializer = self.get_serializer(instance=queryset, many=True) serializer = self.get_serializer(instance=queryset, many=True)
return Response(serializer.data) return Response(serializer.data)
@ -113,8 +113,8 @@ def get_git_version(request):
return JsonResponse(serializer.data, safe=False) return JsonResponse(serializer.data, safe=False)
async def get_client(request): def get_client(request):
client_data = await ClientUtilCustom().sync_client_db() client_data = ClientUtilCustom().sync_client_db()
return JsonResponse(client_data, safe=False) return JsonResponse(client_data, safe=False)
def download_client(request): def download_client(request):

Loading…
Cancel
Save