taskman
v1.0.0
Published
Provides a task manager for organizing and scheduling asynchronous operations
Downloads
2
Readme
Taskman
Taskman is a node.js library for managing (possibly) asynchronous operations. This library strives to be different from other asynchronous operations managers by focusing on the relationships between tasks. Tasks that have no dependencies are run first, and tasks that do have dependencies are run as soon as their dependencies have completed.
To use a task manager, you must first instantiate an instance.
var Task = require("node-taskman"),
task = new Task();
Then you add your tasks in the form of functions. The task function should call either the finish or cancel method passed in to it once the task has completed/errored. This will trigger any dependents this task has.
var task1 = task.add(function(finish, cancel){
console.log("I am a task");
setTimeout(function(){
finish();
}, 1000);
});
Next, specify the relationships between the different tasks.
task.createDependency(task3, [task1, task2]);
Finally, run and enjoy!
task.run(function(){
console.log("success!");
})
You can instantiate as many taskmans as you want, and can even nest taskmans inside of each other. See examples/
for other interesting ways of using taskman.
Install
npm install taskman
Methods
add
Description
Adds a task to the manager.
Signature
taskman.add(taskID, dependencies, task)
Parameters
- taskID optional The identifier of the task. If ommitted, a task is generated automatically with the form "@###".
- dependencies <String | Array> optional The task ID(s) of the task's dependencies. This is the same as calling taskman.createDependency(task, dependencies).
- task <function(finish, cancel)> The task to add.
Throws
- Task ID is already taken.
- Task is not a function.
Returns
The task ID of the newly created task.
cancel
Description
Cancels the run in progress and calls the cancel callback supplied to Taskman#run, if available.
Signature
taskman.cancel(customData)
Parameters
- customData optional Data to be passed to the cancel callback.
clearDependencies
Description
Clears all of the dependencies for the supplied targets
Signature
taskman.clearDependencies(targets)
Parameters
- targets <String | Array> optional The task ID's of the targets to clear the dependencies for. If no targets are specified, all dependencies are cleared.
createDependency
Description
Creates a dependency between two or more tasks
Signature
taskman.createDependency(targets, dependencies)
Parameters
- targets <String | Array> The task ID(s) target or targets to create dependencies for. If more than one target is supplied, every dependency specified is set as a dependency of each target.
- dependencies <String | Array> The task ID(s) of the dependency or dependencies of the targets.
Throws
- Unsupported targets type. The targets parameter is not a string or an array.
- Unsupported dependencies type. The dependencies parameter is not a string or an array.
reset
Description
Resets the task so that it can be used again. All registered tasks and dependencies are discarded.
Signature
taskman.reset()
run
Description
Runs the scheduled tasks. Any changes to the dependencies after this method is called are ignored.
Signature
taskman.run(serializeTasksFlag, completion, cancel)
Parameters
- serializeTasksFlag Convienience flag that calls Taskman@serializeTasks under the hood
- completion <function()> The function to call once all tasks have completed successfully
- cancel <function(customData)> The function to call if one of the tasks calls Taskman#cancel or its cancel function. The data passed in to Taskman#cancel is supplied as the argument to this function.
serializeTasks
Description
Automatically creates a dependency between every task such that the next task does not run until the task before it has completed. Any tasks added after this method is called are not serialized.
Signature
taskman.serializeTasks()
License
Copyright 2012-2013 Bryan Hughes [email protected]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.