Skip to content

Commit

Permalink
Merge pull request #35 from CruGlobal/fix-dbt-trigger
Browse files Browse the repository at this point in the history
Fix dbt trigger running errors
  • Loading branch information
tony-guan-cru authored Feb 3, 2025
2 parents f645770 + dcaec84 commit 4e95092
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions dbt-trigger/dbt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class DbtClient:
def __init__(self, access_token: str, account_id: str):
self.access_token = access_token
self.account_id = account_id
self.headers = {"Authorization": f"Bearer {access_token}"}
self.headers = {
"Authorization": f"Bearer {self.access_token}",
}
self.account_url = f"https://cloud.getdbt.com/api/v2/accounts/{account_id}"
self.base_url = "https://cloud.getdbt.com/api/v2"

Expand All @@ -31,7 +33,6 @@ def _request(self, url, data=None, params=None, method="GET"):

def trigger_job(self, job_id):
logger.info(f"Triggering dbt job {job_id} on account {self.account_id}")

response = self._request(
f"{self.account_url}/jobs/{job_id}/run/",
data={"cause": f"Triggered by Google Cloud Function"},
Expand Down
16 changes: 11 additions & 5 deletions dbt-trigger/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,21 @@ def trigger_dbt_job(request):
logger.exception("Failed to retrieve job_id")
raise

dbt_token = os.environ["DBT_TOKEN"]
dbt_token = os.environ.get("DBT_TOKEN", None).strip("\ufeff").strip()
if not dbt_token:
logger.exception("DBT token is missing or invalid.")
return "Internal error: missing DBT token", 500
account_id = "10206"
try:
client = DbtClient(access_token=dbt_token, account_id=account_id)
job_run_response = client.trigger_job(job_id)
run_id = job_run_response["data"]["id"]
if run_id is None:
logger.exception(f"dbt run failed to start.")
return
if not job_run_response:
logger.exception("No response received from the dbt job trigger endpoint.")
return "Failed to trigger dbt job", 500
run_id = job_run_response.get("data", {}).get("id")
if not run_id:
logger.exception("dbt job trigger response did not include a run ID.")
return "Failed to trigger dbt job", 500
logger.info(f"DBT run {run_id} started successfully.")
return "Trigger dbt job completed", 200
except Exception as e:
Expand Down

0 comments on commit 4e95092

Please sign in to comment.