task_throttler
v0.1.5
Published
Allows you to queue up functions as tasks and set what rate of speed they fire off. Can be a single interval or it can repeat.
Downloads
2
Readme
Task Throttler
Task Throttler is a module that is designed to allow you to queue up functions and have them start/stop and run at whatever rate over time that you specify. I had to access an API that could only have requests made to it 30 times in a minute so I made this simple module to allow me to create the task with the ability to start and stop it anytime and best of all specify the interval not only in milliseconds but also in plaintext. Made it in about 30 minutes so its kind of dirty and not the most efficient for long running scripts, but if you are making a quick importer or exporter that needs to read/write and api, it should work fine. Keep an eye out for the git, it would be cool to make this more efficient.
Version
0.1.2
Tech
Task Throttler only uses nodes built in timer functions. It is not meant to be super precise, just simple and effective.
Installation
npm install task_throttler
Usage
This example is just passing in an anonymous function as a callback, but you can create any function and pass it to the queue.
//This will create a task that starts automatically and will execute every second.
var Throttler = require('task_throttler'),
throttle = new Throttle,
someArray = ["item1", "item2", "item3", "item4", "item5", "item6"];
throttle.queue("testTask", 1000, {
autoStart : true
}, function(task) {
//Stuff below here
if (queue.length) {
console.log(queue);
someArray.shift();
} else {
throttle.stop(task.name);
}
//stuff above here
});
Methods
queue()
Throttler.queue(name, interval, options, callback);
This creates the task. When adding a task to the queue it just means you have made one that can be used. It can be autostarted or started manually and when stopped it is not automatically deleted, it will remain in the task object to be turned on again later if you wish.
Arguments
####-name (string) - Required
Name of the task
####-interval (string | integer) - Required
Time between each cycle. If you pass it an integer, it will be time in milliseconds. If you pass it a string, it must be in the following format: " [number of cycles]/[unit of time measurement] ", ie: "5/minute" = 5 times a minute.
####-options (object) - Optional
Options to be passed to your task;
autoStart: (true | false) - Automatically starts the script. Default: false
runOnce: (true | false) - Script will automatically stop after one cycle, it uses setTimeout. Default: false
killOnStop: (true | false) - Automatically removes task from queue when it is stopped. Default: false
cycles: (integer) - Number of times you want the task to run before stopping. Default: 0 (forever)
####-callback (function) - Required
Your actual task. Can be an anonymous function or a named one.
start()
Throttler.start(name);
Start a task, name is the actualy name you gave it when using .queue()
stop()
Throttler.stop(name);
Stop a task, name is the actualy name you gave it when using .queue()
kill()
Throttler.stop(name);
Kill a task, name is the actualy name you gave it when using .queue(). Once killed, a task cannot be restarted and would need to be requeued.
running()
Throttler.running();
This is more of a utility function. It will return and object that contains all of the running tasks. Could use it to iterate through and stop or kill all etc. In the source, there actually are some arguments, but they are used for managing the running object.
There are a few more utility functions, but I didn't intend on ever using them directly, but they are there if you want to take a look and mess with them. License
MIT
Free to use for whatever