Przeglądaj źródła

fix(backend): some functions were holding up the job queue

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 lat temu
rodzic
commit
02eff38cb2

+ 3 - 7
backend/logic/actions/queueSongs.js

@@ -43,6 +43,7 @@ CacheModule.runJob("SUB", {
 		const queueSongModel = await DBModule.runJob("GET_MODEL", {
 			modelName: "queueSong"
 		});
+
 		queueSongModel.findOne({ _id: songId }, (err, song) => {
 			WSModule.runJob("EMIT_TO_ROOM", {
 				room: "admin.queue",
@@ -60,13 +61,8 @@ export default {
 	 * @param cb
 	 */
 	length: isAdminRequired(async function length(session, cb) {
-		const queueSongModel = await DBModule.runJob(
-			"GET_MODEL",
-			{
-				modelName: "queueSong"
-			},
-			this
-		);
+		const queueSongModel = await DBModule.runJob("GET_MODEL", { modelName: "queueSong" }, this);
+
 		async.waterfall(
 			[
 				next => {

+ 8 - 11
backend/logic/actions/songs.js

@@ -26,7 +26,7 @@ CacheModule.runJob("SUB", {
 	channel: "song.added",
 	cb: async songId => {
 		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
-		songModel.findOne({ _id: songId }, (err, song) => {
+		songModel.findOne({ songId }, (err, song) => {
 			WSModule.runJob("EMIT_TO_ROOM", {
 				room: "admin.songs",
 				args: ["event:admin.song.added", song]
@@ -39,7 +39,7 @@ CacheModule.runJob("SUB", {
 	channel: "song.updated",
 	cb: async songId => {
 		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
-		songModel.findOne({ _id: songId }, (err, song) => {
+		songModel.findOne({ songId }, (err, song) => {
 			WSModule.runJob("EMIT_TO_ROOM", {
 				room: "admin.songs",
 				args: ["event:admin.song.updated", song]
@@ -62,6 +62,7 @@ CacheModule.runJob("SUB", {
 				}
 			]
 		});
+
 		WSModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(sockets => {
 			sockets.forEach(socket => {
 				socket.dispatch("event:song.newRatings", {
@@ -254,12 +255,8 @@ export default {
 			[
 				next => {
 					SongsModule.runJob("GET_SONG", { id: songId }, this)
-						.then(response => {
-							next(null, response.song);
-						})
-						.catch(err => {
-							next(err);
-						});
+						.then(response => next(null, response.song))
+						.catch(err => next(err));
 				}
 			],
 			async (err, song) => {
@@ -435,18 +432,18 @@ export default {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 
-					this.log("ERROR", "SONGS_UPDATE", `Failed to remove song "${songId}". "${err}"`);
+					this.log("ERROR", "SONGS_REMOVE", `Failed to remove song "${songId}". "${err}"`);
 
 					return cb({ status: "failure", message: err });
 				}
 
-				this.log("SUCCESS", "SONGS_UPDATE", `Successfully remove song "${songId}".`);
+				this.log("SUCCESS", "SONGS_REMOVE", `Successfully remove song "${songId}".`);
 
 				CacheModule.runJob("PUB", { channel: "song.removed", value: songId });
 
 				return cb({
 					status: "success",
-					message: "Song has been successfully updated"
+					message: "Song has been successfully removed"
 				});
 			}
 		);

+ 8 - 4
backend/logic/actions/stations.js

@@ -358,16 +358,18 @@ CacheModule.runJob("SUB", {
 		StationsModule.runJob("INITIALIZE_STATION", { stationId }).then(async response => {
 			const { station } = response;
 			station.userCount = StationsModule.usersPerStationCount[stationId] || 0;
+
 			WSModule.runJob("EMIT_TO_ROOM", {
 				room: "admin.stations",
 				args: ["event:admin.station.added", station]
-			});
+			}).then(() => {});
+
 			// TODO If community, check if on whitelist
 			if (station.privacy === "public")
 				WSModule.runJob("EMIT_TO_ROOM", {
 					room: "home",
 					args: ["event:stations.created", station]
-				});
+				}).then(() => {});
 			else {
 				const sockets = await WSModule.runJob("GET_SOCKETS_FOR_ROOM", {
 					room: "home"
@@ -2247,13 +2249,15 @@ export default {
 
 				(station, next) => {
 					CacheModule.runJob("HDEL", { table: "stations", key: stationId }, this)
-						.then(next(null, station))
+						.then(() => next(null, station))
 						.catch(next);
 				},
 
 				(station, next) => {
 					if (station.playlist2)
-						PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: station.playlist2 }).then().catch();
+						PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: station.playlist2 })
+							.then(() => {})
+							.catch(next);
 					next(null, station);
 				}
 			],

+ 1 - 7
backend/logic/stations.js

@@ -1010,13 +1010,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					next => {
-						DBModule.runJob(
-							"GET_MODEL",
-							{
-								modelName: "user"
-							},
-							this
-						).then(userModel => {
+						DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
 							userModel.findOne({ _id: payload.userId }, next);
 						});
 					},

+ 8 - 5
backend/logic/ws.js

@@ -289,6 +289,8 @@ class _WSModule extends CoreClass {
 					return resolve();
 				});
 
+			console.log("room doesn't exist");
+
 			return resolve();
 		});
 	}
@@ -326,11 +328,11 @@ class _WSModule extends CoreClass {
 	 */
 	SOCKETS_JOIN_SONG_ROOM(payload) {
 		return new Promise(resolve => {
-			payload.sockets.forEach(async socketId => {
-				await WSModule.runJob("SOCKET_JOIN_SONG_ROOM", { socketId }, this);
-			});
-
-			return resolve();
+			Promise.allSettled(
+				payload.sockets.map(async socketId => {
+					await WSModule.runJob("SOCKET_JOIN_SONG_ROOM", { socketId }, this);
+				})
+			).then(() => resolve());
 		});
 	}
 
@@ -692,6 +694,7 @@ class _WSModule extends CoreClass {
 								"RUN_ACTION2",
 								`Response to action. Action: ${namespace}.${action}. Response status: ${result.status}`
 							);
+
 							resolve(result);
 						}
 					])