Skip to content

Commit d3f27f6

Browse files
authored
Per process custom configuration (#22)
New Feature: Custom options can be defined to each PM2 process.
1 parent a81ace7 commit d3f27f6

File tree

6 files changed

+439
-316
lines changed

6 files changed

+439
-316
lines changed

README.md

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ pm2 set pm2-slack:slack_url https://slack_url
1313

1414
To get the Slack URL, you need to setup an Incoming Webhook. More details on how to set this up can be found here: https://api.slack.com/incoming-webhooks
1515

16-
## Configure
16+
## Events subscription configuration
1717

1818
The following events can be subscribed to:
1919

20-
- log - All standard out logs from your processes. Default: false
21-
- error - All error logs from your processes. Default: true
22-
- kill - Event fired when PM2 is killed. Default: true
23-
- exception - Any exceptions from your processes. Default: true
24-
- restart - Event fired when a process is restarted. Default: false
25-
- reload - Event fired when a cluster is reloaded. Default: false
26-
- delete - Event fired when a process is removed from PM2. Default: false
27-
- stop - Event fired when a process is stopped. Default: false
28-
- restart overlimit - Event fired when a process is reaches the max amount of times it can restart. Default: true
29-
- exit - Event fired when a process is exited. Default: false
30-
- start - Event fired when a process is started. Default: false
31-
- online - Event fired when a process is online. Default: false
20+
- `log` - All standard out logs from your processes. Default: false
21+
- `error` - All error logs from your processes. Default: true
22+
- `kill` - Event fired when PM2 is killed. Default: true
23+
- `exception` - Any exceptions from your processes. Default: true
24+
- `restart` - Event fired when a process is restarted. Default: false
25+
- `reload` - Event fired when a cluster is reloaded. Default: false
26+
- `delete` - Event fired when a process is removed from PM2. Default: false
27+
- `stop` - Event fired when a process is stopped. Default: false
28+
- `restart overlimit` - Event fired when a process is reaches the max amount of times it can restart. Default: true
29+
- `exit` - Event fired when a process is exited. Default: false
30+
- `start` - Event fired when a process is started. Default: false
31+
- `online` - Event fired when a process is online. Default: false
3232

3333
You can simply turn these on and off by setting them to true or false using the PM2 set command.
3434

@@ -41,30 +41,72 @@ pm2 set pm2-slack:error false
4141

4242
The following options are available:
4343

44-
- username (string) - Set the username used in Slack for posting the message. By default this is the hostname of the server.
45-
- buffer (bool) - Enable/Disable buffering of messages. Messages that occur in short time will be concatenated together and posted as a single slack message. Default: true
46-
- buffer_seconds (int) - If buffering is enables, all messages are stored for this interval. If no new messages comes in this interval, buffered message(s) are sended to Slack. If new message comes in this interval, the "timer" will be reseted and buffer starts waiting for the new interval for a new next message. *Note: Puspose is reduction of push notifications on Slack clients.* Default: 2
47-
- buffer_max_seconds (int) - If time exceed this time, the buffered messages are always sent to Slack, even if new messages are still comming in interval (property `buffer_seconds`). Default: 20
48-
- queue_max (int) - Maximum number of messages, that can be send in one Slack message (in one bufferring round). When the queue exceeds this maximum, next messages are suppresesed and replaced with message "`Next XX messages have been suppressed.`". Default: 100
44+
- `slack_url` (string) - Slack Incomming Webhook URL.
45+
- `servername` / `username` (string) - Set the custom username for Slack messages (visible in message headers). Default: server hostname
46+
- `buffer` (bool) - Enable/Disable buffering of messages. Messages that occur in short time will be concatenated together and posted as a single slack message. Default: true
47+
- `buffer_seconds` (int) - If buffering is enables, all messages are stored for this interval. If no new messages comes in this interval, buffered message(s) are sended to Slack. If new message comes in this interval, the "timer" will be reseted and buffer starts waiting for the new interval for a new next message. *Note: Puspose is reduction of push notifications on Slack clients.* Default: 2
48+
- `buffer_max_seconds` (int) - If time exceed this time, the buffered messages are always sent to Slack, even if new messages are still comming in interval (property `buffer_seconds`). Default: 20
49+
- `queue_max` (int) - Maximum number of messages, that can be send in one Slack message (in one bufferring round). When the queue exceeds this maximum, next messages are suppresesed and replaced with message "*Next XX messages have been suppressed.*". Default: 100
4950

50-
Set these options in the same way you subscribe to events.
51+
Set these options in the same way as subscribing to events.
5152

52-
Example: The following configuration options will enable message buffering, and set the buffer duration to 5 seconds. All messages that occur within maximum 5 seconds delay between two neighboring messages will be concatenated into a single slack message.
53+
54+
###### Example
55+
56+
The following configuration options will enable message buffering, and set the buffer duration to 5 seconds. All messages that occur within maximum 5 seconds delay between two neighboring messages will be concatenated into a single slack message.
5357

5458
```
55-
pm2 set pm2-slack:username pm2-user
59+
pm2 set pm2-slack:slack_url https://hooks.slack.com/services/123456789/123456789/aaaaaaa
5660
pm2 set pm2-slack:buffer_seconds 5
5761
```
5862

5963
Note: In this example, the maximum total delay for messages is still 20 seconds (default value for `buffer_max_seconds`). After this time, the buffer will be flushed
6064
everytime and all messages will be sent.
6165

66+
### Process based custom options
67+
68+
By default, all options are defined for all processes globally.
69+
But you can separately define custom options to each PM2 process.
70+
Use format `pm2-slack:optionName-processName` to process based custom options.
71+
72+
If no custom options is defined, the global `pm2-slack:propertyName` will be used.
73+
74+
Note: By this way, all custom options can be used for specific process, but events subsciptions configuration is always global only.
75+
76+
###### Example
77+
78+
We have many processes, includes process `foo` and process `bar`.
79+
For this two processes will have to define separate Slack URL channel and separate server name.
80+
Same buffer options will be used for all processed.
81+
82+
```
83+
# Define global options for all processes.
84+
pm2 set pm2-slack:buffer_seconds 5
85+
86+
# Define global options for all processes.
87+
# (for process `foo` and `bar` the values will be overridden below).
88+
pm2 set pm2-slack:slack_url https://hooks.slack.com/services/123456789/123456789/aaaaaaa
89+
pm2 set pm2-slack:servername Orion
90+
91+
# Define custom Slack Incomming Webhoook for `foo` process.
92+
pm2 set pm2-slack:slack_url-foo https://hooks.slack.com/services/123456789/123456789/bbbbbbb
93+
pm2 set pm2-slack:servername-foo Foo-server
94+
# Note: The `pm2-slack:buffer_seconds`=5 will be used from global options for this process.
95+
96+
# Define custom Slack Incomming Webhoook for `bar` process
97+
pm2 set pm2-slack:slack_url-bar https://hooks.slack.com/services/123456789/123456789/ccccccc
98+
pm2 set pm2-slack:servername-foo Bar-server
99+
# Note: The `pm2-slack:buffer_seconds`=5 will be used from global options for this process.
100+
```
101+
62102

63103
## Contributing
64104

65105
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
66106

67107
## Release History
108+
- 1.1.0 Custom options can be defined to each PM2 process.
109+
Displaying process ID of cluster mode processes (thanks @abawchen).
68110
- 1.0.0 Message bufferring refactored. Message grouping refactored.
69111
Added datetime parsing from log messages.
70112
- 0.3.4 Added an option to override the Slack username

0 commit comments

Comments
 (0)