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し、メッセージがあればメッセージ内のキーで指定されたスクリプトを実行する。
- Create Pub/Sub topic and subscription on GCP. (ex: test-topic/test-sub)
pip install pubsub_controller
pubsubcontroller init
and input your Pub/Sub setting
(ex: GCP_PROJECT_ID=your project id / SUBSCRIPTION_ID=test-sub)pubsubcontroller subscribe
- Subscriber will start immediately.
- Open Another Terminal Window.
pubsubcontroller publish test-topic test-message '{"target":"exec_sample","text":"test_text"}'
- In the Subscriber's window you will see the contents of the message you just published!
- 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.
- 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.
-
Settings
pubsub_controller/settings.py
Required parameters are set here.
(It is set automatically by thepubsubcontroller init
command)- GCP PROJECT ID
Your GCP ProjectID - SUBSCRIPTION ID
Enter the Subscription ID to be used.
If the Subscription name isprojects/hoge-project/subscriptions/fuga
, please enterfuga
. - Interval Second
Enter the interval to fetch Subscription in seconds.
- GCP PROJECT ID
-
Subscriber
If you need a new subscriber, please refer toapps/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
- config
-
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)
- arguments
- Exec on PythonCode
from apps.publisher.publish_message import publish publish('test-topic', 'test_data', {'target':'exec_sample','text':'test_text'})
- Exec on CommandLine