@@ -203,7 +203,7 @@ def insert(cls, jobs_data, queue=None, statuses_no_storage=None, return_jobs=Tru
203203
204204 def _attach_original_exception (self , exc ):
205205 """ Often, a retry will be raised inside an "except" block.
206- This Keep track of the first exception for debugging purposes. """
206+ This Keep track of the first exception for debugging purposes """
207207
208208 original_exception = sys .exc_info ()
209209 if original_exception [0 ] is not None :
@@ -350,6 +350,27 @@ def save_retry(self, retry_exc):
350350
351351 self ._save_status ("retry" , updates , exception = True )
352352
353+ def _save_traceback_history (self , status , trace , job_exc ):
354+ """ Create traceback history or add a new traceback to history. """
355+ failure_date = datetime .datetime .utcnow ()
356+
357+ new_history = {
358+ "date" : failure_date ,
359+ "status" : status ,
360+ "exceptiontype" : job_exc .__name__
361+ }
362+
363+ traces = trace .split ("---- Original exception: -----" )
364+ if len (traces ) > 1 :
365+ new_history ["original_traceback" ] = traces [1 ]
366+ worker = context .get_current_worker ()
367+ if worker :
368+ new_history ["worker" ] = worker .id
369+ new_history ["traceback" ] = traces [0 ]
370+ self .collection .update ({
371+ "_id" : self .id
372+ }, {"$push" : {"traceback_history" : new_history }})
373+
353374 def save_success (self , result = None ):
354375
355376 dateexpires = datetime .datetime .utcnow () + datetime .timedelta (seconds = self .result_ttl )
@@ -413,7 +434,12 @@ def _save_status(self, status, updates=None, exception=False, w=None, j=None):
413434 trace = traceback .format_exc ()
414435 context .log .error (trace )
415436 db_updates ["traceback" ] = trace
416- db_updates ["exceptiontype" ] = sys .exc_info ()[0 ].__name__
437+ exc = sys .exc_info ()[0 ]
438+ db_updates ["exceptiontype" ] = exc .__name__
439+
440+ if context .get_current_config ().get ("save_traceback_history" ):
441+
442+ self ._save_traceback_history (status , trace , exc )
417443
418444 # In the most common case, we allow an optimization on Mongo writes
419445 if status == "success" :
0 commit comments