Skip to content

Commit 5627ef6

Browse files
committed
fix: rework how Workforce announces expectationManagers to workers
This is to make it clearer when it is done, and not have `registerExpectationManager` wait on all announcements to have been made before returning.
1 parent b816eb5 commit 5627ef6

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

shared/packages/workforce/src/workforce.ts

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,22 +331,21 @@ export class Workforce {
331331
}
332332

333333
public async registerExpectationManager(managerId: ExpectationManagerId, urls: URLMap): Promise<void> {
334-
let em = this.expectationManagers.get(managerId)
335-
if (!em || deepEqual(em.urls, urls)) {
336-
// Added/Changed
334+
this.logger.info(`Register ExpectationManager (${managerId}) at URLs: ${JSON.stringify(urls)}`)
337335

338-
this.logger.info(`Workforce: Register ExpectationManager (${managerId}) at URLs: ${JSON.stringify(urls)}`)
339-
340-
// Announce the new expectation manager to the workerAgents:
341-
for (const workerAgent of this.workerAgents.values()) {
342-
await workerAgent.api.expectationManagerAvailable(managerId, urls)
343-
}
344-
em = this.expectationManagers.get(managerId)
345-
}
336+
const em = this.expectationManagers.get(managerId)
337+
let hasChanged = false
346338
if (!em) {
347-
throw new Error(`Internal Error: (registerExpectationManager) ExpectationManager "${managerId}" not found!`)
339+
hasChanged = true
340+
} else if (!deepEqual(em.urls, urls)) {
341+
em.urls = urls
342+
hasChanged = true
343+
}
344+
345+
if (hasChanged) {
346+
// Announce the new/changed expectation manager to the workerAgents:
347+
this.triggerAnnounceExpectationManagersToWorkers(managerId)
348348
}
349-
em.urls = urls
350349
}
351350
public async requestResourcesForExpectation(exp: Expectation.Any): Promise<boolean> {
352351
return this.workerHandler.requestResourcesForExpectation(exp)
@@ -451,4 +450,42 @@ export class Workforce {
451450
this.logger.error(`Workforce: Error in getRunningApps: ${stringifyError(error)}`)
452451
})
453452
}
453+
454+
private expectationManagersToAnnounceTimeout: NodeJS.Timeout | null = null
455+
private triggerAnnounceExpectationManagersToWorkers(managerId: ExpectationManagerId): void {
456+
this.expectationManagersToAnnounce.add(managerId)
457+
458+
if (this.expectationManagersToAnnounceTimeout) return // already scheduled to trigger
459+
460+
// Trigger an announce soon:
461+
this.expectationManagersToAnnounceTimeout = setTimeout(() => {
462+
this.expectationManagersToAnnounceTimeout = null
463+
464+
Promise.resolve()
465+
.then(async () => {
466+
await this._announceExpectationManagersToWorkers()
467+
})
468+
.catch((err) => {
469+
this.logger.error(`Error in _triggerAnnounceExpectationManagersToWorkers: ${stringifyError(err)}`)
470+
})
471+
}, 1000)
472+
}
473+
474+
private expectationManagersToAnnounce = new Set<ExpectationManagerId>()
475+
private async _announceExpectationManagersToWorkers(): Promise<void> {
476+
const managerIds = Array.from(this.expectationManagersToAnnounce.values())
477+
this.expectationManagersToAnnounce.clear()
478+
479+
for (const managerId of managerIds) {
480+
const em = this.expectationManagers.get(managerId)
481+
if (em?.urls) {
482+
const urls = em.urls
483+
await Promise.all(
484+
Array.from(this.workerAgents.values()).map(async (workerAgent) =>
485+
workerAgent.api.expectationManagerAvailable(managerId, urls)
486+
)
487+
)
488+
}
489+
}
490+
}
454491
}

0 commit comments

Comments
 (0)