@@ -34,41 +34,35 @@ def create_refresh_materialized_view_task():
3434 proto_time .FromDatetime (bucket_time )
3535
3636 # Cloud Tasks setup
37- client = tasks_v2 .CloudTasksClient ()
3837 project = os .getenv ("PROJECT_ID" )
39- location = os .getenv ("LOCATION" )
4038 queue = os .getenv ("MATERIALIZED_VIEW_QUEUE" )
41- url = (
42- f"https://{ os .getenv ('GCP_REGION' )} -"
43- f"{ os .getenv ('PROJECT_ID' )} .cloudfunctions.net/"
44- f"tasks-executor-{ os .getenv ('ENVIRONMENT_NAME' )} "
45- )
46-
47- task_name = client .task_path (project , location , queue , task_name )
39+ logging .debug ("Queue name from env: %s" , queue )
40+ gcp_region = os .getenv ("GCP_REGION" )
41+ environment_name = os .getenv ("ENVIRONMENT" )
42+ url = f"https://{ gcp_region } -" f"{ project } .cloudfunctions.net/" f"tasks-executor-{ environment_name } "
4843
4944 # Enqueue the task
5045 try :
5146 create_http_task_with_name (
52- client = client ,
47+ client = tasks_v2 . CloudTasksClient () ,
5348 body = b"" ,
5449 url = url ,
5550 project_id = project ,
56- gcp_region = location ,
51+ gcp_region = gcp_region ,
5752 queue_name = queue ,
5853 task_name = task_name ,
5954 task_time = proto_time ,
6055 http_method = tasks_v2 .HttpMethod .GET ,
6156 )
62- logging .info (f "Scheduled refresh materialized view task for { timestamp_str } " )
63- return {"message" : f "Refresh task for { timestamp_str } scheduled." }, 200
57+ logging .info ("Scheduled refresh materialized view task for %s" , task_name )
58+ return {"message" : "Refresh task for %s scheduled." % task_name }, 200
6459 except Exception as e :
6560 if "ALREADY_EXISTS" in str (e ):
66- logging .info (f "Task already exists for { timestamp_str } , skipping." )
61+ logging .info ("Task already exists for %s , skipping." , task_name )
6762
6863 except Exception as error :
69- error_msg = f"Error enqueuing task: { error } "
70- logging .error (error_msg )
71- return {"error" : error_msg }, 500
64+ logging .error ("Error enqueuing task: %s" , error )
65+ return {"error" : "Error enqueuing task: %s" % error }, 500
7266
7367
7468def create_http_task_with_name (
@@ -83,11 +77,13 @@ def create_http_task_with_name(
8377 http_method : "tasks_v2.HttpMethod" ,
8478):
8579 """Creates a GCP Cloud Task."""
86-
8780 token = tasks_v2 .OidcToken (service_account_email = os .getenv ("SERVICE_ACCOUNT_EMAIL" ))
8881
82+ parent = client .queue_path (project_id , gcp_region , queue_name )
83+ logging .info ("Queue parent path: %s" , parent )
84+
8985 task = tasks_v2 .Task (
90- name = task_name ,
86+ name = f" { parent } /tasks/ { task_name } " ,
9187 schedule_time = task_time ,
9288 http_request = tasks_v2 .HttpRequest (
9389 url = url ,
@@ -97,4 +93,10 @@ def create_http_task_with_name(
9793 headers = {"Content-Type" : "application/json" },
9894 ),
9995 )
100- client .create_task (parent = client .queue_path (project_id , gcp_region , queue_name ), task = task )
96+ logging .info ("Task created with task_name: %s" , task_name )
97+ try :
98+ response = client .create_task (parent = parent , task = task )
99+ except Exception as e :
100+ logging .error ("Error creating task: %s" , e )
101+ logging .error ("response: %s" , response )
102+ logging .info ("Successfully created task in create_http_task_with_name" )
0 commit comments