1010import tempfile
1111from gettext import gettext as _
1212
13- from contextlib import suppress
14-
1513from django .conf import settings
1614from django .db import connection , transaction , IntegrityError
1715from django .db .models import Q
@@ -167,12 +165,16 @@ def __memory_diagnostic_decorator(task):
167165
168166 stop_event .set ()
169167 artifact = Artifact .init_and_validate (mem_diagnostics_file_path )
170- with suppress (IntegrityError ):
168+ try :
169+ # it is possible for the diagnostic artifact (memory report) to be identical to
170+ # a previous report, in which case we need to handle the case where saving a new
171+ # artifact fails.
171172 artifact .save ()
172- ProfileArtifact .objects .get_or_create (
173- artifact = artifact , name = "memory_profile" , task = task
174- )
175- _logger .info ("Created memory diagnostic data." )
173+ except IntegrityError :
174+ artifact = Artifact .objects .get (sha256 = artifact .sha256 )
175+
176+ ProfileArtifact .objects .get_or_create (artifact = artifact , name = "memory_profile" , task = task )
177+ _logger .info ("Created memory diagnostic data." )
176178
177179 return __memory_diagnostic_decorator
178180
@@ -191,12 +193,18 @@ def __pyinstrument_diagnostic_decorator(task):
191193 f .flush ()
192194
193195 artifact = Artifact .init_and_validate (str (profile_file_path ))
194- with suppress (IntegrityError ):
196+ try :
197+ # it is possible for the diagnostic artifact (memory report) to be identical to
198+ # a previous report, in which case we need to handle the case where saving a new
199+ # artifact fails.
195200 artifact .save ()
196- ProfileArtifact .objects .get_or_create (
197- artifact = artifact , name = "pyinstrument_profile" , task = task
198- )
199- _logger .info ("Created pyinstrument profile data." )
201+ except IntegrityError :
202+ artifact = Artifact .objects .get (sha256 = artifact .sha256 )
203+
204+ ProfileArtifact .objects .get_or_create (
205+ artifact = artifact , name = "pyinstrument_profile" , task = task
206+ )
207+ _logger .info ("Created pyinstrument profile data." )
200208 else :
201209 func (task )
202210
@@ -217,12 +225,18 @@ def __memray_diagnostic_decorator(task):
217225 func (task )
218226
219227 artifact = Artifact .init_and_validate (str (profile_file_path ))
220- with suppress (IntegrityError ):
228+ try :
229+ # it is possible for the diagnostic artifact (memory report) to be identical to
230+ # a previous report, in which case we need to handle the case where saving a new
231+ # artifact fails.
221232 artifact .save ()
222- ProfileArtifact .objects .get_or_create (
223- artifact = artifact , name = "memray_profile" , task = task
224- )
225- _logger .info ("Created memray memory profile data." )
233+ except IntegrityError :
234+ artifact = Artifact .objects .get (sha256 = artifact .sha256 )
235+
236+ ProfileArtifact .objects .get_or_create (
237+ artifact = artifact , name = "memray_profile" , task = task
238+ )
239+ _logger .info ("Created memray memory profile data." )
226240 else :
227241 func (task )
228242
0 commit comments