Skip to content

Instantly share code, notes, and snippets.

@digitalhitler
Created July 17, 2016 08:12
Show Gist options
  • Save digitalhitler/2c789e7956924d7bc9b13178541e58f9 to your computer and use it in GitHub Desktop.
Save digitalhitler/2c789e7956924d7bc9b13178541e58f9 to your computer and use it in GitHub Desktop.
Queue.js
"use strict";
const output = require('./output');
const VKQueue = {
_tasks: [],
_checkInterval: 100
_intervalHandler: false,
_lastRequestStarted: null,
_lastRequestFinished: null,
throttleTimeStart: 350,
throttleTimeNow: 350,
throttleTimeMin: 200,
throttleTimeStep: -30,
enqueue: (options) => {
let task = {};
task.prioritized = options.prioritized || false;
task.startedAt = null;
task.finishedAt = null;
task.failedAt = null;
// When the throttle time reached "too many requests" failure
// and throttleTimeNow has been reset to initial value
task.resetAt = null;
task.timesFailed = 0;
task.timesReset = 0;
task.enqueuedAt = Date.now();
task.executeArgs = options.args || null;
task.execute = () => {
let handler = options.execute;
return new Promise((resolve, reject) => {
task.startedAt = Date.now();
});
};
VKQueue._tasks.push(task);
if(VKQueue._tasks.length === 1) {
};
if(interval < taskControl._interval) {
taskControl._interval = interval;
}
},
_runner: () => {
if(taskControl._tasks.length > 0) {
for(let curr in taskControl._tasks) {
// @todo: complete here
let isExpired = true;
if(isExpired === true) {
taskControl._tasks[curr].latestStart = Date.now();
taskControl._tasks[curr].startedTimes++;
let taskResult = taskControl._tasks[curr].handler();
console.log(taskResult + ' is a result of running' + curr);
}
}
}
},
run: () => {
// taskControl.getMinInterval();
if(typeof taskControl._interval !== 'undefined'
&& taskControl._interval > 0
&& taskControl._interval !== Infinity) {
taskControl._runnerInterval = setInterval(taskControl._runner, taskControl._interval);
output.info('Running tasks every:');
output.info(taskControl._interval);
} else {
throw new TaskControlError("Can't determine minimal interval: are you defined any task?")
}
}
};
module.exports = VKQueue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment