utils.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from libs.utils import human_datetime
  5. from libs.spug import Notification
  6. from threading import Thread
  7. import json
  8. def send_fail_notify(task, msg=None):
  9. rst_notify = json.loads(task.rst_notify)
  10. mode = rst_notify.get('mode')
  11. url = rst_notify.get('value')
  12. if mode != '0' and url:
  13. Thread(target=_do_notify, args=(task, mode, url, msg)).start()
  14. def _do_notify(task, mode, url, msg):
  15. if mode == '1':
  16. texts = [
  17. '## <font color="#f90202">任务执行失败通知</font> ## ',
  18. f'**任务名称:** {task.name} ',
  19. f'**任务类型:** {task.type} ',
  20. f'**描述信息:** {msg or "请在任务计划执行历史中查看详情"} ',
  21. f'**发生时间:** {human_datetime()} ',
  22. '> 来自 Spug运维平台'
  23. ]
  24. data = {
  25. 'msgtype': 'markdown',
  26. 'markdown': {
  27. 'title': '任务执行失败通知',
  28. 'text': '\n\n'.join(texts)
  29. },
  30. 'at': {
  31. 'isAtAll': True
  32. }
  33. }
  34. Notification.handle_request(url, data, 'dd')
  35. elif mode == '2':
  36. data = {
  37. 'task_id': task.id,
  38. 'task_name': task.name,
  39. 'task_type': task.type,
  40. 'message': msg or '请在任务计划执行历史中查看详情',
  41. 'created_at': human_datetime()
  42. }
  43. Notification.handle_request(url, data)
  44. elif mode == '3':
  45. texts = [
  46. '## <font color="warning">任务执行失败通知</font>',
  47. f'任务名称: {task.name}',
  48. f'任务类型: {task.type}',
  49. f'描述信息: {msg or "请在任务计划执行历史中查看详情"}',
  50. f'发生时间: {human_datetime()}',
  51. '> 来自 Spug运维平台'
  52. ]
  53. data = {
  54. 'msgtype': 'markdown',
  55. 'markdown': {
  56. 'content': '\n'.join(texts)
  57. }
  58. }
  59. Notification.handle_request(url, data, 'wx')
  60. elif mode == '4':
  61. data = {
  62. 'msg_type': 'post',
  63. 'content': {
  64. 'post': {
  65. 'zh_cn': {
  66. 'title': '任务执行失败通知',
  67. 'content': [
  68. [{'tag': 'text', 'text': f'任务名称: {task.name}'}],
  69. [{'tag': 'text', 'text': f'任务类型: {task.type}'}],
  70. [{'tag': 'text', 'text': f'描述信息: {msg or "请在任务计划执行历史中查看详情"}'}],
  71. [{'tag': 'text', 'text': f'发生时间: {human_datetime()}'}],
  72. [{'tag': 'at', 'user_id': 'all'}],
  73. ]
  74. }
  75. }
  76. }
  77. }
  78. Notification.handle_request(url, data, 'fs')