Skip to content

Commit

Permalink
[minor] Make the cloud functions re-usable, so we could re-do the ope…
Browse files Browse the repository at this point in the history
…ration incase of failure

[minor] Make some routes cloud compatible
  • Loading branch information
3rd-Eden committed Jan 9, 2013
1 parent e161d44 commit fd0cfaf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
16 changes: 9 additions & 7 deletions node.js/lib/client/apps.js
Expand Up @@ -117,7 +117,7 @@ Apps.prototype.destroy = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/'));

this.request({ method: 'DELETE', uri: argv }, callback);
this.cloud({ method: 'DELETE', uri: argv, appName: appName }, this.request, callback);
};

//
Expand All @@ -130,7 +130,7 @@ Apps.prototype.start = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('start');

this.request({ method: 'POST', uri: argv }, callback);
this.cloud({ method: 'POST', uri: argv, appName: appName }, this.request, callback);
};

//
Expand All @@ -143,7 +143,7 @@ Apps.prototype.restart = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('restart');

this.cloud({ method: 'POST', uri: argv, app: appName }, this.request, callback);
this.cloud({ method: 'POST', uri: argv, appName: appName }, this.request, callback);
};

//
Expand All @@ -156,7 +156,7 @@ Apps.prototype.stop = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('stop');

this.request({ method: 'POST', uri: argv }, callback);
this.cloud({ method: 'POST', uri: argv, appName: appName }, this.request, callback);
};

//
Expand All @@ -182,9 +182,10 @@ Apps.prototype.available = function (app, callback) {
//
Apps.prototype.setDrones = function (appName, drones, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('cloud');
var argv = ['apps'].concat(appName.split('/')).concat('cloud'),
cloud = [{ drones: drones }];

this.request({ method: 'POST', uri: argv, body: [{ drones: drones }] }, callback);
this.cloud({ method: 'POST', uri: argv, body: cloud, appName: appName }, this.request, callback);
};

//
Expand All @@ -201,7 +202,7 @@ Apps.prototype.datacenter = function (appName, cloud, callback) {

if (!Array.isArray(cloud)) cloud = [cloud];

this.request({ method: 'POST', uri: argv, body: cloud }, function (err, result) {
this.cloud({ method: 'POST', uri: argv, body: cloud, appName: appName }, this.request, function (err, result) {
if (err) return callback(err);

callback(err, result);
Expand All @@ -220,6 +221,7 @@ Apps.prototype.datacenter = function (appName, cloud, callback) {
//
Apps.prototype.endpoints = function (callback) {
var self = this;

this.request({ uri: ['endpoints'] }, function (err, result) {
if (err) return callback(err);

Expand Down
46 changes: 29 additions & 17 deletions node.js/lib/client/client.js
Expand Up @@ -47,38 +47,42 @@ Client.prototype.cloud = function (options, api, callback) {
flow = [];

// We don't need to have any datacenter information for these types of calls
if (options.remoteUri || !options.app || !options.method || options.method === 'GET') {
if (options.remoteUri || !options.appName || !options.method || options.method === 'GET') {
return api.call(this, options, callback);
}

//
// We don't have any datacenter data by default as it's only needed for
// starting or stopping the application.
//
if (!Object.keys(this.datacenters).length) flow.push(function (done) {
function endpoints(done) {
self.request({ uri: ['endpoints'] }, function endpoints(err, datacenters) {
if (err) return done(err);

self.datacenters = datacenters.endpoints;
done();
});
});
}

//
// Make sure that we have this app in our cloud cache so we know in which
// datacenter it is.
//
if (!(options.app in this.clouds)) flow.push(function (done) {
var argv = ['apps', options.app, 'cloud'];
function locations(done) {
var argv = ['apps', options.appName, 'cloud'];

self.request({ uri: argv }, function apps(err, result) {
if (err) return done(err);

// For some odd reason, the result is an array of arrays.
self.clouds[options.app] = result[0];
self.clouds[options.appName] = result[0];
done();
});
});
}

//
// We don't have any datacenter data by default as it's only needed for
// starting or stopping the application.
//
if (!Object.keys(this.datacenters).length) flow.push(endpoints);

//
// Make sure that we have this app in our cloud cache so we know in which
// datacenter it is.
//
if (!(options.appName in this.clouds)) flow.push(locations);

//
// Iterate over the possible steps.
Expand All @@ -87,7 +91,7 @@ Client.prototype.cloud = function (options, api, callback) {
if (err) return callback(err);

// The returned clouds is an array of datacenters, iterate over them.
async.map(self.clouds[options.app], function iterate(cloud, done) {
async.map(self.clouds[options.appName], function iterate(cloud, done) {
//
// Clone the options to prevent race conditions.
//
Expand All @@ -100,7 +104,15 @@ Client.prototype.cloud = function (options, api, callback) {
if (!~opts.remoteUri.indexOf('http')) opts.remoteUri = 'http://'+ opts.remoteUri;

api.call(self, opts, done);
}, callback);
}, function ready(err, results) {
if (err) delete self.clouds[options.appName];
callback(err, results);

//
// We probably want to figure out which calls went okay, and which one
// failed when we get an error so we only have to retry that one.
//
});
});
};

Expand Down
2 changes: 1 addition & 1 deletion node.js/lib/client/helpers.js
Expand Up @@ -62,7 +62,7 @@ exports.async = {
arr.forEach(function loop(something, index) {
iterator(something, function completion(err, value) {
if (err) {
callback(err);
callback(err, results);
return callback = noop;
}

Expand Down
2 changes: 1 addition & 1 deletion node.js/lib/client/snapshots.js
Expand Up @@ -54,7 +54,7 @@ Snapshots.prototype.create = function (appName, snapshotName, filename, callback
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName]);

return this.upload({ uri: argv, file: filename }, callback);
return this.upload({ uri: argv, file: filename });
};

//
Expand Down

0 comments on commit fd0cfaf

Please sign in to comment.