2121from sqlalchemy .orm import sessionmaker
2222import logging
2323
24+
2425from shared .common .logging_utils import get_env_logging_level
2526
2627
@@ -116,78 +117,6 @@ def mapper_configure_listener(mapper, class_):
116117event .listen (mapper , "mapper_configured" , mapper_configure_listener )
117118
118119
119- def create_refresh_materialized_view_task ():
120- """
121- Asynchronously refresh a materialized view.
122- Ensures deduplication by generating a unique task name.
123-
124- Returns:
125- dict: Response message and status code.
126- """
127- from google .protobuf import timestamp_pb2
128- from datetime import datetime , timedelta
129- from google .cloud import tasks_v2
130-
131- try :
132- logging .info ("Creating materialized view refresh task." )
133- now = datetime .now ()
134-
135- # BOUNCE WINDOW: next :00 or :30
136- minute = now .minute
137- if minute < 30 :
138- bucket_time = now .replace (minute = 30 , second = 0 , microsecond = 0 )
139- else :
140- bucket_time = now .replace (minute = 0 , second = 0 , microsecond = 0 ) + timedelta (hours = 1 )
141-
142- timestamp_str = bucket_time .strftime ("%Y-%m-%d-%H-%M" )
143- task_name = f"refresh-materialized-view-{ timestamp_str } "
144-
145- # Convert to protobuf timestamp
146- proto_time = timestamp_pb2 .Timestamp ()
147- proto_time .FromDatetime (bucket_time )
148-
149- # Cloud Tasks setup
150- client = tasks_v2 .CloudTasksClient ()
151- project = os .getenv ("PROJECT_ID" )
152- location = os .getenv ("LOCATION" )
153- queue = os .getenv ("MATERIALIZED_VIEW_QUEUE" )
154- url = (
155- f"https://{ os .getenv ('GCP_REGION' )} -"
156- f"{ os .getenv ('PROJECT_ID' )} .cloudfunctions.net/"
157- f"tasks-executor-{ os .getenv ('ENVIRONMENT_NAME' )} "
158- )
159-
160- task_name = client .task_path (project , location , queue , task_name )
161-
162- # Convert to protobuf timestamp
163- proto_time = timestamp_pb2 .Timestamp ()
164- proto_time .FromDatetime (bucket_time )
165-
166- # Enqueue the task
167- try :
168- create_http_task_with_name (
169- client = client ,
170- body = b"" ,
171- url = url ,
172- project_id = project ,
173- gcp_region = location ,
174- queue_name = queue ,
175- task_name = task_name ,
176- task_time = proto_time ,
177- http_method = tasks_v2 .HttpMethod .GET ,
178- )
179- logging .info (f"Scheduled refresh materialized view task for { timestamp_str } " )
180- return {"message" : f"Refresh task for { timestamp_str } scheduled." }, 200
181- except Exception as e :
182- if "ALREADY_EXISTS" in str (e ):
183- logging .info (f"Task already exists for { timestamp_str } , skipping." )
184-
185- except Exception as error :
186- error_msg = f"Error enqueuing task: { error } "
187- logging .error (error_msg )
188- return {"error" : error_msg }, 500
189-
190-
191120def refresh_materialized_view (session : "Session" , view_name : str ) -> bool :
192121 """
193122 Refresh Materialized view by name.
@@ -201,47 +130,6 @@ def refresh_materialized_view(session: "Session", view_name: str) -> bool:
201130 return False
202131
203132
204- def create_http_task_with_name (
205- client , # type: tasks_v2.CloudTasksClient
206- body : bytes ,
207- url : str ,
208- project_id : str ,
209- gcp_region : str ,
210- queue_name : str ,
211- task_name : str ,
212- task_time : str ,
213- http_method ,
214- ):
215- from google .cloud import tasks_v2
216-
217- """Creates a GCP Cloud Task."""
218-
219- token = (tasks_v2 .OidcToken (service_account_email = os .getenv ("SERVICE_ACCOUNT_EMAIL" )),)
220-
221- task = tasks_v2 .Task (
222- http_request = tasks_v2 .HttpRequest (
223- url = url ,
224- http_method = http_method ,
225- oidc_token = token ,
226- body = body ,
227- headers = {"Content-Type" : "application/json" },
228- )
229- )
230- task = {
231- "name" : task_name ,
232- "http_request" : {
233- "http_method" : http_method ,
234- "url" : url ,
235- "headers" : {
236- "Content-Type" : "application/json" ,
237- "Authorization" : f"Bearer { token } " ,
238- },
239- },
240- "schedule_time" : task_time ,
241- }
242- client .create_task (parent = client .queue_path (project_id , gcp_region , queue_name ), task = task )
243-
244-
245133def with_db_session (func = None , db_url : str | None = None ):
246134 """
247135 Decorator to handle the session management for the decorated function.
0 commit comments