Skip to content
This repository was archived by the owner on Mar 21, 2023. It is now read-only.

Commit 008af21

Browse files
committed
Polls Webhooks
Motivation: Chuck would continue attempting to send webhooks and logging every single one of them, causing stability issues due to the sheer number of logs being generated every second. This polls the webhooks every interval (5s default) set in the config to check if they're online. If they aren't online, the webhooks aren't sent, increasing stability.
1 parent 359bfa1 commit 008af21

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ SELECT name, type, data FROM rdmdb.instance;
9090
"enabled": false,
9191
"urls": ["http://127.0.0.1:9003"],
9292
"delay": 5,
93-
"retryCount": 5
93+
"retryCount": 5,
94+
"polling": 5
9495
},
9596
"logs": {
9697
"level": 4,

package-lock.json

Lines changed: 2 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configs/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
"http://127.0.0.1:9002"
6565
],
6666
"delay": 5,
67-
"retryCount": 5
67+
"retryCount": 5,
68+
"polling": 5
6869
},
6970
"logs": {
7071
"level": 4,

src/dataparser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ require('./services/logger.js');
7878

7979
if (config.webhooks.enabled && config.webhooks.urls.length > 0) {
8080
WebhookController.instance.start();
81+
WebhookController.instance.checkOnline();
8182
}
8283
}
8384
})();

src/services/webhook.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
'use strict';
22

33
const axios = require('axios');
4-
const config = require('./config.js');
4+
const { webhooks } = require('./config.js');
55

