diff --git a/command.txt b/command.txt index 2426192..90525d4 100644 --- a/command.txt +++ b/command.txt @@ -1 +1,4 @@ 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 \ No newline at end of file diff --git a/dingxin_toolbox_drf/__init__.py b/dingxin_toolbox_drf/__init__.py index c45523b..8dd74d1 100644 --- a/dingxin_toolbox_drf/__init__.py +++ b/dingxin_toolbox_drf/__init__.py @@ -1,2 +1,6 @@ import pymysql -pymysql.install_as_MySQLdb() \ No newline at end of file +from .celery import app as celery_app + +pymysql.install_as_MySQLdb() + +__all__ = ('celery_app',) \ No newline at end of file diff --git a/dingxin_toolbox_drf/celery.py b/dingxin_toolbox_drf/celery.py new file mode 100644 index 0000000..50ddf56 --- /dev/null +++ b/dingxin_toolbox_drf/celery.py @@ -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, +# }, +# } diff --git a/dingxin_toolbox_drf/settings.py b/dingxin_toolbox_drf/settings.py index c85e740..8b5d511 100644 --- a/dingxin_toolbox_drf/settings.py +++ b/dingxin_toolbox_drf/settings.py @@ -145,7 +145,9 @@ INSTALLED_APPS = [ 'mock', 'dspt_api', 'product', - 'config' + 'config', + 'django_celery_beat', # 定时任务 + 'django_celery_results', # 定时任务 ] 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 \ No newline at end of file diff --git a/reqirement.txt b/reqirement.txt index 73b0d5f..0be823e 100644 Binary files a/reqirement.txt and b/reqirement.txt differ diff --git a/reqirement_linux.txt b/reqirement_linux.txt index 2607f73..28f094a 100644 Binary files a/reqirement_linux.txt and b/reqirement_linux.txt differ diff --git a/update/tasks.py b/update/tasks.py new file mode 100644 index 0000000..36201e5 --- /dev/null +++ b/update/tasks.py @@ -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 diff --git a/update/utils/client_util_custom.py b/update/utils/client_util_custom.py index 31594d8..32681d2 100644 --- a/update/utils/client_util_custom.py +++ b/update/utils/client_util_custom.py @@ -33,7 +33,7 @@ class ClientUtilCustom: def smb_disconnect(self): self.smb_conn.close() - async def sync_client_db(self): + def sync_client_db(self): # 获取本地数据库最大的同步ID self.deploy_id = ClientRelease.objects.all().aggregate(Max('deploy_id'))['deploy_id__max'] print('deploy_id', self.deploy_id) diff --git a/update/views.py b/update/views.py index 2c7ea96..d6ceb48 100644 --- a/update/views.py +++ b/update/views.py @@ -53,13 +53,13 @@ class CinemaViewSet(CacheResponseMixin, viewsets.ModelViewSet): # filter_fields = ('ip',) filterset_fields = ('ip',) GetVersion().main_process() # 修改数据model时需要注释调 - ClientUtilCustom().sync_client_db() + # ClientUtilCustom().sync_client_db() @action(methods=['get'], detail=False) @method_decorator(cache_page(60 * 1)) def refresh(self, request, *args, **kwargs): GetVersion().main_process() - ClientUtilCustom().sync_client_db() + # ClientUtilCustom().sync_client_db() queryset = Cinema.objects.filter(is_delete=False).all().order_by('ip') serializer = self.get_serializer(instance=queryset, many=True) return Response(serializer.data) @@ -113,8 +113,8 @@ def get_git_version(request): return JsonResponse(serializer.data, safe=False) -async def get_client(request): - client_data = await ClientUtilCustom().sync_client_db() +def get_client(request): + client_data = ClientUtilCustom().sync_client_db() return JsonResponse(client_data, safe=False) def download_client(request):