diff --git a/MangoServer/PyAutoTest/auto_test/auto_system/apps.py b/MangoServer/PyAutoTest/auto_test/auto_system/apps.py index 454dabd3fec234d1b26215f80b94e57535746496..98ed016009c85bdb15b56751f547469380f6f68d 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 91179af09e5e05ee8b18d71fbe4e2b42097721fd..0000000000000000000000000000000000000000 --- 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 35% 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 987acb021a98931510129743f249305f700d4539..5a8631a0047e32501e5279a47cee20854f09a728 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(): + pass diff --git a/MangoServer/PyAutoTest/settings/__init__.py b/MangoServer/PyAutoTest/settings/__init__.py index c52de3c5062f3015012f4a8b09c8bded2853149b..4ab2a8181d6f0f2271814add4dbb040d9224c196 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 diff --git a/MangoServer/PyAutoTest/tools/decorator/error_response.py b/MangoServer/PyAutoTest/tools/decorator/error_response.py index 4aeca0c49ce128bf9f3b00ce2512303cb69f8449..2d2f675fdfdf8649700458014d84c4ec657cef1a 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