2424 # Create a GCS consumer object
2525 c = consume.GCSKafkaConsumer(
2626 kafka_config=config,
27- bucket_name='<PROJECT_ID>_ztf_alert_avro_bucket',
2827 kafka_topic='my_kafka_topic_name',
29- pubsub_topic='ztf_alert_avro_in_bucket',
28+ bucket_name='<PROJECT_ID>_ztf_alert_avro_bucket',
29+ pubsub_alert_data_topic='ztf_alert_data',
30+ pubsub_in_GCS_topic='ztf_alert_avro_in_bucket',
3031 debug=True # Use debug to run without updating your kafka offset
3132 )
3233
6061from confluent_kafka import Consumer , KafkaException
6162
6263from broker import exceptions
64+ from broker .pub_sub_client .message_service import publish_pubsub
6365
6466if not os .getenv ('GPB_OFFLINE' , False ):
6567 from google .cloud import pubsub , storage
@@ -143,25 +145,28 @@ def __init__(
143145 kafka_config : dict ,
144146 kafka_topic : str ,
145147 bucket_name : str ,
146- pubsub_topic : str ,
148+ pubsub_alert_data_topic : str ,
149+ pubsub_in_GCS_topic : str ,
147150 debug : bool = False ):
148151 """Ingests data from a kafka stream and stores a copy in GCS
149152
150- Storage bucket and PubSub topic must already exist and have
153+ Storage bucket and PubSub topics must already exist and have
151154 appropriate permissions.
152155
153156 Args:
154157 kafka_config: Kafka consumer configuration properties
155158 kafka_topic: Kafka topics to subscribe to
156- bucket_name: Name of the bucket to upload into
157- pubsub_topic: PubSub topic to publish to
159+ bucket_name: Name of the CGS bucket to upload into
160+ pubsub_alert_data_topic: PubSub topic for alert data
161+ pubsub_in_GCS_topic: PubSub topic for "alert in GCS" notifications
158162 debug: Run without committing Kafka position
159163 """
160164
161165 self ._debug = debug
162166 self .kafka_topic = kafka_topic
163167 self .bucket_name = bucket_name
164- self .pubsub_topic = pubsub_topic
168+ self .pubsub_alert_data_topic = pubsub_alert_data_topic
169+ self .pubsub_in_GCS_topic = pubsub_in_GCS_topic
165170 self .kafka_server = kafka_config ["bootstrap.servers" ]
166171 log .info (f'Initializing consumer: { self .__repr__ ()} ' )
167172
@@ -175,15 +180,6 @@ def __init__(
175180 self .bucket = self .storage_client .get_bucket (bucket_name )
176181 log .info (f'Connected to bucket: { self .bucket .name } ' )
177182
178- # Configure PubSub topic
179- project_id = os .getenv ('GOOGLE_CLOUD_PROJECT' )
180- self .publisher = pubsub .PublisherClient ()
181- self .topic_path = self .publisher .topic_path (project_id , pubsub_topic )
182-
183- # Raise error if topic does not exist
184- self .topic = self .publisher .get_topic (self .topic_path )
185- log .info (f'Connected to PubSub: { self .topic_path } ' )
186-
187183 def close (self ) -> None :
188184 """Close down and terminate the Kafka Consumer"""
189185
@@ -252,21 +248,6 @@ def upload_bytes_to_bucket(self, data: bytes, destination_name: str) -> None:
252248 self .fix_schema (temp_file , survey , version )
253249 blob .upload_from_file (temp_file )
254250
255- def publish_pubsub (self , message : str ) -> Future :
256- """Publish a PubSub alert
257-
258- Args:
259- message: The message to publish
260-
261- Returns:
262- The Id of the published message
263- """
264-
265- log .debug (f'Publishing message: { message } ' )
266- message_data = message .encode ('UTF-8' )
267- future = self .publisher .publish (self .topic_path , data = message_data )
268- return future .result ()
269-
270251 def run (self ) -> None :
271252 """Ingest kafka Messages to GCS and PubSub"""
272253
@@ -288,8 +269,10 @@ def run(self) -> None:
288269 file_name = f'{ timestamp } .avro'
289270
290271 log .debug (f'Ingesting { file_name } ' )
272+ publish_pubsub (self .pubsub_in_GCS_topic , file_name .encode ('UTF-8' ))
273+ publish_pubsub (self .pubsub_alert_data_topic , msg .value ())
291274 self .upload_bytes_to_bucket (msg .value (), file_name )
292- self . publish_pubsub ( file_name )
275+
293276 if not self ._debug :
294277 self .commit ()
295278
@@ -307,7 +290,8 @@ def __repr__(self) -> str:
307290 f'kafka_server: { self .kafka_server } , '
308291 f'kafka_topic: { self .kafka_topic } , '
309292 f'bucket_name: { self .bucket_name } , '
310- f'pubsub_topic: { self .pubsub_topic } '
293+ f'pubsub_alert_data_topic: { self .pubsub_alert_data_topic } '
294+ f'pubsub_in_GCS_topic: { self .pubsub_in_GCS_topic } '
311295 ')>'
312296 )
313297
0 commit comments