Skip to content

Commit

Permalink
Merge pull request #67 from benbp/gen-tag-rename
Browse files Browse the repository at this point in the history
Rename generate-tag graph to match glob load pattern
  • Loading branch information
benbp committed Mar 2, 2016
2 parents 7915d91 + c652b70 commit 14fd919
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ Copyright 2015-2016, EMC, Inc.
brew install mongodb26
brew install rabbitmq

*It is highly recommended to create indexes in mongo to reduce the CPU footprint and increase performance*

```
$ mongo pxe
> db.createCollection('taskdependencies')
> db.createCollection('graphobjects')
> db.taskdependencies.createIndex({ taskId: 1 })
> db.taskdependencies.createIndex({ graphId: 1 })
> db.taskdependencies.createIndex({ graphId: 1, state: 1 })
```

## running

*NOTE: requires RabbitMQ and Mongo to be running to start and test correctly.*
Expand Down Expand Up @@ -209,6 +198,12 @@ PUT

For more detailed information, see our [readthedocs page](http://rackhd.readthedocs.org/en/latest/rackhd/graphs.html?workflow-graphs).

Graph definition files must be saved as javascript or json files in `./lib/graphs/` (nested directories are okay), and filenames must match the pattern
`*-graph.js` or `*-graph.json`. If a graph is saved as a `.js` file, it should export a javascript object conforming to the graph definition schema.
If a graph is saved as a `.json` file, it must be valid json.

Graph definitions can alternatively be uploaded through the API as detailed above in [API commands](#api-commands).

Graphs are defined via a JSON definition that conform to this schema:

- friendlyName (string): a human readable name for the graph
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
module.exports = {
friendlyName: 'Generate Tag',
injectableName: 'Graph.GenerateTags',
options: {
"generate-tag": {
nodeId: null
}
},
tasks: [
{
label: 'generate-tag',
Expand Down
42 changes: 40 additions & 2 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

var di = require('di'),
dihelper = require('on-core')(di).helper,
// Load all graphs within the on-taskgraph/lib/graphs directory (including nested directories)
// that match the naming convention '*-graph.js' or '*-graph.json'.
graphLibrary = dihelper.requireGlob(__dirname + '/graphs/**/*-graph.+(js|json)');

module.exports = loaderFactory;
Expand All @@ -19,11 +21,22 @@ di.annotate(loaderFactory, new di.Inject(
function loaderFactory(store, taskLibrary, Logger, assert, Promise, _) {
var logger = Logger.initialize(loaderFactory);

/**
* File loader class for loading graph definitions off disk, and persisting
* both graph and task definitions into the store.
*
* @constructor
*/
function Loader() {
this.graphLibrary = graphLibrary;
this.taskLibrary = taskLibrary;
}

/**
* Persist task definitions into the store
*
* @memberOf Loader
*/
Loader.prototype.persistTasks = function(definitions) {
return Promise.map(definitions, function(definition) {
assert.object(definition);
Expand All @@ -32,6 +45,11 @@ function loaderFactory(store, taskLibrary, Logger, assert, Promise, _) {
});
};

/**
* Persist graph definitions into the store
*
* @memberOf Loader
*/
Loader.prototype.persistGraphs = function(definitions) {
return Promise.map(definitions, function(definition) {
assert.object(definition);
Expand All @@ -41,12 +59,23 @@ function loaderFactory(store, taskLibrary, Logger, assert, Promise, _) {
});
};

/**
* Object construction helper
*
* @memberOf Loader
*/
Loader.prototype.definitionsArrayToHash = function(data) {
return _.transform(data, function(result, definition) {
result[definition.injectableName] = definition;
}, {});
};

/**
* Merge existing graph and task definitions in the database with
* the ones found on disk to find outdated definitions.
*
* @memberOf Loader
*/
Loader.prototype.mergeDefinitionArrays = function(overlay, base) {
overlay = this.definitionsArrayToHash(overlay);
base = this.definitionsArrayToHash(base);
Expand All @@ -68,6 +97,13 @@ function loaderFactory(store, taskLibrary, Logger, assert, Promise, _) {
});
};

/**
* Load all graphs within the on-taskgraph/lib/graphs directory (including nested directories)
* that match the naming convention '*-graph.js' or '*-graph.json' and
* persist those definitions into the store.
*
* @memberOf Loader
*/
Loader.prototype.load = function() {
var self = this;

Expand All @@ -78,6 +114,7 @@ function loaderFactory(store, taskLibrary, Logger, assert, Promise, _) {
.spread(function(taskCatalog, graphCatalog) {
var graphResults = self.mergeDefinitionArrays(self.graphLibrary, graphCatalog);
var taskResults = self.mergeDefinitionArrays(self.taskLibrary, taskCatalog);
// Ignore persisting base tasks (task.implementsTask returns undefined in that case)
taskResults = _.filter(taskResults, function(task) {
return task.implementsTask;
});
Expand All @@ -88,8 +125,9 @@ function loaderFactory(store, taskLibrary, Logger, assert, Promise, _) {
];
})
.spread(function(taskResults, graphResults) {
logger.info("Loaded " + taskResults.length + " tasks");
logger.info("Loaded " + graphResults.length + " graphs");
logger.info("Loaded " + taskResults.length + " tasks from Task.taskLibrary dependency");
logger.info("Loaded " + graphResults.length +
" graphs matching the pattern 'lib/graphs/**/*-graph.+(js|json)'");
})
.then(function() {
return [store.getTaskDefinitions(), store.getGraphDefinitions()];
Expand Down

0 comments on commit 14fd919

Please sign in to comment.