Ver código fonte

Fixed more backend issues

Kristian Vos 4 anos atrás
pai
commit
1f1248e2fa
3 arquivos alterados com 57 adições e 40 exclusões
  1. 23 2
      backend/core.js
  2. 8 1
      backend/index.js
  3. 26 37
      backend/logic/stations.js

+ 23 - 2
backend/core.js

@@ -65,6 +65,15 @@ class Queue {
 		return this.runningTasks.length;
 	}
 
+	/**
+	 * Returns the amount of running jobs.
+	 *
+	 * @returns {number} - amount of running jobs
+	 */
+	lengthPaused() {
+		return this.pausedTasks.length;
+	}
+
 	/**
 	 * Adds a job to the queue, with a given priority.
 	 *
@@ -94,6 +103,12 @@ class Queue {
 	 */
 	pauseRunningJob(job) {
 		const task = this.runningTasks.find(task => task.job.toString() === job.toString());
+		if (!task) {
+			console.log(
+				`Attempted to pause job ${job.name} (${job.toString()}), but couldn't find it in running tasks.`
+			);
+			return;
+		}
 		this.runningTasks.remove(task);
 		this.pausedTasks.push(task);
 	}
@@ -105,6 +120,12 @@ class Queue {
 	 */
 	resumeRunningJob(job) {
 		const task = this.pausedTasks.find(task => task.job.toString() === job.toString());
+		if (!task) {
+			console.log(
+				`Attempted to resume job ${job.name} (${job.toString()}), but couldn't find it in paused tasks.`
+			);
+			return;
+		}
 		this.pausedTasks.remove(task);
 		this.queue.unshift(task);
 		setTimeout(() => {
@@ -117,7 +138,8 @@ class Queue {
 	 */
 	_handleQueue() {
 		if (this.queue.length > 0) {
-			const task = this.queue.reduce((a, b) => (a.priority < b.priority ? b : a));
+			const task = this.queue.reduce((a, b) => (a.priority < b.priority ? a : b));
+			// console.log(`First task: `, task);
 			if (task) {
 				if ((!this.paused && this.runningTasks.length < this.concurrency) || task.priority === -1) {
 					this.queue.remove(task);
@@ -435,7 +457,6 @@ export default class CoreClass {
 			parentJob ? parentJob.task.priority : Infinity,
 			this.priorities[name] ? this.priorities[name] : 10
 		);
-		console.log(_priority);
 		this.jobQueue.push(job, _priority);
 		// }
 

+ 8 - 1
backend/index.js

@@ -427,7 +427,7 @@ process.stdin.on("data", data => {
 			console.log(
 				`${moduleName.toUpperCase()}${Array(tabsNeeded).join(
 					"\t"
-				)}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Concurrency: ${
+				)}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Jobs paused: ${module.jobQueue.lengthPaused()} Concurrency: ${
 					module.jobQueue.concurrency
 				}. Stage: ${module.getStage()}`
 			);
@@ -457,6 +457,13 @@ process.stdin.on("data", data => {
 	if (command.startsWith("debug")) {
 		moduleManager.modules.utils.runJob("DEBUG");
 	}
+
+	if (command.startsWith("eval")) {
+		const evalCommand = command.replace("eval ", "");
+		console.log(`Running eval command: ${evalCommand}`);
+		const response = eval(evalCommand);
+		console.log(`Eval response: `, response);
+	}
 });
 
 export default moduleManager;

+ 26 - 37
backend/logic/stations.js

@@ -217,13 +217,9 @@ class _StationsModule extends CoreClass {
 					(station, next) => {
 						if (!station) return next("Station not found.");
 
-						NotificationsModule.runJob(
-							"UNSCHEDULE",
-							{
-								name: `stations.nextSong?id=${station._id}`
-							},
-							this
-						)
+						NotificationsModule.runJob("UNSCHEDULE", {
+							name: `stations.nextSong?id=${station._id}`
+						})
 							.then()
 							.catch();
 
@@ -232,13 +228,9 @@ class _StationsModule extends CoreClass {
 							{
 								name: `stations.nextSong?id=${station._id}`,
 								cb: () =>
-									StationsModule.runJob(
-										"SKIP_STATION",
-										{
-											stationId: station._id
-										},
-										this
-									),
+									StationsModule.runJob("SKIP_STATION", {
+										stationId: station._id
+									}),
 								unique: true,
 								station
 							},
@@ -286,15 +278,11 @@ class _StationsModule extends CoreClass {
 								.catch(next);
 						}
 						// name, time, cb, station
-						NotificationsModule.runJob(
-							"SCHEDULE",
-							{
-								name: `stations.nextSong?id=${station._id}`,
-								time: timeLeft,
-								station
-							},
-							this
-						);
+						NotificationsModule.runJob("SCHEDULE", {
+							name: `stations.nextSong?id=${station._id}`,
+							time: timeLeft,
+							station
+						});
 
 						return next(null, station);
 					}
@@ -461,7 +449,7 @@ class _StationsModule extends CoreClass {
 									.then()
 									.catch();
 							}
-							station = this.stationSchema(station);
+							station = StationsModule.stationSchema(station);
 							CacheModule.runJob("HSET", {
 								table: "stations",
 								key: payload.stationId,
@@ -512,15 +500,13 @@ class _StationsModule extends CoreClass {
 									songList: station.playlist
 								});
 							}
-							CacheModule.runJob("GET_SCHEMA", { schemaName: "station" }, this).then(stationSchema => {
-								station = stationSchema(station);
-								CacheModule.runJob("HSET", {
-									table: "stations",
-									key: station._id,
-									value: station
-								});
-								next(true, station);
+							station = StationsModule.stationSchema(station);
+							CacheModule.runJob("HSET", {
+								table: "stations",
+								key: station._id,
+								value: station
 							});
+							next(true, station);
 						} else next("Station not found");
 					}
 				],
@@ -761,7 +747,8 @@ class _StationsModule extends CoreClass {
 						if (station.type === "official" && station.playlist.length === 0) {
 							return StationsModule.runJob("CALCULATE_SONG_FOR_STATION", { station }, this)
 								.then(playlist => {
-									if (playlist.length === 0) return next(null, this.defaultSong, 0, station);
+									if (playlist.length === 0)
+										return next(null, StationsModule.defaultSong, 0, station);
 
 									return SongsModule.runJob(
 										"GET_SONG",
@@ -773,9 +760,11 @@ class _StationsModule extends CoreClass {
 										.then(response => {
 											next(null, response.song, 0, station);
 										})
-										.catch(() => next(null, this.defaultSong, 0, station));
+										.catch(() => next(null, StationsModule.defaultSong, 0, station));
 								})
-								.catch(next);
+								.catch(err => {
+									next(err);
+								});
 						}
 
 						if (station.type === "official" && station.playlist.length > 0) {
@@ -808,10 +797,10 @@ class _StationsModule extends CoreClass {
 														station.playlist = newPlaylist;
 														next(null, response.song, 0);
 													})
-													.catch(() => next(null, this.defaultSong, 0));
+													.catch(() => next(null, StationsModule.defaultSong, 0));
 											})
 											.catch(() => {
-												next(null, this.defaultSong, 0);
+												next(null, StationsModule.defaultSong, 0);
 											});
 									}
 								},