Skip to content

Commit f093eec

Browse files
authored
feat: set a schedule for executing export csv (#926)
1 parent 8bd3bd6 commit f093eec

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

functions-python/export_csv/function_config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "export-csv",
33
"description": "Export the DB feed data as a csv file",
44
"entry_point": "export_and_upload_csv",
5-
"timeout": 20,
6-
"memory": "1Gi",
5+
"timeout": 600,
6+
"memory": "2Gi",
77
"trigger_http": true,
88
"include_folders": ["helpers", "dataset_service"],
99
"include_api_folders": ["utils", "database", "feed_filters", "common", "database_gen"],

infra/functions-python/main.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,31 @@ resource "google_cloudfunctions2_function" "export_csv" {
891891
}
892892
}
893893
}
894+
}
894895

896+
resource "google_cloud_scheduler_job" "export_csv_scheduler" {
897+
name = "export-csv-scheduler-${var.environment}"
898+
description = "Schedule the export_csv function"
899+
time_zone = "Etc/UTC"
900+
schedule = var.export_csv_schedule
901+
region = var.gcp_region
902+
paused = var.environment == "prod" ? false : true
903+
depends_on = [google_cloudfunctions2_function.export_csv, google_cloudfunctions2_function_iam_member.export_csv_invoker]
904+
http_target {
905+
http_method = "POST"
906+
uri = google_cloudfunctions2_function.export_csv.url
907+
oidc_token {
908+
service_account_email = google_service_account.functions_service_account.email
909+
}
910+
headers = {
911+
"Content-Type" = "application/json"
912+
}
913+
}
914+
# Export CSV can take several minutes to run (5?) so we need to give it a longer deadline
915+
attempt_deadline = "600s"
895916
}
896917

918+
897919
# IAM entry for all users to invoke the function
898920
resource "google_cloudfunctions2_function_iam_member" "tokens_invoker" {
899921
project = var.project_id
@@ -1050,4 +1072,12 @@ resource "google_project_iam_member" "datastore_owner" {
10501072
project = var.project_id
10511073
role = "roles/datastore.owner"
10521074
member = "serviceAccount:${google_service_account.functions_service_account.email}"
1075+
}
1076+
1077+
resource "google_cloudfunctions2_function_iam_member" "export_csv_invoker" {
1078+
project = var.project_id
1079+
location = var.gcp_region
1080+
cloud_function = google_cloudfunctions2_function.export_csv.name
1081+
role = "roles/cloudfunctions.invoker"
1082+
member = "serviceAccount:${google_service_account.functions_service_account.email}"
10531083
}

infra/functions-python/vars.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ variable "gbfs_scheduler_schedule" {
6767

6868
variable "transitland_scraping_schedule" {
6969
type = string
70-
description = "Schedule for the GBFS scheduler job"
70+
description = "Schedule Transitland scraping job"
7171
default = "0 0 3 * *" # every month on the 3rd day at 00:00
7272
}
7373

@@ -79,4 +79,10 @@ variable "transitland_api_key" {
7979
variable "operations_oauth2_client_id" {
8080
type = string
8181
description = "value of the OAuth2 client id for the Operations API"
82+
}
83+
84+
variable "export_csv_schedule" {
85+
type = string
86+
description = "Schedule the export_csv function"
87+
default = "0 4 * * 2,5" # At 4am every Tuesday and Friday.
8288
}

0 commit comments

Comments
 (0)