Explorar o código

Added check for skip station task.

KrisVos130 %!s(int64=8) %!d(string=hai) anos
pai
achega
ce746d20d6
Modificáronse 2 ficheiros con 31 adicións e 6 borrados
  1. 0 2
      backend/logic/actions/stations.js
  2. 31 4
      backend/logic/tasks.js

+ 0 - 2
backend/logic/actions/stations.js

@@ -236,7 +236,6 @@ module.exports = {
 				});
 			}
 		], (err, stations) => {
-			console.log(err, stations);
 			if (err) {
 				err = utils.getError(err);
 				logger.error("STATIONS_INDEX", `Indexing stations failed. "${err}"`);
@@ -815,7 +814,6 @@ module.exports = {
 			}
 		], (err, station) => {
 			if (err) {
-				console.log(err);
 				err = utils.getError(err);
 				logger.error("STATIONS_CREATE", `Creating station failed. "${err}"`);
 				return cb({'status': 'failure', 'message': err});

+ 31 - 4
backend/logic/tasks.js

@@ -1,5 +1,8 @@
 'use strict';
 
+const cache = require("./cache");
+const Stations = require("./stations");
+const async = require("async");
 let utils;
 let tasks = {};
 
@@ -12,16 +15,40 @@ let testTask = (callback) => {
 	}, 10000);
 };
 
+let checkStationSkipTask = (callback) => {
+	console.log(`Checking for stations`);
+	async.waterfall([
+		(next) => {
+			cache.hgetall('stations', next);
+		},
+		(stations, next) => {
+			async.each(stations, (station, next2) => {
+				if (station.paused || !station.currentSong || !station.currentSong.title) return next2();
+				const timeElapsed = Date.now() - station.startedAt - station.timePaused;
+				if (timeElapsed <= station.currentSong.duration) return next2();
+				else {
+					console.log(`Skipping ${station._id}`);
+					stations.skipStation(station._id);
+					next2();
+				}
+			}, () => {
+				next();
+			});
+		}
+	], () => {
+		callback();
+	});
+};
 
 module.exports = {
 	init: function(cb) {
 		utils = require('./utils');
-		this.createTask("testTask", testTask, 5000);
-		this.pauseTask("testTask");
+		this.createTask("testTask", testTask, 5000, true);
+		this.createTask("stationSkipTask", checkStationSkipTask, 1000 * 60 * 30);
 
 		cb();
 	},
-	createTask: function(name, fn, timeout) {
+	createTask: function(name, fn, timeout, paused = false) {
 		tasks[name] = {
 			name,
 			fn,
@@ -29,7 +56,7 @@ module.exports = {
 			lastRan: 0,
 			timer: null
 		};
-		this.handleTask(tasks[name]);
+		if (!paused) this.handleTask(tasks[name]);
 	},
 	pauseTask: (name) => {
 		tasks[name].timer.pause();