Skip to content

Commit

Permalink
Send notification to Slack when agent comes online or offline
Browse files Browse the repository at this point in the history
  • Loading branch information
rushi committed Sep 3, 2024
1 parent afe3f74 commit 446ad66
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"slack": {
"token": "",
"signingSecret": ""
"signingSecret": "",
"defaultChannel": "C0712GGAN94"
},
"jira": {
"url": ""
Expand Down
24 changes: 24 additions & 0 deletions src/handlers/AgentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const Handler = require("./Handler");
const _ = require("lodash");
const chalk = require("chalk");
const dayjs = require("dayjs");
const config = require("config");
const util = require("util");
const AgentUpdatedNotification = require("../templates/AgentUpdatedNotification");

class AgentHandler extends Handler {
static shouldHandle(request) {
Expand All @@ -18,6 +20,7 @@ class AgentHandler extends Handler {
const { host_name, agent_config_state, agent_state, build_state } = body;
const now = dayjs().format("YYYY-MM-DD HH:mm");
if (!isDeployAgent) {
process.stdout.write(".");
return;
}

Expand All @@ -30,10 +33,14 @@ class AgentHandler extends Handler {
if (agent_state === "LostContact") {
AgentHandler.log(body);
console.log(chalk.bgRed.white(`[${now}] Deployment Agent lost contact`));
this.doNotify(body, `Agent **${host_name}** in ${agent_state} state`);
} else if (agent_state === "Idle" && build_state === "Idle") {
AgentHandler.log(body);
console.log(chalk.bgGreen.white(`[${now}] Deployment Agent came online?`));
this.doNotify(body, `**${host_name}** has come back online`);
}
} else {
this.doNotify(body, "Testing");
}

console.log(`[${now}] ${agentName} Status: ${configState} State: ${agentState} • Build State: ${buildState}`);
Expand All @@ -42,6 +49,23 @@ class AgentHandler extends Handler {
}
}

async doNotify(agent, text) {
const notification = new AgentUpdatedNotification(agent)?.toJSON(text);
try {
const response = await this.app.client.chat.postMessage({
text,
token: config.get("slack.token"),
channel: config.get("slack.defaultChannel"),
...notification,
});
console.log("Slack message sent", response?.ok, response?.message.text ?? response);
} catch (error) {
console.log("Error sending slack message", chalk.red(error.message));
console.log(notification);
}
process.exit(0);
}

static log(data, options = {}) {
console.log(
util.inspect(data, {
Expand Down
39 changes: 39 additions & 0 deletions src/templates/AgentUpdatedNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class AgentUpdatedNotification {
constructor(agent) {
this.agent = agent;
}

getColor() {
if (this.agent.agent_state === "LostContact") {
return "#ff5a5a"; // Yellow
}

if (this.agent.agent_state === "Idle" && this.agent.build_state === "Idle") {
return "#00FF7F"; // Green
}

if (this.agent.agent_state === "Building" || this.agent.build_state === "Building") {
return "#27ce70"; // Another green
}

return "#1352c6"; // Blue
}

toJSON(text) {
const { agent } = this;
const payload = {
attachments: [{
mrkdwn_in: ["text", "pretext"],
color: this.getColor(),
title: text ?? `Something changed with agent: ${agent.host_name}`,
text: `Agent State: ${agent.agent_state} Build State: ${agent.build_state}`,
title_link: `https://sage.ci.xola.com/go/agents/${agent.uuid}/job_run_history`,
footer: `Agent UUID: ${agent.uuid}`
}]
}

return payload;
}
}

module.exports = AgentUpdatedNotification;
4 changes: 2 additions & 2 deletions src/templates/PipelineFailedNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class PipelineFailedNotification {
fields.push({ title: `Failed Jobs (${failedJobs.length})`, value: message });
}

const emails = [`Commited By: ${pipeline.getCommitterEmail()}`];
const emails = [`Committed By: ${pipeline.getCommitterEmail()}`];
if (pipeline.getApprovedByEmail() && pipeline.getApprovedByEmail() !== pipeline.getCommitterEmail()) {
emails.push(`Trigerred by: ${pipeline.getApprovedByEmail()}`);
emails.push(`Triggered by: ${pipeline.getApprovedByEmail()}`);
}

let footer = `Status: ${pipeline.get("stage.result")}, well done`;
Expand Down

0 comments on commit 446ad66

Please sign in to comment.