File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
django_declarative_apis/machinery Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 2626 import cid .locals
2727
2828 _get_correlation_id = cid .locals .get_cid
29+ _set_correlation_id = cid .locals .set_cid
2930except ImportError :
3031 _get_correlation_id = lambda : None # noqa: E731
32+ _set_correlation_id = lambda _ : None # noqa: E731
3133
3234JOB_COUNT_CACHE_KEY = "future_task_runner:job_id"
3335QUEUE_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 ,
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments