Skip to content

Commit

Permalink
compute: use common/Operation (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and callmehiphop committed Nov 14, 2016
1 parent 6d55ac6 commit 45b7086
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 302 deletions.
3 changes: 1 addition & 2 deletions packages/google-cloud-compute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@
"compute engine"
],
"dependencies": {
"@google-cloud/common": "^0.7.0",
"@google-cloud/common": "^0.8.0",
"arrify": "^1.0.0",
"async": "^2.0.1",
"create-error-class": "^3.0.2",
"extend": "^3.0.0",
"gce-images": "^0.3.0",
"is": "^3.0.1",
"modelo": "^4.2.0",
"string-format-obj": "^1.0.0"
},
"devDependencies": {
Expand Down
81 changes: 7 additions & 74 deletions packages/google-cloud-compute/src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
'use strict';

var common = require('@google-cloud/common');
var events = require('events');
var modelo = require('modelo');
var util = require('util');

/*! Developer Documentation
*
Expand Down Expand Up @@ -159,23 +158,17 @@ function Operation(scope, name) {
get: true
};

common.ServiceObject.call(this, {
common.Operation.call(this, {
parent: scope,
baseUrl: isCompute ? '/global/operations' : '/operations',
id: name,
methods: methods
});

events.EventEmitter.call(this);

this.completeListeners = 0;
this.hasActiveListeners = false;
this.name = name;

this.listenForEvents_();
}

modelo.inherits(Operation, common.ServiceObject, events.EventEmitter);
util.inherits(Operation, common.Operation);

/**
* Get the operation's metadata. For a detailed description of metadata see
Expand Down Expand Up @@ -230,62 +223,6 @@ Operation.prototype.getMetadata = function(callback) {
});
};

/**
* Convenience method that wraps the `complete` and `error` events in a
* Promise.
*
* @return {promise}
*
* @example
* operation.promise().then(function(metadata) {
* // The operation is complete.
* }, function(err) {
* // An error occurred during the operation.
* });
*/
Operation.prototype.promise = function() {
var self = this;

return new self.Promise(function(resolve, reject) {
self
.on('error', reject)
.on('complete', function(metadata) {
resolve([metadata]);
});
});
};

/**
* Begin listening for events on the operation. This method keeps track of how
* many "complete" listeners are registered and removed, making sure polling is
* handled automatically.
*
* As long as there is one active "complete" listener, the connection is open.
* When there are no more listeners, the polling stops.
*
* @private
*/
Operation.prototype.listenForEvents_ = function() {
var self = this;

this.on('newListener', function(event) {
if (event === 'complete') {
self.completeListeners++;

if (!self.hasActiveListeners) {
self.hasActiveListeners = true;
self.startPolling_();
}
}
});

this.on('removeListener', function(event) {
if (event === 'complete' && --self.completeListeners === 0) {
self.hasActiveListeners = false;
}
});
};

/**
* Poll `getMetadata` to check the operation's status. This runs a loop to ping
* the API on an interval.
Expand All @@ -295,21 +232,17 @@ Operation.prototype.listenForEvents_ = function() {
*
* @private
*/
Operation.prototype.startPolling_ = function() {
Operation.prototype.poll_ = function(callback) {
var self = this;

if (!this.hasActiveListeners) {
return;
}

this.getMetadata(function(err, metadata, apiResponse) {
// Parsing the response body will automatically create an ApiError object if
// the operation failed.
var parsedHttpRespBody = common.util.parseHttpRespBody(apiResponse);
err = err || parsedHttpRespBody.err;

if (err) {
self.emit('error', err);
callback(err);
return;
}

Expand All @@ -319,12 +252,12 @@ Operation.prototype.startPolling_ = function() {
}

if (metadata.status !== 'DONE') {
setTimeout(self.startPolling_.bind(self), 500);
callback();
return;
}

self.status = metadata.status;
self.emit('complete', metadata);
callback(null, metadata);
});
};

Expand Down
10 changes: 10 additions & 0 deletions packages/google-cloud-compute/system-test/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ describe('Compute', function() {
});
});
});

it('should run operation as a promise', function() {
var snapshot = disk.snapshot(generateName('snapshot'));

return snapshot.create()
.then(function(response) {
var operation = response[1];
return operation.promise();
});
});
});

describe('firewalls', function() {
Expand Down
Loading

0 comments on commit 45b7086

Please sign in to comment.