Skip to content

Commit

Permalink
fix(backend): cmsi发送内容分割 #9206
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Jan 25, 2025
1 parent 691e94f commit e40d0c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions dbm-ui/backend/core/notify/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

0 comments on commit e40d0c4

Please sign in to comment.