Procházet zdrojové kódy

Merge branch 'polishing' into owen-manage-station

Kristian Vos před 4 roky
rodič
revize
8e1b34cfd0

+ 3 - 2
backend/config/template.json

@@ -14,7 +14,8 @@
 		"youtube": {
 			"key": "",
 			"rateLimit": 500,
-			"requestTimeout": 5000
+			"requestTimeout": 5000,
+			"retryAmount": 2
 		},
 		"recaptcha": {
 			"secret": "",
@@ -90,5 +91,5 @@
 			]
 		}
 	},
-	"configVersion": 4
+	"configVersion": 5
 }

+ 1 - 1
backend/index.js

@@ -3,7 +3,7 @@ import "./loadEnvVariables.js";
 import util from "util";
 import config from "config";
 
-const REQUIRED_CONFIG_VERSION = 4;
+const REQUIRED_CONFIG_VERSION = 5;
 
 // eslint-disable-next-line
 Array.prototype.remove = function (item) {

+ 63 - 8
backend/logic/youtube.js

@@ -1,6 +1,7 @@
 import async from "async";
 import config from "config";
 
+import * as rax from "retry-axios";
 import axios from "axios";
 
 import CoreClass from "../core";
@@ -61,6 +62,14 @@ class _YouTubeModule extends CoreClass {
 			this.rateLimiter = new RateLimitter(config.get("apis.youtube.rateLimit"));
 			this.requestTimeout = config.get("apis.youtube.requestTimeout");
 
+			this.axios = axios.create();
+			this.axios.defaults.raxConfig = {
+				instance: this.axios,
+				retry: config.get("apis.youtube.retryAmount"),
+				noResponseRetries: config.get("apis.youtube.retryAmount")
+			};
+			rax.attach(this.axios);
+
 			resolve();
 		});
 	}
@@ -87,8 +96,20 @@ class _YouTubeModule extends CoreClass {
 		return new Promise((resolve, reject) =>
 			YouTubeModule.rateLimiter.continue().then(() => {
 				YouTubeModule.rateLimiter.restart();
-				axios
-					.get("https://www.googleapis.com/youtube/v3/search", { params })
+				YouTubeModule.axios
+					.get("https://www.googleapis.com/youtube/v3/search", {
+						params,
+						raxConfig: {
+							onRetryAttempt: err => {
+								const cfg = rax.getConfig(err);
+								YouTubeModule.log(
+									"ERROR",
+									"SEARCH",
+									`Attempt #${cfg.currentRetryAttempt}. Error: ${err.message}`
+								);
+							}
+						}
+					})
 					.then(res => {
 						if (res.data.err) {
 							YouTubeModule.log("ERROR", "SEARCH", `${res.data.error.message}`);
@@ -124,10 +145,20 @@ class _YouTubeModule extends CoreClass {
 
 			YouTubeModule.rateLimiter.continue().then(() => {
 				YouTubeModule.rateLimiter.restart();
-				axios
+				YouTubeModule.axios
 					.get("https://www.googleapis.com/youtube/v3/videos", {
 						params,
-						timeout: YouTubeModule.requestTimeout
+						timeout: YouTubeModule.requestTimeout,
+						raxConfig: {
+							onRetryAttempt: err => {
+								const cfg = rax.getConfig(err);
+								YouTubeModule.log(
+									"ERROR",
+									"GET_SONG",
+									`Attempt #${cfg.currentRetryAttempt}. Error: ${err.message}`
+								);
+							}
+						}
 					})
 					.then(res => {
 						if (res.data.error) {
@@ -176,6 +207,7 @@ class _YouTubeModule extends CoreClass {
 						return resolve({ song });
 					})
 					.catch(err => {
+						// console.log(111, err, payload);
 						YouTubeModule.log("ERROR", "GET_SONG", `${err.message}`);
 						return reject(new Error("An error has occured. Please try again later."));
 					});
@@ -248,6 +280,7 @@ class _YouTubeModule extends CoreClass {
 					}
 				],
 				(err, response) => {
+					// console.log(222, err, payload);
 					if (err && err !== true) {
 						YouTubeModule.log("ERROR", "GET_PLAYLIST", "Some error has occurred.", err.message);
 						reject(new Error(err.message));
@@ -281,10 +314,20 @@ class _YouTubeModule extends CoreClass {
 
 			YouTubeModule.rateLimiter.continue().then(() => {
 				YouTubeModule.rateLimiter.restart();
-				axios
+				YouTubeModule.axios
 					.get("https://www.googleapis.com/youtube/v3/playlistItems", {
 						params,
-						timeout: YouTubeModule.requestTimeout
+						timeout: YouTubeModule.requestTimeout,
+						raxConfig: {
+							onRetryAttempt: err => {
+								const cfg = rax.getConfig(err);
+								YouTubeModule.log(
+									"ERROR",
+									"GET_PLAYLIST_PAGE",
+									`Attempt #${cfg.currentRetryAttempt}. Error: ${err.message}`
+								);
+							}
+						}
 					})
 					.then(res => {
 						if (res.data.err) {
@@ -299,6 +342,7 @@ class _YouTubeModule extends CoreClass {
 						return resolve({ songs });
 					})
 					.catch(err => {
+						// console.log(333, err, payload);
 						YouTubeModule.log("ERROR", "GET_PLAYLIST_PAGE", `${err.message}`);
 						if (err.message === "Request failed with status code 404") {
 							return reject(new Error("Playlist not found. Is the playlist public/unlisted?"));
@@ -338,10 +382,20 @@ class _YouTubeModule extends CoreClass {
 
 			return YouTubeModule.rateLimiter.continue().then(() => {
 				YouTubeModule.rateLimiter.restart();
-				axios
+				YouTubeModule.axios
 					.get("https://www.googleapis.com/youtube/v3/videos", {
 						params,
-						timeout: YouTubeModule.requestTimeout
+						timeout: YouTubeModule.requestTimeout,
+						raxConfig: {
+							onRetryAttempt: err => {
+								const cfg = rax.getConfig(err);
+								YouTubeModule.log(
+									"ERROR",
+									"FILTER_MUSIC_VIDEOS",
+									`Attempt #${cfg.currentRetryAttempt}. Error: ${err.message}`
+								);
+							}
+						}
 					})
 					.then(res => {
 						if (res.data.err) {
@@ -368,6 +422,7 @@ class _YouTubeModule extends CoreClass {
 							.catch(err => reject(err));
 					})
 					.catch(err => {
+						// console.log(444, err, payload);
 						YouTubeModule.log("ERROR", "FILTER_MUSIC_VIDEOS", `${err.message}`);
 						return reject(new Error("Failed to find playlist from YouTube"));
 					});

+ 1 - 0
backend/package.json

@@ -17,6 +17,7 @@
   "dependencies": {
     "async": "3.1.0",
     "axios": "^0.21.1",
+    "retry-axios": "^2.4.0",
     "bcrypt": "^5.0.0",
     "bluebird": "^3.5.5",
     "body-parser": "^1.19.0",