Skip to content

Commit 478d827

Browse files
authored
PubSub init retry and additional logs (#451)
* Update pubSubUtil.js * updates * Update dataManager.js * Update dataManager.js * updates * Update ds-api-mgr-role-definition.yaml * Update pubSubUtil.js
1 parent e418d79 commit 478d827

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

api/config/ds-api-mgr-role-definition.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
title: "Datashare Manager Role"
22
description: "Custom Datashare API role to interface with GCP services"
3-
stage: "BETA"
3+
stage: "GA"
44
includedPermissions:
55
- bigquery.datasets.create
66
- bigquery.datasets.delete
@@ -20,7 +20,6 @@ includedPermissions:
2020
- bigquery.tables.updateData
2121
- compute.projects.get
2222
- iam.serviceAccounts.signBlob
23-
- pubsub.snapshots.seek
2423
- pubsub.subscriptions.consume
2524
- pubsub.subscriptions.create
2625
- pubsub.subscriptions.get

api/deploy.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ gcloud run deploy ds-api \
6161
--image gcr.io/${PROJECT_ID}/ds-api:${TAG} \
6262
--platform gke \
6363
--service-account ${SERVICE_ACCOUNT_NAME} \
64-
--update-env-vars=PROJECT_ID="${PROJECT_ID}"
64+
--update-env-vars=PROJECT_ID="${PROJECT_ID}" \
65+
--use-http2
6566

6667
gcloud run services update-traffic ds-api \
6768
--cluster $CLUSTER \

api/v1alpha/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"license": "Apache-2.0",
66
"dependencies": {
77
"@hapi/joi": "^16.1.7",
8+
"async-retry": "^1.3.1",
89
"axios": "^0.21.1",
910
"cds-shared": "file:../../shared",
1011
"compression": "^1.7.4",

api/v1alpha/src/admin/dataManager.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const metaManager = require('../lib/metaManager');
2323
const datasetManager = require('../datasets/dataManager');
2424
const procurementManager = require('../procurements/dataManager');
2525
const fs = require('fs');
26+
const retry = require('async-retry');
2627

2728
require.extensions['.sql'] = function (module, filename) {
2829
module.exports = fs.readFileSync(filename, 'utf8');
@@ -241,6 +242,26 @@ async function setupDatasharePrerequisites(projectId) {
241242
});
242243
}
243244

245+
/**
246+
* Starts the PubSub listener, and retries if launch failure
247+
*/
248+
async function startPubSubListener() {
249+
function logRetry(error, attempt) {
250+
console.warn(`PubSub listener performing retry attempt: ${attempt} after ${error}`);
251+
}
252+
253+
// eslint-disable-next-line no-unused-vars
254+
await retry(async bail => {
255+
await initializePubSubListener();
256+
}, {
257+
// retries: 5,
258+
forever: true,
259+
minTimeout: 30000,
260+
maxTimeout: 60000,
261+
onRetry: logRetry
262+
})
263+
}
264+
244265
/**
245266
* Initializes PubSub listener for entitlement auto approvals
246267
*/
@@ -348,6 +369,13 @@ async function initializePubSubListener() {
348369
let subscription = pubSubUtil.getSubscription(subscriptionName, subscriberOptions);
349370
subscription.on('message', messageHandler);
350371
subscription.on('error', errorHandler);
372+
subscription.on('close', () => { console.error('Subscription closed') });
373+
subscription.detached((cb) => {
374+
console.warn('Subscription detached');
375+
if (cb) {
376+
console.error(cb);
377+
}
378+
});
351379
} catch (err) {
352380
console.error(err);
353381
}
@@ -362,6 +390,6 @@ async function initializePubSubListener() {
362390
module.exports = {
363391
initializeSchema,
364392
syncResources,
365-
initializePubSubListener
393+
startPubSubListener
366394
};
367395

api/v1alpha/src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,7 @@ app.listen(PORT, () => {
302302
console.log("Listening on port " + PORT + ". Press Ctrl+C to quit.");
303303
});
304304

305-
try {
306-
const adminMgr = require('./admin/dataManager');
307-
adminMgr.initializePubSubListener();
308-
} catch (err) {
309-
console.error(err);
310-
}
305+
const adminMgr = require('./admin/dataManager');
306+
adminMgr.startPubSubListener();
311307

312308
module.exports = app;

shared/pubSubUtil.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,10 @@ class PubSubUtil {
9292
* this function will only work if you have the necessary permissions on the topic
9393
*/
9494
async checkIfSubscriptionExists(topicName, subscriptionProjectId, subscriptionName) {
95-
const topic = this.pubsub.topic(topicName);
96-
const formattedSubscription = this.client.subscriptionPath(subscriptionProjectId, subscriptionName);
97-
const subscription = topic.subscription(formattedSubscription);
95+
const formattedSubscription = `projects/${subscriptionProjectId}/subscriptions/${subscriptionName}`;
96+
const subscription = this.getSubscription(formattedSubscription);
9897
const response = await subscription.exists();
9998
const exists = response[0];
100-
if (this.VERBOSE_MODE) {
101-
console.log(`Checking if subscription exists: '${topicName}': ${formattedSubscription}`);
102-
}
10399
return exists;
104100
}
105101

0 commit comments

Comments
 (0)