Skip to content

Commit 6040669

Browse files
authored
Feature/deferred task correlation (#120)
* test for correlation id in deferred tasks update correlation_id if cid module installed Fixes #119 * update changelog
1 parent 9bf934e commit 6040669

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [0.25.2] - 2023-01-20
8+
### Fixed
9+
- [PR 120](https://github.com/salesforce/django-declarative-apis/pull/120) Fix correlation ID logging for deferred tasks
10+
711
# [0.25.1] - 2022-12-19
812
### Fixed
913
- [PR 117](https://github.com/salesforce/django-declarative-apis/pull/117) Fix README specification in pyproject.toml

django_declarative_apis/machinery/tasks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import cid.locals
2727

2828
_get_correlation_id = cid.locals.get_cid
29+
_set_correlation_id = cid.locals.set_cid
2930
except ImportError:
3031
_get_correlation_id = lambda: None # noqa: E731
32+
_set_correlation_id = lambda _: None # noqa: E731
3133

3234
JOB_COUNT_CACHE_KEY = "future_task_runner:job_id"
3335
QUEUE_LENGTH_CACHE_KEY = "future_task_runner:current_queue_length"
@@ -151,6 +153,8 @@ def future_task_runner(
151153
resource_instance = resource_class.objects.get(pk=resource_instance_id)
152154
endpoint_task = getattr(endpoint_class, endpoint_method_name)
153155

156+
_set_correlation_id(correlation_id)
157+
154158
_log_task_stats(
155159
endpoint_method_name,
156160
resource_instance_id,
@@ -291,6 +295,8 @@ def resource_task_runner(
291295
resource_instance = resource_class.objects.get(pk=resource_instance_id)
292296
resource_method = getattr(resource_instance, resource_method_name)
293297

298+
_set_correlation_id(correlation_id)
299+
294300
_log_task_stats(
295301
"{0}.{1}".format(resource_class_name, resource_method_name),
296302
resource_instance_id,

tests/machinery/test_base.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,38 @@ def setUp(self):
820820
"filtered_retry_count_2": 0,
821821
}
822822

823+
def test_future_task_runner_sets_cid(self):
824+
data = {"cid": None}
825+
826+
def set_correlation_id(cid):
827+
data["cid"] = cid
828+
829+
conf = tasks.future_task_runner.app.conf
830+
old_val = conf["task_always_eager"]
831+
conf["task_always_eager"] = True
832+
833+
old_set_cid = tasks._set_correlation_id
834+
old_get_cid = tasks._get_correlation_id
835+
tasks._set_correlation_id = set_correlation_id
836+
tasks._get_correlation_id = lambda: "cid-sentinel"
837+
try:
838+
expected_response = {"foo": "bar"}
839+
endpoint = _TestEndpoint(expected_response)
840+
manager = machinery.EndpointBinder.BoundEndpointManager(
841+
machinery._EndpointRequestLifecycleManager(endpoint), endpoint
842+
)
843+
machinery.EndpointBinder(endpoint).create_bound_endpoint(
844+
manager, HttpRequest()
845+
)
846+
847+
manager.get_response()
848+
finally:
849+
tasks._set_correlation_id = old_set_cid
850+
tasks._get_correlation_id = old_get_cid
851+
conf["task_always_eager"] = old_val
852+
853+
self.assertEqual("cid-sentinel", data["cid"])
854+
823855
def test_get_response_kombu_error_retried(self):
824856
expected_response = {"foo": "bar"}
825857
endpoint = _TestEndpoint(expected_response)

0 commit comments

Comments
 (0)