Schedule recovery handler

Script slack Verified

by hugo697 ยท 8/24/2023

The script

Submitted by hugo697 Bun
Verified 4 days ago
1
import { WebClient } from "@slack/web-api";
2
import dayjs from "dayjs";
3

4
type Slack = {
5
  token: string;
6
};
7

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

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

35
  await web.chat.postMessage({
36
    channel,
37
    text: `Schedule ${schedule_path} recovered ${
38
      success_times > 1 ? (success_times + " times in a row") : ""
39
    }`,
40
    blocks: [
41
      {
42
        "type": "section",
43
        "text": {
44
          "type": "mrkdwn",
45
          "text": `*Schedule <${scheduleUrl}|${schedule_path}> recovered*${
46
            success_times > 1 ? (" " + success_times + " times in a row") : ""
47
          }:\n- ${is_flow ? "Flow" : "Script"}: <${runnableUrl}|${path}>`,
48
        },
49
      },
50
    ],
51
    attachments: [
52
      {
53
        "blocks": [
54
          {
55
            "type": "section",
56
            "text": {
57
              "type": "mrkdwn",
58
              "text": `Last failure at: ${
59
                dayjs(error_started_at).format("DD.MM.YYYY HH:mm (Z)")
60
              }\n${formatError(error)}`,
61
            },
62
          },
63
        ],
64
      },
65
      {
66
        color: "#00ff00",
67
        "blocks": [
68
          {
69
            "type": "section",
70
            "text": {
71
              "type": "mrkdwn",
72
              "text": `Last success at: ${
73
                dayjs(success_started_at).format("DD.MM.YYYY HH:mm (Z)")
74
              }\n\`\`\`\n${success_result}\n\`\`\``,
75
            },
76
          },
77
        ],
78
      },
79
    ],
80
  });
81
}
82

Other submissions
  • Submitted by hugo697 Deno
    Created 632 days ago
    1
    import { WebClient } from "@slack/web-api";
    2
    import dayjs from "dayjs";
    3
    
    
    4
    type Slack = {
    5
      token: string;
    6
    };
    7
    
    
    8
    function formatError(error: any) {
    9
      if (error.stack && error.name && error.message) {
    10
        return `*${error.name}: ${error.message}*\`\`\`\n${error.stack}\n\`\`\``;
    11
      } else {
    12
        return `\`\`\`\n${JSON.stringify(error, null, 2)}\n\`\`\``;
    13
      }
    14
    }
    15
    
    
    16
    export async function main(
    17
      path: string, // The path of the script or flow that errored
    18
      is_flow: boolean, // Whether the runnable is a flow
    19
      schedule_path: string, // The path of the schedule
    20
      error: object, // The error details
    21
      error_started_at: string, // The start datetime of the latest job that failed
    22
      success_times: number, // The number of times the schedule succeeded before calling the recovery handler.
    23
      success_result: object, // The result of the latest successful job
    24
      success_started_at: string, // The start datetime of the latest successful job
    25
      slack: Slack,
    26
      channel: string,
    27
    ) {
    28
      const baseUrl = process.env["WM_BASE_URL"];
    29
      const scheduleUrl = baseUrl + "/runs?schedule_path=" +
    30
        encodeURIComponent(schedule_path);
    31
      const runnableUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
    32
        path;
    33
      const web = new WebClient(slack.token);
    34
    
    
    35
      await web.chat.postMessage({
    36
        channel,
    37
        text: `Schedule ${schedule_path} recovered ${
    38
          success_times > 1 ? (success_times + " times in a row") : ""
    39
        }`,
    40
        blocks: [
    41
          {
    42
            "type": "section",
    43
            "text": {
    44
              "type": "mrkdwn",
    45
              "text": `*Schedule <${scheduleUrl}|${schedule_path}> recovered*${
    46
                success_times > 1 ? (" " + success_times + " times in a row") : ""
    47
              }:\n- ${is_flow ? "Flow" : "Script"}: <${runnableUrl}|${path}>`,
    48
            },
    49
          },
    50
        ],
    51
        attachments: [
    52
          {
    53
            "blocks": [
    54
              {
    55
                "type": "section",
    56
                "text": {
    57
                  "type": "mrkdwn",
    58
                  "text": `Last failure at: ${
    59
                    dayjs(error_started_at).format("DD.MM.YYYY HH:mm (Z)")
    60
                  }\n${formatError(error)}`,
    61
                },
    62
              },
    63
            ],
    64
          },
    65
          {
    66
            color: "#00ff00",
    67
            "blocks": [
    68
              {
    69
                "type": "section",
    70
                "text": {
    71
                  "type": "mrkdwn",
    72
                  "text": `Last success at: ${
    73
                    dayjs(success_started_at).format("DD.MM.YYYY HH:mm (Z)")
    74
                  }\n\`\`\`\n${JSON.stringify(success_result)}\n\`\`\``,
    75
                },
    76
              },
    77
            ],
    78
          },
    79
        ],
    80
      });
    81
    }
    82