Skip to content

Commit 99306af

Browse files
committed
Check cache in task again before sending telemetry
1 parent 9eb386f commit 99306af

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

gramps_webapi/api/tasks.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@
5353
transaction_to_json,
5454
)
5555
from .search import get_search_indexer, get_semantic_search_indexer
56-
from .telemetry import get_telemetry_payload, send_telemetry, update_telemetry_timestamp
56+
from .telemetry import (
57+
get_telemetry_payload,
58+
send_telemetry,
59+
telemetry_sent_last_24h,
60+
update_telemetry_timestamp,
61+
)
5762
from .util import (
5863
abort_with_message,
5964
check_quota_people,
@@ -612,6 +617,13 @@ def update_search_indices_from_transaction(
612617
@shared_task()
613618
def send_telemetry_task(tree: str):
614619
"""Send telemetry"""
620+
if telemetry_sent_last_24h():
621+
# Although this task will only be called by the backend if it hasn't been
622+
# called in the last 24 hours, we check it here again because if a server
623+
# is misconfigured and the celery worker is run in a container that doesn't
624+
# have access to the same persistent cache as the backend, we don't want to
625+
# send telemetry every time a request hits the API.
626+
return None
615627
data = get_telemetry_payload(tree_id=tree)
616628
# if the request fails, an exception will be raised
617629
send_telemetry(data=data)

gramps_webapi/api/telemetry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def send_telemetry(data: dict[str, str | int | float]) -> None:
2424
response.raise_for_status() # Raise exception for HTTP errors
2525

2626

27+
def telemetry_sent_last_24h() -> bool:
28+
"""Check if telemetry has been sent in the last 24 hours."""
29+
return time.time() - telemetry_last_sent() < 24 * 60 * 60
30+
31+
2732
def should_send_telemetry() -> bool:
2833
"""Whether telemetry should be sent."""
2934
if current_app.config.get("DISABLE_TELEMETRY"):
@@ -37,7 +42,7 @@ def should_send_telemetry() -> bool:
3742
# do not send telemetry during tests unless MOCK_TELEMETRY is set
3843
return False
3944
# only send telemetry if it has not been sent in the last 24 hours
40-
if time.time() - telemetry_last_sent() < 24 * 60 * 60:
45+
if telemetry_sent_last_24h():
4146
return False
4247
return True
4348

0 commit comments

Comments
 (0)