diff --git a/.changes/unreleased/Features-20250213-135245.yaml b/.changes/unreleased/Features-20250213-135245.yaml new file mode 100644 index 0000000..0c9c00e --- /dev/null +++ b/.changes/unreleased/Features-20250213-135245.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Add "invocation_started_at" global field +time: 2025-02-13T13:52:45.156677-05:00 +custom: + Author: gshank + Issue: "245" diff --git a/dbt_common/invocation.py b/dbt_common/invocation.py index adbd7d1..2f7cfc8 100644 --- a/dbt_common/invocation.py +++ b/dbt_common/invocation.py @@ -1,12 +1,19 @@ import uuid +from datetime import datetime _INVOCATION_ID = str(uuid.uuid4()) +_INVOCATION_STARTED_AT = datetime.utcnow() def get_invocation_id() -> str: return _INVOCATION_ID +def get_invocation_started_at() -> datetime: + return _INVOCATION_STARTED_AT + + def reset_invocation_id() -> None: - global _INVOCATION_ID + global _INVOCATION_ID, _INVOCATION_STARTED_AT _INVOCATION_ID = str(uuid.uuid4()) + _INVOCATION_STARTED_AT = datetime.utcnow() diff --git a/tests/unit/test_invocation.py b/tests/unit/test_invocation.py new file mode 100644 index 0000000..d20f028 --- /dev/null +++ b/tests/unit/test_invocation.py @@ -0,0 +1,12 @@ +from dbt_common.invocation import get_invocation_id, get_invocation_started_at, reset_invocation_id + + +def test_invocation_started_at(): + inv_id = get_invocation_id() + assert inv_id + inv_start = get_invocation_started_at() + assert inv_start + + reset_invocation_id() + inv_start != get_invocation_started_at() + inv_id != get_invocation_id()