agilify
What is agilify?
agilify
handles dependencies between tasks and resolves them in an specific tree of requirements according to
your call.
Quick Examples
// register some tasksvar jar = ; // it will resolve all deep dependencies itselfjar; // add tasks on the flyjar;
Do I need agilify?
Probably, it can
- handle different processes with changing requirements
- speedup your
middlewares
by using non blocking / parallel execution - visualize easily your dependencies by it's structure
E.g. you have some components, let's say:
- A) connect database
- B) fetch user information (depends on A)
- C) fetch users shopping cart (depends on B)
- D) check blacklist (depends on A)
- E) check rate limit (api call, no dependency)
Most systems would execute those tasks in series, but task A and E, and after A has responded, B and D can executed in parallel.
Installation
npm install agilify --save
Usage
agilify
consists of three parts, a container, some tasks and a runtime process to handle the execution.
Simple usage example
var agilify = define = agilifydefine; // setup task 1var dependencyA = ; // setup task 2 which depends on 1var myTask = ; // register them in a jarvar taskJar = ; // now you can start callstaskJar;
Documentation
agilify(arrayOfTasks)
agilify()
is a short hand method for creating a new Agilify
instance with predefined tasks.
Examples
var agilify = define = agilifydefine; // creates an empty instancevar agilifyJar = ; // with predefined tasks. The array must contain instances of `AgilifyTask`var agilifyJar = ;
define([name], [dependencies], function)
With define
you can create instances of AgilifyTask
which can be applied to the task jar. Tasks must have an
unique name and a function to call. If you skip the name it will try to it from the given function.
Tasks can have dependencies which must be fulfilled before the task can be executed.
Examples
// named function without dependenciesvar task = ; // set an explicit namevar task = ; // function with dependencies, both lines create the same taskvar task = ;var task = ;
Agilify object
The Agilify
object is a container of tasks. For different types of tasks you can create instances for each.
// load the objectvar Agilify = Agilify;
constructor([tasks])
You can set up Agilify
immediately by passing an array of AgilifyTask instances.
Arguments
tasks
Array of AgilifyTask instances, optional.
Example
var myTaskJar = task1 task2;
taskByName(name)
Returns a task by given name. If it doesn't match, undefined
will be returned.
Arguments
name
Name of task to search for
Example
var myTaskJar = task1;myTaskJar; // returns task -> task1
register(name, dependencies, function)
Register adds a task to your Agilify
instance. You can call register
in different ways:
- register(name, [dependencies], function)
- register([name], [dependencies], named function)
- register(task instance)
Arguments
name
the name of the new task or task instancedependencies
list of task names, order of names specifies the order of results in your functionfunction(dep1, dep2, ..., callback)
your function to execute, list of arguments is defined by the list of dependencies plus the callbackcallback(err, result)
result will be passed to the dependent in current execution process
Examples
Ways to register a new task without dependencies
myTaskJar; // use define to create a task and pass it to registermyTaskJar; // use a named functionmyTaskJar; // override the name explicitmyTaskJar; // with (empty) dependencies
Example of passing results of dependencies
myTaskJar; myTaskJar;
addTask(task)
Just only to add a task to the Agilify
instance. It will raise an Error if a task with a given name already exits or
not a name is defined. addTask()
is called internally when using register()
.
Arguments
task
AgilifyTask
instance to add
run([dependencies], [context], fnc)
With run()
, you can invoke an execution of some code which relies on dependencies in this jar.
Quick example
myTaskJar;
Arguments
dependencies
array of task names which are required, optional but recommended :)context
an object which is bound to any task function in execution. You can access thecontext
in those functions viathis
, optionalfunction(err, res1, res2, ...)
your function to execute when all dependencies are fulfilled. If something went wrong in this chain, the error will be passed there, otherwise it'snull
Examples
Basic example
myTaskJar;
Express app middleware: Make req
and res
available to the tasks.
var context = req: req res: res; myTaskJar;
TODO
- write proper documentation
- agilify.discover('./tasks', callback);
- debug() integration
- time measurement
- caching of generated dependency list
- check if dependency list is in theory resolvable (detect circular dependencies)