@@ -529,6 +529,8 @@ const setupRun = (
529529 const readyQueue = createAsyncQueue < string > ( )
530530 const pendingByPrior = new Map < string , Set < string > > ( )
531531 const activeTaskIds = new Set < string > ( )
532+ const routingTasks = new Set < Promise < void > > ( )
533+ const reconciliationTasks = new Set < Promise < void > > ( )
532534 const taskTracker = createTaskTracker ( taskManager )
533535 let routingTaskCount = 0
534536 let didEmitAllProcessed = false
@@ -570,7 +572,7 @@ const setupRun = (
570572
571573 didEmitAllProcessed = false
572574 routingTaskCount += 1
573- void ( async ( ) => {
575+ const routingTask = ( async ( ) => {
574576 let wasRouted = false
575577 try {
576578 const task = await taskManager . getTask ( id )
@@ -588,6 +590,11 @@ const setupRun = (
588590 if ( ! wasRouted ) emitAllProcessedIfIdle ( )
589591 }
590592 } ) ( )
593+ routingTasks . add ( routingTask )
594+ void routingTask . then (
595+ ( ) => routingTasks . delete ( routingTask ) ,
596+ ( ) => routingTasks . delete ( routingTask ) ,
597+ )
591598 }
592599 const { taskisInLoop, taskOutOfLoop, getTasksInProgress, clearTasksInProgress } =
593600 workerLoggingHelper ( streamEmit )
@@ -667,7 +674,12 @@ const setupRun = (
667674 if ( getTasksInProgress ( ) <= 0 && readyQueue . count ( ) === 0 && pendingByPrior . size > 0 ) {
668675 streamEmit ( { stage : 'waiting' } )
669676 }
670- void reconcilePendingTasks ( ) . finally ( ( ) => emitAllProcessedIfIdle ( ) )
677+ const reconciliationTask = reconcilePendingTasks ( ) . finally ( ( ) => emitAllProcessedIfIdle ( ) )
678+ reconciliationTasks . add ( reconciliationTask )
679+ void reconciliationTask . then (
680+ ( ) => reconciliationTasks . delete ( reconciliationTask ) ,
681+ ( ) => reconciliationTasks . delete ( reconciliationTask ) ,
682+ )
671683 } , 1000 )
672684
673685 try {
@@ -679,6 +691,7 @@ const setupRun = (
679691 } finally {
680692 clearInterval ( reconciliationInterval )
681693 }
694+ await Promise . allSettled ( [ ...routingTasks , ...reconciliationTasks ] )
682695 readyQueue . clear ( )
683696 pendingByPrior . clear ( )
684697 activeTaskIds . clear ( )
@@ -728,8 +741,9 @@ export function runTaskWorker(
728741 > ( )
729742 let currentTaskCtrl : AbortController | undefined = new AbortController ( )
730743 let queueTask : ( ( id : string ) => void ) | undefined = undefined
744+ const activeRuns = new Set < Promise < void > > ( )
731745
732- const workerStop = ( message : string ) => {
746+ const cancelCurrentRun = ( message : string ) => {
733747 currentTaskCtrl ?. abort ( message )
734748 // in case of any errors, especially if its an interrupt event we simply want to cancel everything :P
735749 // empty our task queue :)
@@ -739,7 +753,7 @@ export function runTaskWorker(
739753 }
740754
741755 // we have put all our dependencies in restartable workers.
742- // if anyone calls the "workerStop" the function wil simply re-start the worker
756+ // If the current run is cancelled, the worker starts a new run when another task is queued.
743757 // as soon as a new task was added....
744758 const externalQueueTask = ( id : string ) => {
745759 if ( currentTaskCtrl ?. signal . aborted || ! queueTask ) {
@@ -751,14 +765,25 @@ export function runTaskWorker(
751765 currentTaskCtrl = newTaskCtrl
752766 queueTask = newQueueTask
753767
754- void run ( defaultTask , errorTask )
768+ const workerRun = run ( defaultTask , errorTask )
769+ activeRuns . add ( workerRun )
770+ void workerRun . then (
771+ ( ) => activeRuns . delete ( workerRun ) ,
772+ ( error ) => {
773+ activeRuns . delete ( workerRun )
774+ taskProcessingStream . emit ( { stage : 'error' , info : humanizeError ( error ) } )
775+ } ,
776+ )
755777 }
756778 queueTask ( id )
757779 }
758780 return {
759781 workerStream : taskProcessingStream . stream ,
760782 toolRpcPort,
761- workerStop,
783+ cancelCurrentRun,
784+ workerSettled : async ( ) => {
785+ await Promise . allSettled ( activeRuns )
786+ } ,
762787 queueTask : externalQueueTask ,
763788 }
764789}
0 commit comments