66
/**
77
* WebhookController relay class.
88
*/
99
class WebhookController {
10-
static instance = new WebhookController(config.webhooks.urls, config.webhooks.delay, config.webhooks.retryCount);
10+
static instance = new WebhookController(webhooks.urls, webhooks.delay, webhooks.retryCount, webhooks.polling);
1111

1212
/**
1313
* Initialize new WebhookController object.
1414
* @param {*} urls
1515
* @param {number} delay
1616
* @param {number} retryCount
1717
*/
18-
constructor(urls, delay = 5, retryCount) {
18+
constructor(urls, delay = 5, retryCount, polling) {
1919
console.info('[WebhookController] Starting up...');
2020
this.urls = urls;
2121
this.delay = delay;
2222
this.retryCount = retryCount;
23+
this.polling = polling;
24+
this.online = [];
2325
this.pokemonEvents = [];
2426
this.pokestopEvents = [];
2527
this.lureEvents = [];
@@ -37,6 +39,7 @@ class WebhookController {
3739
*/
3840
start() {
3941
this.timer = setInterval(() => this.loopEvents(), this.delay * 1000);
42+
setInterval(() => this.checkOnline(), this.polling * 1000);
4043
}
4144

4245
/**
@@ -52,7 +55,7 @@ class WebhookController {
5255
* @param {*} pokemon
5356
*/
5457
addPokemonEvent(pokemon) {
55-
if (!config.webhooks.enabled || this.urls.length === 0) {
58+
if (!webhooks.enabled || this.urls.length === 0) {
5659
return;
5760
}
5861
this.pokemonEvents.push(pokemon);
@@ -63,7 +66,7 @@ class WebhookController {
6366
* @param {*} pokestop
6467
*/
6568
addPokestopEvent(pokestop) {
66-
if (!config.webhooks.enabled || this.urls.length === 0) {
69+
if (!webhooks.enabled || this.urls.length === 0) {
6770
return;
6871
}
6972
this.pokestopEvents.push(pokestop);
@@ -74,7 +77,7 @@ class WebhookController {
7477
* @param {*} pokestop
7578
*/
7679
addLureEvent(pokestop) {
77-
if (!config.webhooks.enabled || this.urls.length === 0) {
80+
if (!webhooks.enabled || this.urls.length === 0) {
7881
return;
7982
}
8083
this.lureEvents.push(pokestop);
@@ -85,7 +88,7 @@ class WebhookController {
8588
* @param {*} pokestop
8689
*/
8790
addInvasionEvent(pokestop) {
88-
if (!config.webhooks.enabled || this.urls.length === 0) {
91+
if (!webhooks.enabled || this.urls.length === 0) {
8992
return;
9093
}
9194
this.invasionEvents.push(pokestop);
@@ -96,7 +99,7 @@ class WebhookController {
9699
* @param {*} pokestop
97100
*/
98101
addQuestEvent(pokestop) {
99-
if (!config.webhooks.enabled || this.urls.length === 0) {
102+
if (!webhooks.enabled || this.urls.length === 0) {
100103
return;
101104
}
102105
this.questEvents.push(pokestop);
@@ -107,7 +110,7 @@ class WebhookController {
107110
* @param {*} gym
108111
*/
109112
addGymEvent(gym) {
110-
if (!config.webhooks.enabled || this.urls.length === 0) {
113+
if (!webhooks.enabled || this.urls.length === 0) {
111114
return;
112115
}
113116
this.gymEvents.push(gym);
@@ -118,7 +121,7 @@ class WebhookController {
118121
* @param {*} gym
119122
*/
120123
addGymInfoEvent(gym) {
121-
if (!config.webhooks.enabled || this.urls.length === 0) {
124+
if (!webhooks.enabled || this.urls.length === 0) {
122125
return;
123126
}
124127
this.gymInfoEvents.push(gym);
@@ -129,7 +132,7 @@ class WebhookController {
129132
* @param {*} gym
130133
*/
131134
addEggEvent(gym) {
132-
if (!config.webhooks.enabled || this.urls.length === 0) {
135+
if (!webhooks.enabled || this.urls.length === 0) {
133136
return;
134137
}
135138
this.eggEvents.push(gym);
@@ -140,7 +143,7 @@ class WebhookController {
140143
* @param {*} gym
141144
*/
142145
addRaidEvent(gym) {
143-
if (!config.webhooks.enabled || this.urls.length === 0) {
146+
if (!webhooks.enabled || this.urls.length === 0) {
144147
return;
145148
}
146149
this.raidEvents.push(gym);
@@ -151,7 +154,7 @@ class WebhookController {
151154
* @param {*} weather
152155
*/
153156
addWeatherEvent(weather) {
154-
if (!config.webhooks.enabled || this.urls.length === 0) {
157+
if (!webhooks.enabled || this.urls.length === 0) {
155158
return;
156159
}
157160
this.weatherEvents.push(weather);
@@ -277,7 +280,7 @@ class WebhookController {
277280
*/
278281
sendEvents(events, url, retryCount) {
279282
// If events is not set, skip..
280-
if (!events) {
283+
if (!events || !this.online.includes(url)) {
281284
return;
282285
}
283286
// axios request options
@@ -298,14 +301,37 @@ class WebhookController {
298301
.catch(err => {
299302
if (err) {
300303
if (retryCount < this.retryCount) {
301-
console.error('[WebhookController] Error occurred, trying again:', err);
304+
console.error(`[WebhookController] Error occurred, trying again for ${url}`);
302305
this.sendEvents(events, url, retryCount++);
303306
} else {
304-
console.error('[WebhookController] Error occurred, max retry count reached:', err);
307+
console.error(`[WebhookController] Error occurred, max retry count reached for ${url}`);
305308
}
306309
}
307310
});
308311
}
312+
313+
checkOnline() {
314+
this.online = [];
315+
this.urls.forEach(url => {
316+
axios({
317+
url: url,
318+
method: 'POST',
319+
data: { pokemon_id: 0 },
320+
headers: {
321+
'Accept': 'application/json',
322+
'Content-Type': 'application/json',
323+
'Cache-Control': 'no-cache',
324+
'User-Agent': 'Nodedradamus',
325+
},
326+
})
327+
.then(() => this.online.push(url))
328+
.catch(err => {
329+
if (err) {
330+
console.error(`${url} is offline`);
331+
};
332+
});
333+
});
334+
};
309335
}
310336

311337
module.exports = WebhookController;

0 commit comments

Comments
 (0)