File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -318,10 +318,16 @@ def get_or_create_engagement(
318318 target_end = (timezone .now () + timedelta (days = 365 )).date ()
319319 # Create the engagement
320320 with transaction .atomic ():
321- return Engagement .objects .select_for_update ().create (
321+ # Lock the parent product row to serialize engagement creation per product
322+ locked_product = Product .objects .select_for_update ().get (pk = product .pk )
323+ # Re-check for an existing engagement now that we hold the lock
324+ existing = get_last_object_or_none (Engagement , product = locked_product , name = engagement_name )
325+ if existing :
326+ return existing
327+ return Engagement .objects .create (
322328 engagement_type = "CI/CD" ,
323329 name = engagement_name ,
324- product = product ,
330+ product = locked_product ,
325331 lead = get_current_user (),
326332 target_start = target_start ,
327333 target_end = target_end ,
You can’t perform that action at this time.
0 commit comments