From e40d0c46d3ff34b2d1fefafaa66d4722bbd6edf6 Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Sat, 25 Jan 2025 11:02:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20cmsi=E5=8F=91=E9=80=81=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E5=88=86=E5=89=B2=20#9206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/core/notify/handlers.py | 3 ++ .../local_tasks/mysql_check_partition.py | 32 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/dbm-ui/backend/core/notify/handlers.py b/dbm-ui/backend/core/notify/handlers.py index f0aa6dc6ca..390e7398c6 100644 --- a/dbm-ui/backend/core/notify/handlers.py +++ b/dbm-ui/backend/core/notify/handlers.py @@ -24,6 +24,7 @@ from backend.core.notify.exceptions import NotifyBaseException from backend.core.notify.template import FAILED_TEMPLATE, FINISHED_TEMPLATE, TERMINATE_TEMPLATE, TODO_TEMPLATE from backend.db_meta.models import AppCache +from backend.env import DEFAULT_USERNAME from backend.exceptions import ApiResultError from backend.ticket.builders import BuilderFactory from backend.ticket.constants import TicketStatus, TicketType, TodoStatus @@ -215,6 +216,8 @@ def send_wecom_robot(self): "text": {"content": self.content}, "group_receiver": self.receivers, } + # 机器人发送,则receiver__username要置为用户名/admin。TODO: 应该支持填会话ID or 填空的 + self.receivers = [DEFAULT_USERNAME] self._cmsi_send_msg(MsgType.WECOM_ROBOT.value, sender=env.WECOM_ROBOT, wecom_robot=wecom_robot) def send_msg(self, msg_type, context): diff --git a/dbm-ui/backend/db_periodic_task/local_tasks/mysql_check_partition.py b/dbm-ui/backend/db_periodic_task/local_tasks/mysql_check_partition.py index 914dd9d467..1745c83d56 100644 --- a/dbm-ui/backend/db_periodic_task/local_tasks/mysql_check_partition.py +++ b/dbm-ui/backend/db_periodic_task/local_tasks/mysql_check_partition.py @@ -49,11 +49,13 @@ def mysql_check_partition(): return chat_ids = env.MYSQL_CHATID.split(",") if content: - title = _("【DBM】分区表异常 ") - content = _("【DBM】分区表异常情况 {} \n业务名称 bk_biz_id DB类型 失败/未执行 数量 DBA 策略ID\n{}").format( - datetime.date.today(), content - ) - CmsiHandler(title, content, chat_ids).send_wecom_robot() + cut_msgs = _cut_content(content) + for msg in cut_msgs: + title = _("【DBM】分区表异常 ") + partition_msg = _("【DBM】分区表异常情况 {} \n业务名称 bk_biz_id DB类型 失败/未执行 数量 DBA 策略ID\n{}").format( + datetime.date.today(), msg + ) + CmsiHandler(title, partition_msg, chat_ids).send_wecom_robot() return @@ -80,3 +82,23 @@ def _format_msg(logs: list, db_type: str, fail_type: str, content: str): ) ) return content + + +def _cut_content(content: str): + """ + 将content按照不超过2048字符进行分割,防止内容超限。 + 这里取1024长度作为界限 + """ + split_contents = content.split("\n") + max_len = 1024 + + contents = [] + current_content = "" + for index, msg in enumerate(split_contents): + if msg: + current_content += msg + "\n" + if len(current_content) > max_len or index == len(split_contents) - 1: + contents.append(current_content) + current_content = "" + + return contents