Edits history of script submission #3833 for ' Workspace or Schedule error handler (slack)'

  • deno
    import { WebClient } from "@slack/web-api";
    import dayjs from "dayjs";
    
    type Slack = {
      token: string;
    };
    
    function formatError(error: any) {
      if (error.stack && error.name && error.message) {
        return `*${error.name}: ${error.message}*\`\`\`\n${error.stack}\n\`\`\``;
      } else {
        return `\`\`\`\n${JSON.stringify(error, null, 2)}\n\`\`\``;
      }
    }
    
    export async function main(
      workspace_id: string,
      job_id: string, // The UUID of the job that errored
      path: string, // The path of the script or flow that errored
      is_flow: boolean, // Whether the runnable is a flow
      schedule_path: string, // The path of the schedule
      error: object, // The error details
      started_at: string, // The start datetime of the latest job that failed
      failed_times: number, // Minimum number of times the schedule failed before calling the error handler
      slack: Slack,
      channel: string,
    ) {
      const baseUrl = process.env["WM_BASE_URL"];
      const scheduleUrl = baseUrl + "/runs?schedule_path=" +
        encodeURIComponent(schedule_path) + "&workspace=" + encodeURIComponent(workspace_id);
      const scriptOrFlowRunUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
        path + "?workspace=" + encodeURIComponent(workspace_id);
      const web = new WebClient(slack.token);
      const jobRunUrl = baseUrl + "/run/" + job_id + "?workspace=" + encodeURIComponent(workspace_id);
    
      let rawText = ""
      if (schedule_path === undefined) {
        rawText = `${schedule_path != undefined ? `Schedule ${schedule_path} failed ` : ""}${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      } else {
        rawText = `Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}> failed ${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      }
    
      let mdText = ""
      if (schedule_path === undefined) {
        mdText = `*Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"} <${scriptOrFlowRunUrl}|${path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:`
      } else {
        mdText = `*Schedule <${scheduleUrl}|${schedule_path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:\n- Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}>`
      }
    
      await web.chat.postMessage({
        channel,
        text: rawText,
        blocks: [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": mdText,
            },
          },
        ],
        attachments: [
          {
            color: "#ff0000",
            "blocks": [{
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": `Last failure at: ${
                  dayjs(started_at).format("DD.MM.YYYY HH:mm (Z)")
                }\n${formatError(error)}`,
              },
            }],
          },
        ],
      });
    }
    

    Submitted by hugo697 626 days ago

  • deno
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    import dayjs from "npm:dayjs";
    
    type Slack = {
      token: string;
    };
    
    function formatError(error: any) {
      if (error.stack && error.name && error.message) {
        return `*${error.name}: ${error.message}*\`\`\`\n${error.stack}\n\`\`\``;
      } else {
        return `\`\`\`\n${JSON.stringify(error, null, 2)}\n\`\`\``;
      }
    }
    
    export async function main(
      workspace_id: string,
      job_id: string, // The UUID of the job that errored
      path: string, // The path of the script or flow that errored
      is_flow: boolean, // Whether the runnable is a flow
      schedule_path: string, // The path of the schedule
      error: object, // The error details
      started_at: string, // The start datetime of the latest job that failed
      failed_times: number, // Minimum number of times the schedule failed before calling the error handler
      slack: Slack,
      channel: string,
    ) {
      const baseUrl = Deno.env.get("WM_BASE_URL");
      const scheduleUrl = baseUrl + "/runs?schedule_path=" +
        encodeURIComponent(schedule_path) + "&workspace=" + encodeURIComponent(workspace_id);
      const scriptOrFlowRunUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
        path + "?workspace=" + encodeURIComponent(workspace_id);
      const web = new WebClient(slack.token);
      const jobRunUrl = baseUrl + "/run/" + job_id + "?workspace=" + encodeURIComponent(workspace_id);
    
      let rawText = ""
      if (schedule_path === undefined) {
        rawText = `${schedule_path != undefined ? `Schedule ${schedule_path} failed ` : ""}${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      } else {
        rawText = `Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}> failed ${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      }
    
      let mdText = ""
      if (schedule_path === undefined) {
        mdText = `*Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"} <${scriptOrFlowRunUrl}|${path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:`
      } else {
        mdText = `*Schedule <${scheduleUrl}|${schedule_path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:\n- Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}>`
      }
    
      await web.chat.postMessage({
        channel,
        text: rawText,
        blocks: [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": mdText,
            },
          },
        ],
        attachments: [
          {
            color: "#ff0000",
            "blocks": [{
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": `Last failure at: ${
                  dayjs(started_at).format("DD.MM.YYYY HH:mm (Z)")
                }\n${formatError(error)}`,
              },
            }],
          },
        ],
      });
    }
    

    Submitted by hugo697 935 days ago

  • deno
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    import dayjs from "npm:dayjs";
    
    type Slack = {
      token: string;
    };
    
    function formatError(error: any) {
      if (error.stack && error.name && error.message) {
        return `*${error.name}: ${error.message}*\`\`\`\n${error.stack}\n\`\`\``;
      } else {
        return `\`\`\`\n${JSON.stringify(error, null, 2)}\n\`\`\``;
      }
    }
    
    export async function main(
      workspace_id: string,
      job_id: string, // The UUID of the job that errored
      path: string, // The path of the script or flow that errored
      is_flow: boolean, // Whether the runnable is a flow
      schedule_path: string, // The path of the schedule
      error: object, // The error details
      started_at: string, // The start datetime of the latest job that failed
      failed_times: number, // Minimum number of times the schedule failed before calling the error handler
      slack: Slack,
      channel: string,
    ) {
      const baseUrl = Deno.env.get("WM_BASE_URL");
      const scheduleUrl = baseUrl + "/runs?schedule_path=" +
        encodeURIComponent(schedule_path) +  + "&workspace=" + encodeURIComponent(workspace_id);
      const scriptOrFlowRunUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
        path + "?workspace=" + encodeURIComponent(workspace_id);
      const web = new WebClient(slack.token);
      const jobRunUrl = baseUrl + "/run/" + job_id + "?workspace=" + encodeURIComponent(workspace_id);
    
      let rawText = ""
      if (schedule_path === undefined) {
        rawText = `${schedule_path != undefined ? `Schedule ${schedule_path} failed ` : ""}${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      } else {
        rawText = `Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}> failed ${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      }
    
      let mdText = ""
      if (schedule_path === undefined) {
        mdText = `*Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"} <${scriptOrFlowRunUrl}|${path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:`
      } else {
        mdText = `*Schedule <${scheduleUrl}|${schedule_path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:\n- Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}>`
      }
    
      await web.chat.postMessage({
        channel,
        text: rawText,
        blocks: [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": mdText,
            },
          },
        ],
        attachments: [
          {
            color: "#ff0000",
            "blocks": [{
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": `Last failure at: ${
                  dayjs(started_at).format("DD.MM.YYYY HH:mm (Z)")
                }\n${formatError(error)}`,
              },
            }],
          },
        ],
      });
    }
    

    Submitted by hugo697 935 days ago

  • deno
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    import dayjs from "npm:dayjs";
    
    type Slack = {
      token: string;
    };
    
    function formatError(error: any) {
      if (error.stack && error.name && error.message) {
        return `*${error.name}: ${error.message}*\`\`\`\n${error.stack}\n\`\`\``;
      } else {
        return `\`\`\`\n${JSON.stringify(error, null, 2)}\n\`\`\``;
      }
    }
    
    export async function main(
      job_id: string, // The UUID of the job that errored
      path: string, // The path of the script or flow that errored
      is_flow: boolean, // Whether the runnable is a flow
      schedule_path: string, // The path of the schedule
      error: object, // The error details
      started_at: string, // The start datetime of the latest job that failed
      failed_times: number, // Minimum number of times the schedule failed before calling the error handler
      slack: Slack,
      channel: string,
    ) {
      const baseUrl = Deno.env.get("WM_BASE_URL");
      const scheduleUrl = baseUrl + "/runs?schedule_path=" +
        encodeURIComponent(schedule_path);
      const scriptOrFlowRunUrl = baseUrl + (is_flow ? "/flows/get/" : "/scripts/get/") +
        path;
      const web = new WebClient(slack.token);
      const jobRunUrl = baseUrl + "/run/" + job_id;
    
      let rawText = ""
      if (schedule_path === undefined) {
        rawText = `${schedule_path != undefined ? `Schedule ${schedule_path} failed ` : ""}${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      } else {
        rawText = `Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}> failed ${failed_times > 1 ? (failed_times + " times in a row") : ""}`
      }
    
      let mdText = ""
      if (schedule_path === undefined) {
        mdText = `*Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"} <${scriptOrFlowRunUrl}|${path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:`
      } else {
        mdText = `*Schedule <${scheduleUrl}|${schedule_path}> failed*${failed_times > 1 ? (" " + failed_times + " times in a row") : ""}:\n- Run <${jobRunUrl}|${job_id}> of ${is_flow ? "flow" : "script"}: <${scriptOrFlowRunUrl}|${path}>`
      }
    
      await web.chat.postMessage({
        channel,
        text: rawText,
        blocks: [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": mdText,
            },
          },
        ],
        attachments: [
          {
            color: "#ff0000",
            "blocks": [{
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": `Last failure at: ${
                  dayjs(started_at).format("DD.MM.YYYY HH:mm (Z)")
                }\n${formatError(error)}`,
              },
            }],
          },
        ],
      });
    }
    

    Submitted by hugo697 941 days ago