@@ -440,31 +440,10 @@ def dispatch(
440440 # are_resources_available() now acquires ALL locks atomically
441441 if are_resources_available (task ):
442442 # All locks acquired successfully
443- # Reload task state from database to check if it's still WAITING
444- # (a worker might have executed it between creation and lock acquisition)
445- task .refresh_from_db ()
446-
447- if task .state != TASK_STATES .WAITING :
448- # Task was already executed by a worker, release locks and return
449- _logger .info (
450- "IMMEDIATE DISPATCH: Task %s already in state "
451- "'%s', releasing locks without execution" ,
452- task .pk ,
453- task .state ,
454- )
455- safe_release_task_locks (task )
456- return task
457-
458- # Task is still WAITING, proceed with execution
443+ # Proceed with execution
459444 current_app = AppStatus .objects .current ()
460445 lock_owner = current_app .name if current_app else f"immediate-{ task .pk } "
461446 try :
462- _logger .info (
463- "IMMEDIATE DISPATCH: Task %s acquired all locks,"
464- " executing in API process (AppStatus=%s)" ,
465- task .pk ,
466- lock_owner ,
467- )
468447 with using_workdir ():
469448 execute_task (task )
470449 except Exception :
@@ -474,10 +453,10 @@ def dispatch(
474453 raise
475454 elif deferred :
476455 # Locks not available, defer to worker
456+ # Clear app_lock so workers can pick this up
477457 # No locks were acquired (atomic operation failed), so nothing to clean up
478- _logger .info (
479- "IMMEDIATE DISPATCH: Task %s could not acquire locks, deferring to worker" , task .pk
480- )
458+ Task .objects .filter (pk = task .pk ).update (app_lock = None )
459+ task .app_lock = None
481460 else :
482461 # Can't acquire locks and can't be deferred - cancel task
483462 # No locks were acquired, so just set state
@@ -513,32 +492,10 @@ async def adispatch(
513492 # async_are_resources_available() now acquires ALL locks atomically
514493 if await async_are_resources_available (task ):
515494 # All locks acquired successfully
516- # Reload task state from database to check if it's still WAITING
517- # (a worker might have executed it between creation and lock acquisition)
518- await task .arefresh_from_db ()
519-
520- if task .state != TASK_STATES .WAITING :
521- # Task was already executed by a worker, release locks and return
522- _logger .info (
523- "IMMEDIATE DISPATCH (async): Task %s already in"
524- " state '%s', releasing locks without execution" ,
525- task .pk ,
526- task .state ,
527- )
528- await async_safe_release_task_locks (task )
529- return task
530-
531- # Task is still WAITING, proceed with execution
495+ # Proceed with execution
532496 current_app = await sync_to_async (AppStatus .objects .current )()
533497 lock_owner = current_app .name if current_app else f"immediate-{ task .pk } "
534498 try :
535- _logger .info (
536- "IMMEDIATE DISPATCH (async): Task %s acquired "
537- "all locks, executing in API process "
538- "(AppStatus=%s)" ,
539- task .pk ,
540- lock_owner ,
541- )
542499 with using_workdir ():
543500 await aexecute_task (task )
544501 except Exception :
@@ -548,11 +505,10 @@ async def adispatch(
548505 raise
549506 elif deferred :
550507 # Locks not available, defer to worker
508+ # Clear app_lock so workers can pick this up
551509 # No locks were acquired (atomic operation failed), so nothing to clean up
552- _logger .info (
553- "IMMEDIATE DISPATCH (async): Task %s could not acquire locks, deferring to worker" ,
554- task .pk ,
555- )
510+ await Task .objects .filter (pk = task .pk ).aupdate (app_lock = None )
511+ task .app_lock = None
556512 else :
557513 # Can't acquire locks and can't be deferred - cancel task
558514 # No locks were acquired, so just set state
0 commit comments