From 2adc9e7ab781175fd9135078c0ceee102d6429cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E9=B9=8F?= <729164035@qq.com> Date: Wed, 31 Jul 2024 18:26:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PyAutoTest/auto_test/auto_system/apps.py | 51 +++++++++++-------- .../service/management/scan_table.py | 35 ------------- .../scan_table.py} | 7 +++ MangoServer/PyAutoTest/settings/__init__.py | 7 +-- 4 files changed, 39 insertions(+), 61 deletions(-) delete mode 100644 MangoServer/PyAutoTest/auto_test/auto_system/service/management/scan_table.py rename MangoServer/PyAutoTest/auto_test/auto_system/service/{management/__init__.py => scheduled_tasks/scan_table.py} (32%) diff --git a/MangoServer/PyAutoTest/auto_test/auto_system/apps.py b/MangoServer/PyAutoTest/auto_test/auto_system/apps.py index 454dabd3..98ed0160 100644 --- a/MangoServer/PyAutoTest/auto_test/auto_system/apps.py +++ b/MangoServer/PyAutoTest/auto_test/auto_system/apps.py @@ -3,15 +3,15 @@ # @Description: # @Time : 2023/1/17 10:20 # @Author : 毛鹏 - -import logging +import os import threading import time +from apscheduler.schedulers.background import BackgroundScheduler from django.apps import AppConfig from django.db import ProgrammingError, OperationalError -log = logging.getLogger('system') +from PyAutoTest.tools.log_collector import log class AutoSystemConfig(AppConfig): @@ -19,20 +19,31 @@ class AutoSystemConfig(AppConfig): name = 'PyAutoTest.auto_test.auto_system' def ready(self): - def delayed_task(): - time.sleep(10) - try: - from PyAutoTest.auto_test.auto_system.service.scheduled_tasks.tasks import Tasks - Tasks.create_jobs() - except OperationalError: - pass - except RuntimeError: - pass - except ProgrammingError: - log.error( - '请先迁移数据库再运行服务!!!如果正在迁移请忽略~') - raise Exception( - '请先迁移数据库再运行服务!!!如果正在迁移请忽略~') - - delayed_thread = threading.Thread(target=delayed_task) - delayed_thread.start() + def run(): + time.sleep(5) + self.minute_task() + self.delayed_task() + + if os.environ.get('RUN_MAIN', None) == 'true': + task1 = threading.Thread(target=run) + task1.start() + + @staticmethod + def minute_task(): + from PyAutoTest.auto_test.auto_system.service.scheduled_tasks.scan_table import mytask + sched = BackgroundScheduler() + sched.add_job(mytask, 'interval', seconds=60) + sched.start() + + @staticmethod + def delayed_task(): + try: + from PyAutoTest.auto_test.auto_system.service.scheduled_tasks.tasks import Tasks + Tasks.create_jobs() + except OperationalError: + pass + except RuntimeError: + pass + except ProgrammingError: + log.system.error('请先迁移数据库再运行服务!!!如果正在迁移请忽略~') + raise Exception('请先迁移数据库再运行服务!!!如果正在迁移请忽略~') diff --git a/MangoServer/PyAutoTest/auto_test/auto_system/service/management/scan_table.py b/MangoServer/PyAutoTest/auto_test/auto_system/service/management/scan_table.py deleted file mode 100644 index 91179af0..00000000 --- a/MangoServer/PyAutoTest/auto_test/auto_system/service/management/scan_table.py +++ /dev/null @@ -1,35 +0,0 @@ -# -*- coding: utf-8 -*- -# @Project: auto_test -# @Description: -# @Time : 2024-07-30 上午11:41 -# @Author : 毛鹏 -from django.core.management.base import BaseCommand -from django.utils import timezone -from PyAutoTest.auto_test.auto_ui.models import UiCase -import threading -from django.core.management.base import BaseCommand -from django.utils import timezone -from croniter import croniter -import threading - -def execute_task(cron_expression): - # 在这里执行你的任务 - print(f"执行任务: {cron_expression}") - -class Command(BaseCommand): - help = '每分钟检查 Cron 表达式' - - def handle(self, *args, **kwargs): - current_time = timezone.now() - records = UiCase.objects.all() # 获取所有记录 - - for record in records: - print(record.name) - # cron_expression = record.cron # 假设你的模型有一个 cron 字段 - # cron = croniter(cron_expression, current_time) - # - # # 检查当前时间是否符合 Cron 表达式 - # if cron.get_next() == current_time: - # threading.Thread(target=execute_task, args=(cron_expression,)).start() - - print(f"扫描时间: {current_time}") diff --git a/MangoServer/PyAutoTest/auto_test/auto_system/service/management/__init__.py b/MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py similarity index 32% rename from MangoServer/PyAutoTest/auto_test/auto_system/service/management/__init__.py rename to MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py index 987acb02..83e298e8 100644 --- a/MangoServer/PyAutoTest/auto_test/auto_system/service/management/__init__.py +++ b/MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py @@ -3,3 +3,10 @@ # @Description: # @Time : 2024-07-30 上午11:41 # @Author : 毛鹏 +from apscheduler.schedulers.background import BackgroundScheduler +import time +sched = BackgroundScheduler() + +@sched.scheduled_job('interval', seconds=30) # 表示间隔一分钟会执行函数 +def mytask(): + print(f'{time.time()}次要任务') diff --git a/MangoServer/PyAutoTest/settings/__init__.py b/MangoServer/PyAutoTest/settings/__init__.py index c52de3c5..4ab2a818 100644 --- a/MangoServer/PyAutoTest/settings/__init__.py +++ b/MangoServer/PyAutoTest/settings/__init__.py @@ -44,7 +44,6 @@ INSTALLED_APPS = [ 'rest_framework', # 前后端分离 'corsheaders', # 跨域 'channels', # 验证 - 'django_crontab', # 任务 ] MIDDLEWARE = [ @@ -268,8 +267,4 @@ CORS_ALLOW_METHODS = ( 'POST', 'PUT', 'VIEW', -) - -CRONJOBS = [ - ('* * * * *', 'PyAutoTest.auto_test.auto_system.service.management.scan_table'), # 替换为你的应用名 -] +) \ No newline at end of file -- Gitee From df2bc7efec3d754d65976b92f4c9bab67fe86d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E9=B9=8F?= <729164035@qq.com> Date: Thu, 1 Aug 2024 14:06:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/scheduled_tasks/scan_table.py | 2 +- .../tools/decorator/error_response.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py b/MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py index 83e298e8..5a8631a0 100644 --- a/MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py +++ b/MangoServer/PyAutoTest/auto_test/auto_system/service/scheduled_tasks/scan_table.py @@ -9,4 +9,4 @@ sched = BackgroundScheduler() @sched.scheduled_job('interval', seconds=30) # 表示间隔一分钟会执行函数 def mytask(): - print(f'{time.time()}次要任务') + pass diff --git a/MangoServer/PyAutoTest/tools/decorator/error_response.py b/MangoServer/PyAutoTest/tools/decorator/error_response.py index 4aeca0c4..2d2f675f 100644 --- a/MangoServer/PyAutoTest/tools/decorator/error_response.py +++ b/MangoServer/PyAutoTest/tools/decorator/error_response.py @@ -4,9 +4,11 @@ # @Time : 2024-07-25 上午9:55 # @Author : 毛鹏 import traceback +from datetime import datetime from rest_framework.request import Request +from PyAutoTest.auto_test.auto_system.service.notic_tools import NoticeMain from PyAutoTest.exceptions import MangoServerError from PyAutoTest.tools.log_collector import log from PyAutoTest.tools.view.error_msg import ERROR_MSG_0000 @@ -36,6 +38,23 @@ def error_response(app: str): except Exception as error: trace = traceback.format_exc() log_dict.get(app, log.system).error(f'错误内容:{error}-错误详情:{trace}') + content = f""" + 芒果测试平台管理员请注意查收: + 触发用户:{request.user.get('username')} + 触发时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} + 错误函数:{func.__name__} + 异常类型: {type(error)} + 错误提示: {str(error)} + 错误详情:{trace} + 参数list:{args} + 参数dict:{kwargs} + + ********************************** + 详细情况可前往芒果自动化平台查看,非相关负责人员可忽略此消息。谢谢! + + -----------芒果自动化平台 + """ + NoticeMain.mail_send(content) return ResponseData.fail(ERROR_MSG_0000) return wrapper -- Gitee