Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.25.2] - 2023-01-20
### Fixed
- [PR 120](https://github.com/salesforce/django-declarative-apis/pull/120) Fix correlation ID logging for deferred tasks

# [0.25.1] - 2022-12-19
### Fixed
- [PR 117](https://github.com/salesforce/django-declarative-apis/pull/117) Fix README specification in pyproject.toml
Expand Down
6 changes: 6 additions & 0 deletions django_declarative_apis/machinery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import cid.locals
Copy link
Collaborator

@bgrant bgrant Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we use it in testing, should we add django-cid to our optional-dependencies?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're actually mocking it in testing, so it's not necessary. What's the rule for putting things in optional-dependencies? It doesn't seem to simplify installing things with pip

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh- maybe you overload the necessary method in testing. I'm trying to think about where, if anywhere, django-cid should go in our listed dependencies in pyproject.toml. Maybe nowhere, and (ideally) we would just document that DDA supports it somewhere (README or docs). That doesn't need to be in this PR.


_get_correlation_id = cid.locals.get_cid
_set_correlation_id = cid.locals.set_cid
except ImportError:
_get_correlation_id = lambda: None # noqa: E731
_set_correlation_id = lambda _: None # noqa: E731

JOB_COUNT_CACHE_KEY = "future_task_runner:job_id"
QUEUE_LENGTH_CACHE_KEY = "future_task_runner:current_queue_length"
Expand Down Expand Up @@ -151,6 +153,8 @@ def future_task_runner(
resource_instance = resource_class.objects.get(pk=resource_instance_id)
endpoint_task = getattr(endpoint_class, endpoint_method_name)

_set_correlation_id(correlation_id)

_log_task_stats(
endpoint_method_name,
resource_instance_id,
Expand Down Expand Up @@ -291,6 +295,8 @@ def resource_task_runner(
resource_instance = resource_class.objects.get(pk=resource_instance_id)
resource_method = getattr(resource_instance, resource_method_name)

_set_correlation_id(correlation_id)

_log_task_stats(
"{0}.{1}".format(resource_class_name, resource_method_name),
resource_instance_id,
Expand Down
32 changes: 32 additions & 0 deletions tests/machinery/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,38 @@ def setUp(self):
"filtered_retry_count_2": 0,
}

def test_future_task_runner_sets_cid(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test covering when cid is not present?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically every existing test is checking that condition

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Thanks!

data = {"cid": None}

def set_correlation_id(cid):
data["cid"] = cid

conf = tasks.future_task_runner.app.conf
old_val = conf["task_always_eager"]
conf["task_always_eager"] = True

old_set_cid = tasks._set_correlation_id
old_get_cid = tasks._get_correlation_id
tasks._set_correlation_id = set_correlation_id
tasks._get_correlation_id = lambda: "cid-sentinel"
try:
expected_response = {"foo": "bar"}
endpoint = _TestEndpoint(expected_response)
manager = machinery.EndpointBinder.BoundEndpointManager(
machinery._EndpointRequestLifecycleManager(endpoint), endpoint
)
machinery.EndpointBinder(endpoint).create_bound_endpoint(
manager, HttpRequest()
)

manager.get_response()
finally:
tasks._set_correlation_id = old_set_cid
tasks._get_correlation_id = old_get_cid
conf["task_always_eager"] = old_val

self.assertEqual("cid-sentinel", data["cid"])

def test_get_response_kombu_error_retried(self):
expected_response = {"foo": "bar"}
endpoint = _TestEndpoint(expected_response)
Expand Down