Browse Source

Added tasks system.

KrisVos130 8 years ago
parent
commit
29d850f169
3 changed files with 55 additions and 1 deletions
  1. 4 0
      backend/index.js
  2. 50 0
      backend/logic/tasks.js
  3. 1 1
      backend/logic/utils.js

+ 4 - 0
backend/index.js

@@ -16,6 +16,7 @@ const playlists = require('./logic/playlists');
 const cache = require('./logic/cache');
 const cache = require('./logic/cache');
 const notifications = require('./logic/notifications');
 const notifications = require('./logic/notifications');
 const logger = require('./logic/logger');
 const logger = require('./logic/logger');
+const tasks = require('./logic/tasks');
 const config = require('config');
 const config = require('config');
 
 
 process.on('uncaughtException', err => {
 process.on('uncaughtException', err => {
@@ -62,6 +63,9 @@ async.waterfall([
 	// setup the logger
 	// setup the logger
 	(next) => logger.init(next),
 	(next) => logger.init(next),
 
 
+	// setup the tasks system
+	(next) => tasks.init(next),
+
 	// setup the frontend for local setups
 	// setup the frontend for local setups
 	(next) => {
 	(next) => {
 		if (!config.get("isDocker")) {
 		if (!config.get("isDocker")) {

+ 50 - 0
backend/logic/tasks.js

@@ -0,0 +1,50 @@
+'use strict';
+
+let utils;
+let tasks = {};
+
+let testTask = (callback) => {
+	//Stuff
+	console.log("Starting task");
+	setTimeout(() => {
+		console.log("Callback");
+		callback();
+	}, 10000);
+};
+
+
+module.exports = {
+	init: function(cb) {
+		utils = require('./utils');
+		this.createTask("testTask", testTask, 5000);
+		this.pauseTask("testTask");
+
+		cb();
+	},
+	createTask: function(name, fn, timeout) {
+		tasks[name] = {
+			name,
+			fn,
+			timeout,
+			lastRan: 0,
+			timer: null
+		};
+		this.handleTask(tasks[name]);
+	},
+	pauseTask: (name) => {
+		tasks[name].timer.pause();
+	},
+	resumeTask: (name) => {
+		tasks[name].timer.resume();
+	},
+	handleTask: function(task) {
+		if (task.timer) task.timer.pause();
+
+		task.fn(() => {
+			task.lastRan = Date.now();
+			task.timer = new utils.Timer(() => {
+				this.handleTask(task);
+			}, task.timeout, false);
+		});
+	}
+};

+ 1 - 1
backend/logic/utils.js

@@ -13,7 +13,7 @@ class Timer {
 		this.timerId = undefined;
 		this.timerId = undefined;
 		this.start = undefined;
 		this.start = undefined;
 		this.paused = paused;
 		this.paused = paused;
-		this.remaining = moment.duration(delay, "hh:mm:ss").asSeconds() * 1000;
+		this.remaining = delay;
 		this.timeWhenPaused = 0;
 		this.timeWhenPaused = 0;
 		this.timePaused = Date.now();
 		this.timePaused = Date.now();