@@ -115,7 +115,10 @@ def add3(a, b):
115115 _instrument_task (add3 , system = None , manual = True )
116116
117117 tb = task_for_test ()
118- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create :
118+ with (
119+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create ,
120+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
121+ ):
119122 add3 .defer (a = 1 , b = 2 )
120123
121124 create .assert_called_once ()
@@ -137,7 +140,10 @@ def add_queued(a, b):
137140 _instrument_task (add_queued , system = None , manual = True )
138141
139142 tb = task_for_test ()
140- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create :
143+ with (
144+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create ,
145+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
146+ ):
141147 add_queued .defer (a = 1 , b = 2 )
142148
143149 assert create .call_args .kwargs ["queue" ] == "high_priority"
@@ -168,13 +174,69 @@ async def add5(a, b):
168174 _instrument_task (add5 , system = None , manual = True )
169175
170176 tb = task_for_test ()
171- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ):
177+ with (
178+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ),
179+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
180+ ):
172181 asyncio .run (add5 .defer_async (a = 1 , b = 2 ))
173182
174183 jobs = list (app .connector .jobs .values ())
175184 assert jobs [0 ]["args" ][TB_TASK_ID_KWARG ] == tb .id
176185
177186
187+ @pytest .mark .usefixtures ("_bind_settings" )
188+ def test_defer_records_job_id_as_external_id (app ):
189+ @app .task (name = "add_ext" )
190+ def add_ext (a , b ):
191+ return a + b
192+
193+ _instrument_task (add_ext , system = None , manual = True )
194+
195+ tb = task_for_test ()
196+ with (
197+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ),
198+ mock .patch ("taskbadger.procrastinate.update_task_safe" ) as update ,
199+ ):
200+ job_id = add_ext .defer (a = 1 , b = 2 )
201+
202+ update .assert_called_once_with (tb .id , external_id = str (job_id ))
203+
204+
205+ @pytest .mark .usefixtures ("_bind_settings" )
206+ def test_defer_async_records_job_id_as_external_id (app ):
207+ @app .task (name = "add_ext_async" )
208+ async def add_ext_async (a , b ):
209+ return a + b
210+
211+ _instrument_task (add_ext_async , system = None , manual = True )
212+
213+ tb = task_for_test ()
214+ with (
215+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ),
216+ mock .patch ("taskbadger.procrastinate.update_task_safe" ) as update ,
217+ ):
218+ job_id = asyncio .run (add_ext_async .defer_async (a = 1 , b = 2 ))
219+
220+ update .assert_called_once_with (tb .id , external_id = str (job_id ))
221+
222+
223+ def test_defer_no_external_id_when_untracked (app ):
224+ @app .task (name = "add_untracked" )
225+ def add_untracked (a , b ):
226+ return a + b
227+
228+ _instrument_task (add_untracked , system = None , manual = True )
229+
230+ # Badger is not configured, so no pending task is created and nothing to update.
231+ with (
232+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = None ),
233+ mock .patch ("taskbadger.procrastinate.update_task_safe" ) as update ,
234+ ):
235+ add_untracked .defer (a = 1 , b = 2 )
236+
237+ update .assert_not_called ()
238+
239+
178240@pytest .mark .usefixtures ("_bind_settings" )
179241def test_end_to_end_via_worker (app ):
180242 @app .task (name = "add6" )
@@ -194,7 +256,7 @@ def add6(a, b):
194256 app .run_worker (wait = False , install_signal_handlers = False , listen_notify = False )
195257
196258 create .assert_called_once ()
197- statuses = [c .kwargs ["status" ] for c in update .call_args_list ]
259+ statuses = [c .kwargs ["status" ] for c in update .call_args_list if "status" in c . kwargs ]
198260 assert statuses == [StatusEnum .PROCESSING , StatusEnum .SUCCESS ]
199261
200262
@@ -206,7 +268,10 @@ def bare(a):
206268 return a
207269
208270 tb = task_for_test ()
209- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ):
271+ with (
272+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ),
273+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
274+ ):
210275 bare .defer (a = 1 )
211276
212277 assert getattr (bare , "_taskbadger_manual" ) is True
@@ -223,7 +288,10 @@ def raw(a):
223288 return a
224289
225290 tb = task_for_test ()
226- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create :
291+ with (
292+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create ,
293+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
294+ ):
227295 raw .defer (a = 1 )
228296
229297 create .assert_called_once ()
@@ -248,7 +316,10 @@ def dup(a):
248316 # Two @track applications must not double-wrap; defer once still creates one
249317 # PENDING task and injects one id.
250318 tb = task_for_test ()
251- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create :
319+ with (
320+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create ,
321+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
322+ ):
252323 dup .defer (a = 1 )
253324 assert create .call_count == 1
254325 jobs = list (app .connector .jobs .values ())
@@ -312,7 +383,7 @@ def self_complete():
312383
313384 # The wrapper's post-call SUCCESS update is skipped because the cached
314385 # task is already SUCCESS. PROCESSING update is still allowed (early path).
315- statuses = [c .kwargs ["status" ] for c in update .call_args_list ]
386+ statuses = [c .kwargs ["status" ] for c in update .call_args_list if "status" in c . kwargs ]
316387 assert StatusEnum .PROCESSING in statuses
317388 # Last attempted SUCCESS call should be suppressed
318389 assert statuses .count (StatusEnum .SUCCESS ) == 0
@@ -326,7 +397,10 @@ def recorder(a, b):
326397 return a + b
327398
328399 tb = task_for_test ()
329- with mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create :
400+ with (
401+ mock .patch ("taskbadger.procrastinate.create_task_safe" , return_value = tb ) as create ,
402+ mock .patch ("taskbadger.procrastinate.update_task_safe" ),
403+ ):
330404 recorder .defer (a = 5 , b = 6 )
331405
332406 assert create .call_args .kwargs ["data" ] == {
0 commit comments