google-cloud-node: Automatically create topic / subscription if not found.
Presently I have some code to publish to a particular topic, creating it if necessary:
function publish(message, cb){
topic.publish(message, function(err){
if(err && err.code == 404){
logging.info("Creating pub/sub topic.");
pubsub.createTopic(topicName, function(err, _){
if(err) return cb(err);
publish(message, cb);
});
} else {
cb(err);
}
});
}
And similar code to listen for messages to a topic. It goes the other way around - trying to create one at first then re-using if it’s already there.
function subscribe(cb){
topic.subscribe(subscriptionName, function(err, subscription){
if(!err) return wireSubscription(subscription);
if(err && err.code == 409) return wireSubscription(topic.subscription(subscriptionName));
throw err;
});
...
}
According to this comment, it seems that the intended usage is to create the topic and subscription outside of the application content and never touch them afterwards. It’s not clear in the documentation that this is expected.
I think it would be great to either:
- Consolidate subscribe/subscription createTopic/topic into combined create/reuse functions (e.g. .topic() .subscription()). There can be an optional argument for erring instead of re-using. This hides all of this somewhat ugly logic from users that do want this use-case.
- Explicitly document that topics and subscriptions should be created beforehand.
I would personally prefer the former as it removes the need to write a “bootstrapping” script and simplifies the user-facing API.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 19 (11 by maintainers)
Commits related to this issue
- feat: adds style enumeration (#445) This PR was generated using Autosynth. :rainbow: Synth log will be available here: https://source.cloud.google.com/results/invocations/792bebba-dea1-4f73-8394-fb5... — committed to googleapis/google-cloud-node by yoshi-automation 3 years ago
- chore: release 3.11.0 (#446) :robot: I have created a release \*beep\* \*boop\* --- ## [3.11.0](https://www.github.com/googleapis/nodejs-asset/compare/v3.10.0...v3.11.0) (2021-01-09) ### Features ... — committed to googleapis/google-cloud-node by release-please[bot] 3 years ago
- build(secrets): begin migration to secret manager from keystore (#445) This PR was generated using Autosynth. :rainbow: Synth log will be available here: https://source.cloud.google.com/results/invo... — committed to googleapis/google-cloud-node by yoshi-automation 4 years ago
- build(secrets): begin migration to secret manager from keystore (#445) This PR was generated using Autosynth. :rainbow: Synth log will be available here: https://source.cloud.google.com/results/invo... — committed to googleapis/google-cloud-node by yoshi-automation 4 years ago
- fix: do not modify options object, use defaultScopes (#445) Regenerated the library using [gapic-generator-typescript](https://github.com/googleapis/gapic-generator-typescript) v1.2.1. — committed to googleapis/google-cloud-node by alexander-fenster 4 years ago
- chore: release 2.2.1 (#448) :robot: I have created a release \*beep\* \*boop\* --- ### [2.2.1](https://www.github.com/googleapis/nodejs-dataproc/compare/v2.2.0...v2.2.1) (2021-01-09) ### Bug Fixes... — committed to googleapis/google-cloud-node by release-please[bot] 3 years ago
- chore(deps): update dependency @types/mocha to v9 (#445) [![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | ... — committed to googleapis/google-cloud-node by renovate-bot 2 years ago
- chore(deps): update dependency @types/node to v18 (#445) [![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package... — committed to googleapis/google-cloud-node by renovate-bot 2 years ago
- feat: adds style enumeration (#445) This PR was generated using Autosynth. :rainbow: Synth log will be available here: https://source.cloud.google.com/results/invocations/792bebba-dea1-4f73-8394-fb5... — committed to googleapis/google-cloud-node by yoshi-automation 3 years ago
- chore: release 3.11.0 (#446) :robot: I have created a release \*beep\* \*boop\* --- ## [3.11.0](https://www.github.com/googleapis/nodejs-asset/compare/v3.10.0...v3.11.0) (2021-01-09) ### Features ... — committed to googleapis/google-cloud-node by release-please[bot] 3 years ago
- chore(deps): update dependency @types/node to v18 (#445) [![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package... — committed to googleapis/google-cloud-node by renovate-bot 2 years ago
- chore: add api_shortname and library_type to repo metadata (#445) — committed to googleapis/google-cloud-node by parthea 3 years ago
- chore: release 1.5.0 (#445) — committed to googleapis/google-cloud-node by yoshi-automation 5 years ago
- Regenerate README (#445) — committed to googleapis/google-cloud-node by deleted user 7 years ago
- Regenerate README (#445) — committed to googleapis/google-cloud-node by deleted user 7 years ago
- build: fix typo in synth.py (#445) — committed to googleapis/google-cloud-node by bcoe 4 years ago
- chore: regenerate common templates (#445) This PR was generated using Autosynth. :rainbow: Synth log will be available here: https://source.cloud.google.com/results/invocations/addf93bb-0c57-4d1f-8f... — committed to googleapis/google-cloud-node by yoshi-automation 3 years ago
I would like to share code to solve this problem.
After run it by
you can go to pages
http://localhost:8085/v1/projects/pure-plaform/topics http://localhost:8085/v1/projects/pure-plaform/subscriptions
and see
The approach we have taken for every object in this library is that you can create one or reference an existing one. If we changed this, it would need to be changed everywhere and would require two API requests rather than one, causing increased delays for responses.
Now actually, the latest pubsub v1beta2 (in this library master branch) call to create a topic/subscription is a PUT call meaning its idempotent (basically no matter how many times you call it, it won’t cause more effects than if you call it just once). This means that you can effectively “create or get” by always creating the topic regardless of whether it exists or not. If we know we don’t need to create it, it would be silly to try to every time we want to do something with it, this would multiply every request time by a factor of two and provide less stability overall.
Hope this makes sense. Let me know if there’s a smart compromise that could be made here.