-
Notifications
You must be signed in to change notification settings - Fork 641
common: create grpc operation object #1525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephenplusplus
merged 5 commits into
googleapis:master
from
callmehiphop:grpc-operation
Aug 25, 2016
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d137c82
common: create grpc operation object
callmehiphop c1d335c
removed useless code
callmehiphop 8c38734
updated jscs rules
callmehiphop 0203a2a
added default callback for cancel
callmehiphop c2fe551
updated pending test
callmehiphop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /*! | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /*! | ||
| * @module common/grpcOperation | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var events = require('events'); | ||
| var modelo = require('modelo'); | ||
|
|
||
| /** | ||
| * @type {module:common/grpcService} | ||
| * @private | ||
| */ | ||
| var GrpcService = require('./grpc-service.js'); | ||
|
|
||
| /** | ||
| * @type {module:common/grpcServiceObject} | ||
| * @private | ||
| */ | ||
| var GrpcServiceObject = require('./grpc-service-object.js'); | ||
|
|
||
| // jscs:disable maximumLineLength | ||
| /** | ||
| * An Operation object allows you to interact with APIs that take longer to | ||
| * process things. | ||
| * | ||
| * @constructor | ||
| * @alias module:common/grpcOperation | ||
| * | ||
| * @param {module:common/grpcService|module:common/grpcServiceObject} parent - The | ||
| * parent object. This should be configured to use the longrunning.operation | ||
| * service. | ||
| * @param {string} name - The operation name. | ||
| */ | ||
| // jscs:enable maximumLineLength | ||
| function GrpcOperation(parent, name) { | ||
| var methods = { | ||
|
|
||
| /** | ||
| * Deletes an operation. | ||
| */ | ||
| delete: { | ||
| protoOpts: { | ||
| service: 'Operations', | ||
| method: 'deleteOperation' | ||
| }, | ||
| reqOpts: { | ||
| name: name | ||
| } | ||
| }, | ||
|
|
||
| /** | ||
| * Checks to see if an operation exists. | ||
| */ | ||
| exists: true, | ||
|
|
||
| /** | ||
| * Retrieves the operation. | ||
| */ | ||
| get: true, | ||
|
|
||
| /** | ||
| * Retrieves metadata for the operation. | ||
| */ | ||
| getMetadata: { | ||
| protoOpts: { | ||
| service: 'Operations', | ||
| method: 'getOperation' | ||
| }, | ||
| reqOpts: { | ||
| name: name | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var config = { | ||
| parent: parent, | ||
| id: name, | ||
| methods: methods | ||
| }; | ||
|
|
||
| GrpcServiceObject.call(this, config); | ||
| events.EventEmitter.call(this); | ||
|
|
||
| this.completeListeners = 0; | ||
| this.hasActiveListeners = false; | ||
|
|
||
| this.listenForEvents_(); | ||
| } | ||
|
|
||
| modelo.inherits(GrpcOperation, GrpcServiceObject, events.EventEmitter); | ||
|
|
||
| /** | ||
| * Cancel the operation. | ||
| * | ||
| * @param {function=} callback - The callback function. | ||
| * @param {?error} callback.err - An error returned while making this | ||
| * request. | ||
| * @param {object} callback.apiResponse - The full API response. | ||
| */ | ||
| GrpcOperation.prototype.cancel = function(callback) { | ||
| var protoOpts = { | ||
| service: 'Operations', | ||
| method: 'cancelOperation' | ||
| }; | ||
|
|
||
| var reqOpts = { | ||
| name: this.id | ||
| }; | ||
|
|
||
| this.request(protoOpts, reqOpts, callback); | ||
| }; | ||
|
|
||
| /** | ||
| * 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 | ||
| */ | ||
| GrpcOperation.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. | ||
| * | ||
| * Note: This method is automatically called once a "complete" event handler is | ||
| * registered on the operation. | ||
| * | ||
| * @private | ||
| */ | ||
| GrpcOperation.prototype.startPolling_ = function() { | ||
| var self = this; | ||
|
|
||
| if (!this.hasActiveListeners) { | ||
| return; | ||
| } | ||
|
|
||
| this.getMetadata(function(err, resp) { | ||
| if (err || resp.error) { | ||
| self.emit('error', err || GrpcService.decorateStatus_(resp.result)); | ||
| return; | ||
| } | ||
|
|
||
| if (!resp.done) { | ||
| setTimeout(self.startPolling_.bind(self), 500); | ||
| return; | ||
| } | ||
|
|
||
| self.emit('complete', resp); | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = GrpcOperation; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.