Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TECH - Envoi de notifications des jobs crons à Slack #2894

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 4 additions & 29 deletions back/src/scripts/handleCRONScript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { calculateDurationInSecondsFrom } from "shared";
import { AppConfig } from "../config/bootstrap/appConfig";
import { OpacifiedLogger, createLogger } from "../utils/logger";
import { notifyTeam } from "../utils/notifyTeam";
import { SentryInstance, configureSentry } from "./configureSentry";

export const handleCRONScript = async <
Expand Down Expand Up @@ -80,7 +80,8 @@ const onScriptSuccess =
monitorSlug: name,
duration: durationInSeconds,
});
return notifyDiscordPipelineReport(report).finally(() => process.exit(0));

return notifyTeam(report).finally(() => process.exit(0));
};

const onScriptError =
Expand Down Expand Up @@ -118,31 +119,5 @@ const onScriptError =
duration: durationInSeconds,
});

return notifyDiscordPipelineReport(report).finally(() => process.exit(1));
return notifyTeam(report).finally(() => process.exit(0));
};

const discordSizeLimit = 1950;
const notifyDiscordPipelineReport = async (rawContent: string) => {
const config = AppConfig.createFromEnv();
const discordPipelineReportsWebhookUrl: string | undefined =
config.discordPipelineReportsWebhookUrl;

if (!discordPipelineReportsWebhookUrl) return;

const content = rawContent.slice(0, discordSizeLimit);

// This is intentionaly not awaited following a fire and forget logic.

await axios.post(
discordPipelineReportsWebhookUrl,
{
username: `${config.envType} - ${config.immersionFacileBaseUrl}`,
content,
},
{
headers: {
"Content-Type": "application/json;charset=UTF-8",
},
},
);
};
20 changes: 9 additions & 11 deletions back/src/utils/notifyTeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ const logger = createLogger(__filename);

const discordSizeLimit = 1950;

export const notifyTeam = (rawContent: string) => {
notifyDiscord(rawContent);
notifySlack(rawContent);
export const notifyTeam = async (rawContent: string) => {
await notifyDiscord(rawContent);
await notifySlack(rawContent);
};

const notifyDiscord = (rawContent: string) => {
const notifyDiscord = async (rawContent: string) => {
const config = AppConfig.createFromEnv();
const discordWebhookUrl = config.discordWebhookUrl;

if (!discordWebhookUrl) return;
if (!discordWebhookUrl) return Promise.resolve();

const content = rawContent.slice(0, discordSizeLimit);

// This is intentionaly not awaited following a fire and forget logic.

axios
return axios
.post(
discordWebhookUrl,
{
Expand All @@ -43,13 +41,13 @@ const notifyDiscord = (rawContent: string) => {
});
};

const notifySlack = (rawContent: string) => {
const notifySlack = async (rawContent: string) => {
const config = AppConfig.createFromEnv();
const slackBotToken = config.slackBotToken;

if (!slackBotToken) return;
if (!slackBotToken) return Promise.resolve();

axios
return axios
.post(
"https://slack.com/api/chat.postMessage",
{
Expand Down
Loading