11import json
2- import logging
32import os
43import threading
54import uuid
65from typing import List
6+ from concurrent import futures
77
8- from database_gen . sqlacodegen_models import Feed
8+ from google . auth import default
99from google .cloud import pubsub_v1
1010from google .cloud .pubsub_v1 .futures import Future
1111
12- env = os . getenv ( "ENV" , "dev" )
13- pubsub_topic_name = f"datasets-batch-topic- { env } "
14- project_id = f"mobility-feeds- { env } "
12+ from database_gen . sqlacodegen_models import Feed
13+ from utils . logger import Logger
14+
1515# Lazy create so we won't try to connect to google cloud when the file is imported.
1616pubsub_client = None
1717
1818lock = threading .Lock ()
19+ logger = Logger ("load_dataset_on_create" ).get_logger ()
1920
2021
2122def get_pubsub_client ():
2223 with lock :
2324 global pubsub_client
2425 if pubsub_client is None :
2526 pubsub_client = pubsub_v1 .PublisherClient ()
27+
2628 return pubsub_client
2729
2830
2931def get_topic_path ():
30- if pubsub_topic_name is None or project_id is None :
31- raise ValueError ( "PUBSUB_TOPIC_NAME and PROJECT_ID must be set in the environment" )
32-
32+ env = os . getenv ( "ENV" , "dev" )
33+ pubsub_topic_name = f"datasets-batch-topic- { env } "
34+ project_id = f"mobility-feeds- { env } " # Cannot use GOOGLE_CLOUD_PROJECT because it points to QA for DEV
3335 return get_pubsub_client ().topic_path (project_id , pubsub_topic_name )
3436
3537
@@ -42,16 +44,17 @@ def publish_callback(future: Future, stable_id: str, topic_path: str):
4244 @param topic_path: The path to the Pub/Sub topic
4345 """
4446 if future .exception ():
45- logging .info (f"Error publishing feed { stable_id } to Pub/Sub topic { topic_path } : { future .exception ()} " )
47+ logger .info (f"Error publishing feed { stable_id } to Pub/Sub topic { topic_path } : { future .exception ()} " )
4648 else :
47- logging .info (f"Published stable_id = { stable_id } ." )
49+ logger .info (f"Published stable_id = { stable_id } ." )
4850
4951
50- def publish (feed : Feed , topic_path : str ):
52+ def publish (feed : Feed , topic_path : str ) -> Future :
5153 """
5254 Publishes a feed to the Pub/Sub topic.
5355 :param feed: The feed to publish
5456 :param topic_path: The path to the Pub/Sub topic
57+ :return: The Future object representing the result of the publishing operation
5558 """
5659 payload = {
5760 "execution_id" : f"batch-uuid-{ uuid .uuid4 ()} " ,
@@ -67,6 +70,7 @@ def publish(feed: Feed, topic_path: str):
6770 data_bytes = json .dumps (payload ).encode ("utf-8" )
6871 future = get_pubsub_client ().publish (topic_path , data = data_bytes )
6972 future .add_done_callback (lambda _ : publish_callback (future , feed .stable_id , topic_path ))
73+ return future
7074
7175
7276def publish_all (feeds : List [Feed ]):
@@ -75,6 +79,14 @@ def publish_all(feeds: List[Feed]):
7579 :param feeds: The list of feeds to publish
7680 """
7781 topic_path = get_topic_path ()
82+ logger .info (f"Publishing { len (feeds )} feeds to Pub/Sub topic { topic_path } ..." )
83+ credentials , project = default ()
84+ logger .info (f"Authenticated project: { project } " )
85+ logger .info (f"Service Account Email: { credentials .service_account_email } " )
86+ publish_futures = []
7887 for feed in feeds :
79- publish (feed , topic_path )
80- logging .info (f"Published { len (feeds )} feeds to Pub/Sub topic { topic_path } ." )
88+ logger .info (f"Publishing feed { feed .stable_id } to Pub/Sub topic { topic_path } ..." )
89+ future = publish (feed , topic_path )
90+ publish_futures .append (future )
91+ futures .wait (publish_futures , return_when = futures .ALL_COMPLETED )
92+ logger .info (f"Published { len (feeds )} feeds to Pub/Sub topic { topic_path } ." )
0 commit comments