-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub.py
More file actions
34 lines (29 loc) · 1.18 KB
/
sub.py
File metadata and controls
34 lines (29 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os, uuid, time, json, random, datetime
from google.cloud import pubsub_v1
from google.api_core.exceptions import AlreadyExists
CHANNEL_MAP = {
22: "Cartoon",
23: "YoYo TV",
52: "News"
}
project_id = os.environ.get('GCP_PROJECT_ID') # Google Project Id
topic_id = "channel" # Topic Id
topic_path = f"projects/{project_id}/topics/{topic_id}"
container_name = os.environ.get('HOSTNAME')
sub = f"sub-{container_name}-{uuid.uuid4().hex}"
sub_path = f"projects/{project_id}/subscriptions/{sub}"
subscriber = pubsub_v1.SubscriberClient()
# 新建一個 Subscription
try:
subscriber.create_subscription(name=sub_path, topic=topic_path)
except AlreadyExists:
print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
f' [WARNING] Subscription already exists, sub_path: {sub_path}')
def callback(message):
data = json.loads(message.data.decode())
if type := data.get('type'):
if type == 'show_channel_info':
print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
f" [INFO] Message received, the channel is {CHANNEL_MAP.get(data.get('id'))}")
message.ack()
future = subscriber.subscribe(sub_path, callback)