@@ -168,7 +168,7 @@ def _write_resources(self, resources: dict) -> None:
168168 blob_json = json .dumps (resource_dict , indent = 4 )
169169
170170 blob .upload_from_string (blob_json , content_type = "application/json" )
171- print (f"Resources written to { self .bucket_name } /{ STORAGE_PREFIX } { self .resource_type } .json" )
171+ print (f"{ self . clock () } - Resources written to { self .bucket_name } /{ STORAGE_PREFIX } { self .resource_type } .json" )
172172
173173 def _stored_resources (self ) -> dict :
174174 """
@@ -179,7 +179,7 @@ def _stored_resources(self) -> dict:
179179 blob = bucket .blob (f"{ STORAGE_PREFIX } { self .resource_type } .json" )
180180
181181 if not blob .exists ():
182- print (f"Blob { self .bucket_name } /{ STORAGE_PREFIX } { self .resource_type } .json does not exist." )
182+ print (f"{ self . clock () } - Blob { self .bucket_name } /{ STORAGE_PREFIX } { self .resource_type } .json does not exist." )
183183 return {}
184184
185185 blob_string = blob .download_as_text ()
@@ -211,7 +211,7 @@ def refresh(self) -> None:
211211
212212 for k , v in list (stored_resources .items ()):
213213 if k not in active_resources :
214- print (f"Resource { k } is no longer alive. Deleting it from the stored resources." )
214+ print (f"{ self . clock () } - Resource { k } is no longer alive. Deleting it from the stored resources." )
215215 del stored_resources [k ]
216216 else :
217217 v .update (clock = self .clock )
@@ -272,12 +272,12 @@ def delete_stale(self, dry_run: bool = True) -> None:
272272 for k , v in stale_resources_map .items ():
273273 if k in active_resources_map :
274274 if dry_run :
275- print (f"Dry run: Would delete resource { k } " )
275+ print (f"{ self . clock () } - Dry run: Would delete resource { k } " )
276276 else :
277- print (f"Deleting resource { k } " )
277+ print (f"{ self . clock () } - Deleting resource { k } " )
278278 self ._delete_resource (k )
279279 else :
280- print (f"Resource { k } marked as stale but no longer exists in GCP. Skipping deletion." )
280+ print (f"{ self . clock () } - Resource { k } marked as stale but no longer exists in GCP. Skipping deletion." )
281281
282282 if not dry_run :
283283 self .refresh ()
@@ -302,5 +302,36 @@ def _active_resources(self) -> dict:
302302
303303 def _delete_resource (self , resource_name : str ) -> None :
304304 topic_name = resource_name .split ('/' )[- 1 ]
305- print (f"Deleting PubSub topic { topic_name } " )
305+ print (f"{ self . clock () } - Deleting PubSub topic { topic_name } " )
306306 self .client .delete_topic (name = resource_name )
307+
308+ if __name__ == "__main__" :
309+ project_id = DEFAULT_PROJECT_ID
310+ bucket_name = DEFAULT_BUCKET_NAME
311+
312+ # Prefixes found after analyzing the PubSub topics in the project
313+ prefixes = [
314+ "psit_topic_input" ,
315+ "psit_topic_output" ,
316+ "wc_topic_input" ,
317+ "wc_topic_output" ,
318+ "leader_board_it_input_topic" ,
319+ "leader_board_it_output_topic" ,
320+ "exercise_streaming_metrics_topic_input" ,
321+ "exercise_streaming_metrics_topic_output" ,
322+ "pubsub_io_performance" ,
323+ "testing" ,
324+ "pubsubNamespace" ,
325+ "game_stats_it_input_topic" ,
326+ "game_stats_it_output_topic"
327+ ]
328+
329+ # Create a PubSubTopicCleaner instance
330+ cleaner = PubSubTopicCleaner (project_id = project_id , bucket_name = bucket_name ,
331+ prefixes = prefixes , time_threshold = DEFAULT_PUBSUB_TOPIC_THRESHOLD )
332+
333+ # Refresh resources
334+ cleaner .refresh ()
335+
336+ # Delete stale resources
337+ cleaner .delete_stale ()
0 commit comments