0
Schedule recovery handler Failure
One script reply has been approved by the moderators Verified

NOTE: this script uses the Microsoft Teams workspace settings resource (EE only)

Created by alex308 34 days ago Viewed 4197 times
0
Submitted by alex308 Bun
Verified 34 days ago
1
import dayjs from 'dayjs'
2
import * as wmill from "windmill-client"
3

4
function formatError(error: any) {
5
  if (error.stack && error.name && error.message) {
6
    return `*${error.name}: ${error.message}*\`\`\`\n${error.stack}\n\`\`\``;
7
  } else {
8
    return `\`\`\`\n${JSON.stringify(error, null, 2)}\n\`\`\``;
9
  }
10
}
11

12
export async function main(
13
  path: string, // The path of the script or flow that errored
14
  is_flow: boolean, // Whether the runnable is a flow
15
  schedule_path: string, // The path of the schedule
16
  error: object, // The error details
17
  error_started_at: string, // The start datetime of the latest job that failed
18
  success_times: number, // The number of times the schedule succeeded before calling the recovery handler.
19
  success_result: object, // The result of the latest successful job
20
  success_started_at: string, // The start datetime of the latest successful job
21
  channel: string,
22
) {
23
  const baseUrl = process.env["WM_BASE_URL"];
24
  const scheduleUrl = baseUrl + "/runs?schedule_path=" +
25
    encodeURIComponent(schedule_path);
26
  const runnableUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
27
    path;
28

29
  const mdText = `*Schedule [${schedule_path}](${scheduleUrl}) recovered*${success_times > 1 ? (" " + success_times + " times in a row") : ""
30
    }:\n- ${is_flow ? "Flow" : "Script"}: [${path}](${runnableUrl})`
31

32
  const lastFailureText = `Last failure at: ${dayjs(error_started_at).format("DD.MM.YYYY HH:mm (Z)")}`
33
  const lastSuccessText = `Last success at: ${dayjs(success_started_at).format("DD.MM.YYYY HH:mm (Z)")}`
34

35
  console.log(lastSuccessText)
36
  await wmill.TeamsService.sendMessageToConversation(
37
    {
38
      requestBody: {
39
        conversation_id: channel,
40
        success: true,
41
        text: mdText,
42
        card_block: {
43
          "type": "message",
44
          "attachments": [
45
            {
46
              "contentType": "application/vnd.microsoft.card.adaptive",
47
              "content": {
48
                "type": "AdaptiveCard",
49
                "body": [
50
                  {
51
                    "type": "TextBlock",
52
                    "size": "Medium",
53
                    "weight": "Bolder",
54
                    "text": "Schedule Recovery Handler"
55
                  },
56
                  {
57
                    "type": "ColumnSet",
58
                    "columns": [
59
                      {
60
                        "type": "Column",
61
                        "items": [
62
                          {
63
                            "type": "Icon",
64
                            "name": "ServerPlay",
65
                            "size": "Medium",
66
                            "style": "Filled",
67
                            "color": "Good"
68
                          }
69
                        ],
70
                        "width": "auto"
71
                      },
72
                      {
73
                        "type": "Column",
74
                        "items": [
75
                          {
76
                            "type": "TextBlock",
77
                            "text": mdText,
78
                            "wrap": true
79
                          },
80
                          {
81
                            "type": "TextBlock",
82
                            "text": lastFailureText,
83
                            "wrap": true
84
                          },
85
                          {
86
                            "type": "CodeBlock",
87
                            "codeSnippet": formatError(error),
88
                            "language": "JavaScript"
89
                          },
90
                          {
91
                            "type": "TextBlock",
92
                            "text": lastSuccessText,
93
                            "wrap": true
94
                          },
95
                          {
96
                            "type": "CodeBlock",
97
                            "codeSnippet": success_result,
98
                            "language": "JavaScript"
99
                          },
100
                        ],
101
                        "width": "stretch"
102
                      }
103
                    ]
104
                  }
105
                ],
106
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
107
                "version": "1.6"
108
              },
109
            }
110
          ]
111
        }
112
      }
113
    }
114
  )
115
}