1414import collections
1515import functools
1616import inspect
17+ import json
1718import logging
1819from contextvars import ContextVar
1920
@@ -215,9 +216,17 @@ def _maybe_create_pending(task, kwargs):
215216 for key in ("value_max" , "tags" ):
216217 if key in opts and opts [key ] is not None :
217218 create_kwargs [key ] = opts [key ]
218- user_data = opts .get ("data" )
219- if user_data :
220- create_kwargs ["data" ] = dict (user_data )
219+
220+ data = dict (opts .get ("data" ) or {})
221+
222+ record_args = opts .get ("record_task_args" )
223+ if record_args is None :
224+ record_args = bool (system ) and system .record_task_args
225+ if record_args :
226+ data ["procrastinate_task_kwargs" ] = _serialize_kwargs (kwargs )
227+
228+ if data :
229+ create_kwargs ["data" ] = data
221230
222231 tb_task = create_task_safe (name , ** create_kwargs )
223232 if tb_task is None :
@@ -228,6 +237,19 @@ def _maybe_create_pending(task, kwargs):
228237 return new_kwargs
229238
230239
240+ def _serialize_kwargs (kwargs ):
241+ """Return a JSON-roundtrippable copy of the defer kwargs.
242+
243+ Procrastinate already requires kwargs be JSON-serializable, so a json
244+ dumps/loads roundtrip is safe. Non-serializable values are dropped with
245+ a warning."""
246+ try :
247+ return json .loads (json .dumps (kwargs ))
248+ except (TypeError , ValueError ) as e :
249+ log .warning ("Error serializing task arguments: %s" , e )
250+ return {}
251+
252+
231253_TRACK_OPT_KEYS = ("name" , "value_max" , "tags" , "data" , "record_task_args" )
232254
233255
0 commit comments