@@ -16,7 +16,6 @@ export class MemoryWorkspaceDispatchStorage implements WorkspaceDispatchStorageA
1616 private workspaceOrder : string [ ] = [ ]
1717 private laneQueues = new Map < string , string [ ] > ( )
1818 private leases = new Map < string , Map < string , number > > ( )
19- private sequence = 0
2019 private cleanupInterval : NodeJS . Timeout | null = null
2120
2221 constructor ( ) {
@@ -296,7 +295,20 @@ export class MemoryWorkspaceDispatchStorage implements WorkspaceDispatchStorageA
296295 }
297296
298297 async popNextWorkspaceId ( ) : Promise < string | null > {
299- return this . workspaceOrder . shift ( ) ?? null
298+ const now = Date . now ( )
299+ const maxScans = this . workspaceOrder . length
300+ for ( let i = 0 ; i < maxScans ; i ++ ) {
301+ const id = this . workspaceOrder . shift ( )
302+ if ( ! id ) return null
303+ const readyAt = this . workspaceReadyAt . get ( id )
304+ if ( readyAt && readyAt > now ) {
305+ this . workspaceOrder . push ( id )
306+ continue
307+ }
308+ this . workspaceReadyAt . delete ( id )
309+ return id
310+ }
311+ return null
300312 }
301313
302314 async getQueuedWorkspaceCount ( ) : Promise < number > {
@@ -307,7 +319,12 @@ export class MemoryWorkspaceDispatchStorage implements WorkspaceDispatchStorageA
307319 return this . workspaceOrder . includes ( workspaceId )
308320 }
309321
310- async ensureWorkspaceActive ( workspaceId : string ) : Promise < void > {
322+ private workspaceReadyAt = new Map < string , number > ( )
323+
324+ async ensureWorkspaceActive ( workspaceId : string , readyAt ?: number ) : Promise < void > {
325+ if ( readyAt && readyAt > Date . now ( ) ) {
326+ this . workspaceReadyAt . set ( workspaceId , readyAt )
327+ }
311328 this . ensureWorkspaceQueued ( workspaceId )
312329 }
313330
@@ -473,6 +490,7 @@ export class MemoryWorkspaceDispatchStorage implements WorkspaceDispatchStorageA
473490 this . workspaceOrder = [ ]
474491 this . laneQueues . clear ( )
475492 this . leases . clear ( )
493+ this . workspaceReadyAt . clear ( )
476494 }
477495
478496 dispose ( ) : void {
0 commit comments