@@ -1739,15 +1739,30 @@ def start_queued_workflows(
17391739 available_tasks = max (0 , queue .concurrency - total_running_tasks )
17401740 max_tasks = min (max_tasks , available_tasks )
17411741
1742- # Lookup unstarted/uncompleted tasks (not running)
1742+ # Retrieve the first max_tasks workflows in the queue.
1743+ # Only retrieve workflows of the appropriate version (or without version set)
17431744 query = (
17441745 sa .select (
17451746 SystemSchema .workflow_queue .c .workflow_uuid ,
17461747 )
1748+ .select_from (
1749+ SystemSchema .workflow_queue .join (
1750+ SystemSchema .workflow_status ,
1751+ SystemSchema .workflow_queue .c .workflow_uuid
1752+ == SystemSchema .workflow_status .c .workflow_uuid ,
1753+ )
1754+ )
17471755 .where (SystemSchema .workflow_queue .c .queue_name == queue .name )
17481756 .where (SystemSchema .workflow_queue .c .started_at_epoch_ms == None )
17491757 .where (SystemSchema .workflow_queue .c .completed_at_epoch_ms == None )
17501758 .order_by (SystemSchema .workflow_queue .c .created_at_epoch_ms .asc ())
1759+ .where (
1760+ sa .or_ (
1761+ SystemSchema .workflow_status .c .application_version
1762+ == app_version ,
1763+ SystemSchema .workflow_status .c .application_version .is_ (None ),
1764+ )
1765+ )
17511766 .with_for_update (nowait = True ) # Error out early
17521767 )
17531768 # Apply limit only if max_tasks is finite
@@ -1779,15 +1794,6 @@ def start_queued_workflows(
17791794 SystemSchema .workflow_status .c .status
17801795 == WorkflowStatusString .ENQUEUED .value
17811796 )
1782- .where (
1783- sa .or_ (
1784- SystemSchema .workflow_status .c .application_version
1785- == app_version ,
1786- SystemSchema .workflow_status .c .application_version .is_ (
1787- None
1788- ),
1789- )
1790- )
17911797 .values (
17921798 status = WorkflowStatusString .PENDING .value ,
17931799 application_version = app_version ,
0 commit comments