Skip to content

Commit 2e00016

Browse files
committed
Adds notification if listeners count falls too low
1 parent 1c6f460 commit 2e00016

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ it as a volume into your container, this script will also look to see if there
1010
is a stream that it can recreate from it. If it can, it will delete that existing
1111
stream from primary and recreate it.
1212

13+
Screammachine can also alert by checking the streamdash api URL and finding
14+
the listeners count. If the listeners count fall below a certain threshold,
15+
it will alert (see optional env variables below).
16+
1317
## requirements
1418

1519
* slack webhook url (https://api.slack.com/incoming-webhooks)
@@ -28,6 +32,12 @@ before you want this script to alert. (Example: 30)
2832
* `PREPEND_MESSAGE` what you'd like to prepend to the slack message. For example, if you want to `@channel` here or add
2933
some emoji's like `:hear_no_evil: :radio:`
3034

35+
**optional**
36+
37+
* `LOW_LISTENERS_THRESHOLD`
38+
* `STREAMDASH_HOURS_URL` the URL that your streamdash's hours api URL is at. Example: http://streamdash.com/api/hour
39+
40+
3141
This can be set up to run as a regular cronjob on a machine (`node index.js`),
3242
or set up as a container running as a kubernetes cronjob.
3343

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const SOURCE_URL = process.env.SOURCE_URL;
55
const SECONDS_OFFSET = process.env.SECONDS_OFFSET;
66
const WEBHOOK_URL = process.env.WEBHOOK_URL;
77
const PREPEND_MESSAGE = process.env.PREPEND_MESSAGE || ':cry:';
8+
const LOW_LISTENERS_THRESHOLD = process.env.LOW_LISTENERS_THRESHOLD;
9+
const STREAMDASH_HOURS_URL = process.env.STREAMDASH_HOURS_URL;
810

911
// JSON file that contains the body to POST to streammachine when creating streams.
1012
const streamsJson = require('./streams.json');
@@ -139,3 +141,26 @@ http.get(SOURCE_URL, (resp) => {
139141
notify(err.message);
140142
});
141143

144+
if (LOW_LISTENERS_THRESHOLD && STREAMDASH_HOURS_URL) {
145+
http.get(STREAMDASH_HOURS_URL, (resp) => {
146+
let data = '';
147+
148+
resp.on('data', (chunk) => {
149+
data += chunk;
150+
});
151+
152+
resp.on('end', () => {
153+
let notify_body = [];
154+
const body = JSON.parse(data);
155+
if (body === undefined) {
156+
return;
157+
}
158+
console.log(`listeners count was ${body.listens.listeners}`);
159+
if (body.listens.listeners < LOW_LISTENERS_THRESHOLD) {
160+
notify(`Listeners count (${body.listens.listeners}) fell below threshold (${LOW_LISTENERS_THRESHOLD}).` +
161+
`Something may be wrong with the stream. Check out Streamdash: ${STREAMDASH_HOURS_URL}`);
162+
}
163+
});
164+
});
165+
}
166+

0 commit comments

Comments
 (0)