Skip to content

Commit

Permalink
add doc received limit and action item to message
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-dna-andersend committed Oct 19, 2018
1 parent bce8618 commit 44a9c76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
14 changes: 8 additions & 6 deletions Listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class Listener {

const pubsubSubscription = this.pubsubClient.subscription(subscriptionFullName);

this.checkDocCountExceeded(sub);
this.extractionApiService.getAccountInfo().then(accountInfo =>
this.checkDocCountExceeded(sub, accountInfo.max_allowed_document_extracts));

pubsubSubscription.get().then((data) => {
const pubsubSub = data[0];
Expand All @@ -91,21 +92,22 @@ class Listener {
console.log('Listeners for subscriptions have been configured, set and await message arrival.');
}

checkDocCountExceeded(subscriptionId) {
checkDocCountExceeded(subscriptionId, maxDocumentsReceived) {
const streamDisabledMsg =
'\nOOPS! Looks like you\'ve exceeded the maximum number of documents received for your account.\n' +
`\nOOPS! Looks like you've exceeded the maximum number of documents received for your account (${maxDocumentsReceived}).\n` +
'As such, no new documents will be added to your stream\'s queue.\n' +
'However, you won\'t lose access to any documents that have already been added to the queue.\n' +
'These will continue to be streamed to you.\n';
'These will continue to be streamed to you.\n' +
'Contact your account administrator with any questions or to upgrade your account limits.';
const interval = 30000;
this.extractionApiService.isStreamDisabled(subscriptionId).then((isDisabled) => {
if (isDisabled) {
console.error(streamDisabledMsg);
}
setTimeout(this.checkDocCountExceeded.bind(this), interval, subscriptionId);
setTimeout(this.checkDocCountExceeded.bind(this), interval, subscriptionId, maxDocumentsReceived);
}).catch((err) => {
console.error(err);
setTimeout(this.checkDocCountExceeded.bind(this), interval, subscriptionId);
setTimeout(this.checkDocCountExceeded.bind(this), interval, subscriptionId, maxDocumentsReceived);
});
}
}
Expand Down
23 changes: 23 additions & 0 deletions services/ExtractionApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ class ExtractionApiService {
});
}

getAccountInfo() {
return this._initialize().then(() => {
const accountId = this.headers.Authorization ? this.credentials.client_id : this.headers['user-key'];
const options = {
method: 'GET',
uri: `${this.host}/${this.prefix}/accounts/${accountId}`,
headers: this.headers,
json: false
};

return request(options).then((response) => {
const result = JSON.parse(response);
if (!result.data || !result.data.attributes) {
throw new Error('Error: Unable to find account info');
}
return result.data.attributes;
}).catch((error) => {
console.error('Error retrieving streaming credentials\n');
throw error;
});
});
}

isStreamDisabled(subscriptionId) {
return this._initialize().then(() => {
const disabledStatus = 'DOC_COUNT_EXCEEDED';
Expand Down
2 changes: 0 additions & 2 deletions services/JwtService.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class JwtService {
method: 'POST',
uri: this.oauthUrl,
body: {
username: this.credentials.user_id,
password: this.credentials.password,
scope: 'openid pib',
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
access_token: initResponse.access_token,
Expand Down

0 comments on commit 44a9c76

Please sign in to comment.