Created May 28, 2015 16:14
-
-
Save tseaver/a2acd37a7c8a9a77d9e5 to your computer and use it in GitHub Desktop.
Multi-tenant pubsub example *without* Client
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
| """Demonstrate a "multi-tenant" pubsub application. | |
| Perform a filtered re-publication of events from a source topic onto | |
| another topic. | |
| Makes *no* use of the implicitly-derived connection/credentials or project ID. | |
| This version does *not* use :class:`gcloud.pubsub.client.Client`. | |
| It passes an explicit connection to each API-triggering method. | |
| It passes explicit project IDs to the :class:`gcloud.pubsub.topic.Topic` | |
| constructors. | |
| """ | |
| import atexit | |
| from gcloud.pubsub.connection import Connection | |
| from gcloud.pubsub.topic import Topic | |
| from gcloud.pubsub.subscription import Subscription | |
| source_conn = Connection.from_service_account_json('/path/to/source.json') | |
| target_conn = Connection.from_service_account_json('/path/to/target.json') | |
| source_topic = Topic('source-topic', project='source-project') | |
| assert source_topic.exists(connection=source_conn) # already set up | |
| source_sub = Subscription('pubsub_multitenant_client', source_topic) | |
| assert not source_sub.exists(connection=source_conn) # local to this script | |
| source_sub.create(connection=source_conn) | |
| atexit.register(source_sub.delete, connection=source_conn) | |
| target_topic = Topic('target-topic', project='target-project') | |
| assert target_topic.exists(connection=target_conn) # already set up | |
| while True: | |
| for ack_id, message in source_sub.pull( | |
| return_immediately=False, max_messages=1, connection=source_conn): | |
| source_sub.acknowledge(ack_id, connection=source_conn) | |
| if message.attributes.get('republish', False): | |
| target_topic.publish(message.data, connection=target_conn, | |
| **message.attributes) |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment