https://github.com/colorfulboard/pubsub_controller
Periodically fetch the Subscription of GCP Pub / Sub and execute the specified script.
https://github.com/colorfulboard/pubsub_controller
gcp gcp-pubsub python python27
Last synced: 4 months ago
JSON representation
Periodically fetch the Subscription of GCP Pub / Sub and execute the specified script.
- Host: GitHub
- URL: https://github.com/colorfulboard/pubsub_controller
- Owner: COLORFULBOARD
- License: apache-2.0
- Created: 2017-11-12T23:06:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-07T23:43:48.000Z (over 3 years ago)
- Last Synced: 2025-02-17T09:38:03.137Z (over 1 year ago)
- Topics: gcp, gcp-pubsub, python, python27
- Language: Python
- Homepage:
- Size: 91.8 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pub/Sub Controller
## Description
Fetch the Subscription of the GCP Pub/Sub regularly, and if there is a message execute the script specified by the key in the message.
GCP Pub/SubのSubscriptionを定期的にFetchし、メッセージがあればメッセージ内のキーで指定されたスクリプトを実行する。
## Installation and Try to this sample.
1. Create Pub/Sub topic and subscription on GCP. (ex: test-topic/test-sub)
2. `pip install pubsub_controller`
3. `pubsubcontroller init` and input your Pub/Sub setting
(ex: GCP_PROJECT_ID=your project id / SUBSCRIPTION_ID=test-sub)
4. `pubsubcontroller subscribe`
5. Subscriber will start immediately.
6. Open Another Terminal Window.
7. `pubsubcontroller publish test-topic test-message '{"target":"exec_sample","text":"test_text"}'`
8. In the Subscriber's window you will see the contents of the message you just published!
## How to add running process when message received.
1. Create a new python file under "exec_classes" directory.
- The same Python filename as the name specified by the attribute "target" key of the message to be published is executed.
2. Implement `def main (message_data, message_attr)` and describe what you want to do after receiving the message.
- "message_data" contains the contents of the received message.
- "message_attr" contains optional attributes of the received message.
## Details
- Settings
`pubsub_controller/settings.py` Required parameters are set here.
(It is set automatically by the `pubsubcontroller init` command)
- GCP PROJECT ID
Your GCP ProjectID
- SUBSCRIPTION ID
Enter the Subscription ID to be used.
If the Subscription name is `projects/hoge-project/subscriptions/fuga`, please enter `fuga`.
- Interval Second
Enter the interval to fetch Subscription in seconds.
- Subscriber
If you need a new subscriber, please refer to `apps/subscriber/pull/exec_classes/exec_sample.py` and create it.
- Pull Subscriber
This is a resident process that pulls Subscription.
- config
For reference, I am creating a supervisor config file.
`apps/subscriber/pull/config/pull_subscriber.ini`
- Publisher
Execute from the CLI or Python script and publish the message to the topic.
- Exec on CommandLine
- `pubsubcontroller publish test test-message '{"target":"exec_sample","text":"test_text"}'`
- arguments
- `arg1 = topic name`
- `arg2 = message data`
- `arg3 = message attribute(json format)`
- Exec on PythonCode
```python
from apps.publisher.publish_message import publish
publish('test-topic', 'test_data', {'target':'exec_sample','text':'test_text'})
```