Skip to content

Commit

Permalink
When updating a collection, it should replace the collection entirely…
Browse files Browse the repository at this point in the history
… - updating/creating/deleting tasks to match the new config passed
  • Loading branch information
mkopinsky committed Jan 19, 2015
1 parent 9dd144e commit 89d873b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api.js
Expand Up @@ -218,10 +218,10 @@ module.exports = function(ummon){
// Modify config for feeding to createCollectionAndTasks
// TODO Simplify stored object
config.collection = req.params.collection;
if ('enabled' in config) {
if (config.hasOwnProperty('enabled')) {
config.config = {enabled: config.enabled};
}
ummon.createCollectionAndTasks(config, function(err){
ummon.updateCollectionAndTasks(config, function(err){
if (err) return next(new restify.InvalidContentError(err.message));

ummon.getTasks(req.params.collection, function(err, collection){
Expand Down
57 changes: 57 additions & 0 deletions lib/ummon.js
Expand Up @@ -593,6 +593,63 @@ Ummon.prototype.createCollectionAndTasks = function(config, callback) {
};


/**
* Update a collection and it's associated tasks. The new config will fully overwrite - update tasks if appropriate,
* create them if appropriate, delete them from ummon if it doesn't exist in the new config passed
*
* @see autoLoadTasks
* @param {Object} config Config object from json
* @param {Function} callback The callback. Simply returns true if all goes well
*/
Ummon.prototype.updateCollectionAndTasks = function(config, callback) {
var self = this;
// Setup the defaults for this collection
self.defaults[config.collection] = config.defaults;

// Setup settings
if (config.config) {
self.config.collections[config.collection] = config.config;
} else {
// Default collection config
self.config.collections[config.collection] = {enabled:true};
}
if (!config.tasks) return callback(new Error('Collection config must include tasks'));

var newTasks = Object.keys(config.tasks);
var oldTasks = _.pluck(_.where(self.tasks, {collection: config.collection}),'name');
var allTasks = _.union(oldTasks, newTasks);

var hasError = false;
var cb = function(err) {
if (err) {
hasError = true;
callback(err);
}
};

_.each(allTasks,function (taskid) {
if (_.contains(newTasks,taskid) && !_.contains(oldTasks,taskid)) {
// Create
self.log.info('Creating task '+taskid);
// Rearrange task config for createTask
var taskConfig = config.tasks[taskid];
taskConfig.name = taskid;
taskConfig.collection = config.collection;
self.createTask(taskConfig, cb);
} else if (!_.contains(newTasks,taskid) && _.contains(oldTasks,taskid)) {
// Delete
self.log.info('Deleting task '+taskid);
self.deleteTask(config.collection+'.'+taskid, cb);
} else if (_.contains(newTasks,taskid) && _.contains(oldTasks,taskid)) {
// Update
self.log.info('Updating task '+taskid);
self.updateTask(config.collection+'.'+taskid, config.tasks[taskid], cb);
}
});
if (!hasError) callback(null);
};


/**
* Show the details for a particular task
*
Expand Down

0 comments on commit 89d873b

Please sign in to comment.