Skip to content

Commit 6de0a44

Browse files
authored
Merge pull request #261 from Sofie-Automation/fix/connection-init-logging
Fix/connection init logging
2 parents 8bcaaa4 + 5627ef6 commit 6de0a44

6 files changed

Lines changed: 83 additions & 23 deletions

File tree

apps/package-manager/packages/generic/src/packageManager.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class PackageManagerHandler {
145145

146146
this.coreHandler.setPackageManagerHandler(this)
147147

148-
this.logger.info('PackageManagerHandler init')
148+
this.logger.info('PackageManagerHandler init...')
149149

150150
coreHandler.onConnected(() => {
151151
this.setupObservers()
@@ -157,10 +157,20 @@ export class PackageManagerHandler {
157157
this.onSettingsChanged()
158158
this.triggerUpdatedExpectedPackages()
159159

160-
await this.callbacksHandler.cleanReportedStatuses()
161-
await this.expectationManager.init()
160+
try {
161+
await this.callbacksHandler.cleanReportedStatuses()
162+
} catch (e) {
163+
this.logger.error(`Error during cleanReportedStatuses()`)
164+
throw e
165+
}
166+
try {
167+
await this.expectationManager.init()
168+
} catch (e) {
169+
this.logger.error(`Error during expectationManager.init()`)
170+
throw e
171+
}
162172

163-
this.logger.info('PackageManagerHandler initialized')
173+
this.logger.info('PackageManagerHandler initialized!')
164174
}
165175
onSettingsChanged(): void {
166176
this.settings = {

shared/packages/api/src/websocketServer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class WebsocketServer extends HelpfulEventEmitter {
2626

2727
this.wss = new WebSocket.Server({ port: port })
2828

29+
this.wss.on('listening', () => {
30+
this.logger.info(`Listening on port ${port}`)
31+
})
32+
2933
this.wss.on('close', () => {
3034
// The websocekt server is closed.
3135

shared/packages/expectationManager/src/internalManager/lib/expectationManagerServer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,22 @@ export class ExpectationManagerServer {
104104
'*': 'ws://127.0.0.1',
105105
}
106106
}
107-
this.logger.info(`##### type: ${this.workForceConnectionOptions.type}`)
107+
this.logger.debug(`##### type: ${this.workForceConnectionOptions.type}`)
108108
if (this.serverOptions.type === 'websocket' && this.serverOptions.port === 0) {
109-
this.logger.info(`##### this.serverOptions.port: ${this.serverOptions.port}`)
109+
this.logger.debug(`##### this.serverOptions.port: ${this.serverOptions.port}`)
110110
// When the configured port i 0, the next free port is picked
111111
for (const key of Object.keys(this._serverAccessUrls)) {
112112
// If no port was specified in the url, add it:
113113
if (!this._serverAccessUrls[key].match(/\/\/[^/]+:\d/)) {
114-
this.logger.info(`updateing ${key}`)
114+
this.logger.debug(`updateing ${key}`)
115115
this._serverAccessUrls[key] += `:${this.manager.expectationManagerServer.websocketServer?.port}`
116116
}
117117
}
118118
}
119119

120-
this.logger.info(`##### this.serverAccessBaseUrls: ${JSON.stringify(this.serverAccessBaseUrls)}`)
121-
this.logger.info(`##### websocketServer?.port: ${this.manager.expectationManagerServer.websocketServer?.port}`)
122-
this.logger.info(`##### this._serverAccessUrls: ${JSON.stringify(this._serverAccessUrls)}`)
120+
this.logger.debug(`##### this.serverAccessBaseUrls: ${JSON.stringify(this.serverAccessBaseUrls)}`)
121+
this.logger.debug(`##### websocketServer?.port: ${this.manager.expectationManagerServer.websocketServer?.port}`)
122+
this.logger.debug(`##### this._serverAccessUrls: ${JSON.stringify(this._serverAccessUrls)}`)
123123

124124
if (!this._serverAccessUrls) throw new Error(`ExpectationManager.serverAccessUrl not set!`)
125125
}

shared/packages/expectationManager/src/internalManager/lib/workforceConnection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export class WorkforceConnection {
5858
})
5959
}
6060
public async init(): Promise<void> {
61+
if (this.workForceConnectionOptions.type === 'websocket') {
62+
this.logger.info(`Connecting to Workforce at ${this.workForceConnectionOptions.url}`)
63+
}
6164
await this.workforceAPI.init(this.workForceConnectionOptions, {
6265
setLogLevel: async (logLevel: LogLevel): Promise<void> => {
6366
this.logger.setLogLevel(logLevel)

shared/packages/worker/src/workerAgent.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ export class WorkerAgent {
240240
const pAppContainer = new Promise<void>((resolve, reject) => {
241241
this.initAppContainerAPIPromise = { resolve, reject }
242242
})
243+
if (this.appContainerConnectionOptions.type === 'websocket') {
244+
this.logger.info(`Connecting to AppContainer at ${this.appContainerConnectionOptions.url}`)
245+
}
243246
await this.appContainerAPI.init(this.appContainerConnectionOptions, {
244247
setLogLevel: async (logLevel: LogLevel) => this.setLogLevel(logLevel),
245248
_debugKill: async () => this._debugKill(),
@@ -260,6 +263,9 @@ export class WorkerAgent {
260263
const pWorkForce = new Promise<void>((resolve, reject) => {
261264
this.initWorkForceAPIPromise = { resolve, reject }
262265
})
266+
if (this.workForceConnectionOptions.type === 'websocket') {
267+
this.logger.info(`Connecting to Workforce at ${this.workForceConnectionOptions.url}`)
268+
}
263269
await this.workforceAPI.init(this.workForceConnectionOptions, {
264270
setLogLevel: async (logLevel: LogLevel) => this.setLogLevel(logLevel),
265271
_debugKill: async () => this._debugKill(),

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)