Skip to content

Commit

Permalink
fix: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kulak91 committed Oct 26, 2022
1 parent 5a1c41d commit d45ce2b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 68 deletions.
16 changes: 8 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
slack_url=https://hooks.slack.com/services/aaa/aaa/aaa
log=true
log=
error=true
kill=true
exception=false
restart=false
kill=
exception=
restart=
reload=true
delete=false
stop=false
exit=true
start=true
delete=
stop=true
exit=
start=
online=true
buffer=true
buffer_seconds=2
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const { parseIncommingLog, parseProcessName } = require('./utils');
* @property {boolean} exception
*/



// const process.env = pmx.initModule();

const slackUrlRouter = {
Expand Down Expand Up @@ -69,6 +67,7 @@ pm2.launchBus(function (err, bus) {

// Listen for process logs
if (process.env.log) {

bus.on('log:out', function (data) {
// if (data.process.name === 'pm2-slack-plus') { return; } // Ignore messages of own module.
const parsedLog = parseIncommingLog(data.data);
Expand Down Expand Up @@ -128,8 +127,10 @@ pm2.launchBus(function (err, bus) {

// Listen for PM2 events
bus.on('process:event', function (data) {
if (!process.env[data.event] && data.event !== 'restart overlimit') { return; } // This event type is disabled by configuration.
if (data.process.name === 'pm2-slack-plus') { return; } // Ignore messages of own module.


if (process.env[data.event] === 'false' && data.event !== 'restart overlimit') { return; }
// if (data.process.name === 'pm2-slack-plus') { return; } // Ignore messages of own module.

let description = null;
let interactive = [];
Expand Down
68 changes: 12 additions & 56 deletions slack-receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const { timeSince } = require('./utils');
const path = require('path');
const exec = require('shelljs').exec;

const confirmStopServer = require('./src/confirmation.json');
const helpMessage = require('./src/help-message.json');
const reloadButton = require('./src/button-reload.json');

// const pmx = require('pmx');
// const process.env = pmx.initModule();

Expand All @@ -23,45 +27,18 @@ const app = new App({


app.message('hi', async ({ message, say }) => {

await say(`Hey there <@${message.user}>!\nIf you want to see the list of available commands type in chat: "help"`);
});

app.message('help', async ({ message, say }) => {
await say(`*List of available commands:*\ntype 'list' - to see the list of PM2 processes\ntype 'emergency_stop' - to stop PM2 processes\ntype 'info_app' - to upload the lastest log file`);
app.message('help', async ({ say }) => {
await say(helpMessage);
});

app.message('emergency_stop', async ({ message, say }) => {
await say({
"text": "Are you sure you want to stop ecosystem.config file?",
"attachments": [
{
"text": "Please confirm",
"fallback": "Confirm stop",
"callback_id": "stop_ecosystem",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "game",
"text": "Thermonuclear War",
"style": "danger",
"type": "button",
"value": "stop_ecosystem_confirm",
"confirm": {
"title": "Are you sure?",
"text": "This will stop server process.",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
}
]
})
app.message('emergency_stop', async ({ say }) => {
await say(confirmStopServer)
});

app.message('list', async ({ message, say }) => {
app.message('list', async ({ say }) => {
const status = {
online: "\u{1F7E2}",
stopping: "\u{1F6AB}",
Expand Down Expand Up @@ -120,36 +97,16 @@ app.message('list', async ({ message, say }) => {
// answer.blocks.push()
// }
}
answer.blocks.push(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Force reload *ecosystem.config* : `
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Reload",
"emoji": true
},
"style": "primary",
"value": `reload`,
"action_id": `button-reload`
}
})
answer.blocks.push(reloadButton);
await say(answer)
}
);

app.action('button-reload', async ({ body, ack, say }) => {
await ack();

await say(`<@${body.user.id}> wants to restart ecosystem`);

if (!adminUsers.find(user => user === body.user.id)) {
await say(`But <@${body.user.name}> has no permissions to reload.`);
await say(`<@${body.user.name}> has no permissions to reload.`);
return;
}

Expand Down Expand Up @@ -193,11 +150,10 @@ app.action({ callback_id: 'stop_ecosystem' }, async ({ body, ack, say }) => {
const serverPath = path.resolve(response?.pm2_env?.pm_cwd);
const child = exec(`cd ${serverPath}; pm2 stop ecosystem.config.js`, { async: true });


await say('Process stopped.');
})

app.message('info_app', async ({ message, client, say, payload }) => {
app.message('info_app', async ({ message, client }) => {

try {
const date = new Date();
Expand Down
18 changes: 18 additions & 0 deletions src/button-reload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Force reload *ecosystem.config* : "
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Reload",
"emoji": true
},
"style": "primary",
"value": "reload",
"action_id": "button-reload"
}
}
27 changes: 27 additions & 0 deletions src/confirmation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"text": "Are you sure you want to stop ecosystem.config file?",
"attachments": [
{
"text": "Please confirm",
"fallback": "Confirm stop",
"callback_id": "stop_ecosystem",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "game",
"text": "Thermonuclear War",
"style": "danger",
"type": "button",
"value": "stop_ecosystem_confirm",
"confirm": {
"title": "Are you sure?",
"text": "This will stop server process.",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
}
]
}
22 changes: 22 additions & 0 deletions src/help-message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"text": "Commands available: list, emergency_stop, info_app",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "List of available commands"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*list* - display current PM2 processes\n*emergency_stop* - stop PM2 processess\n*info_app* - upload the lastest log file"
}
}
]
}

0 comments on commit d45ce2b

Please sign in to comment.