Skip to content

updates to lint, tests and build #3681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update childBridgeService.ts
  • Loading branch information
bwp91 committed Aug 25, 2024
commit 08ad3600debd5a780355c9262f8eb9e9788950c3
63 changes: 42 additions & 21 deletions src/childBridgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export class ChildBridgeService {
private pluginConfig: Array<PlatformConfig | AccessoryConfig> = []
private log: Logging
private displayName?: string
private restartCount = 0
private readonly maxRestarts = 4

constructor(
public type: PluginType,
Expand Down Expand Up @@ -227,17 +229,12 @@ export class ChildBridgeService {
process.stderr.write(data)
})

this.child.on('exit', () => {
this.log.warn('Child bridge process ended')
})

this.child.on('error', (e) => {
this.bridgeStatus = ChildBridgeStatus.DOWN
this.log.error('Child process error', e)
this.log.error('Child bridge process error', e)
})

this.child.once('close', (code, signal) => {
this.bridgeStatus = ChildBridgeStatus.DOWN
this.handleProcessClose(code, signal)
})

Expand All @@ -249,16 +246,16 @@ export class ChildBridgeService {

switch (message.id) {
case ChildProcessMessageEventType.READY: {
this.log(`Launched child bridge with PID ${this.child?.pid}`)
this.log(`Child bridge starting${this.child?.pid ? ` (pid ${this.child.pid})` : ''}...`)
this.loadPlugin()
break
}
case ChildProcessMessageEventType.LOADED: {
const version = (message.data as ChildProcessPluginLoadedEventData).version
if (this.pluginConfig.length > 1) {
this.log(`Loaded ${this.plugin.getPluginIdentifier()} v${version} child bridge successfully with ${this.pluginConfig.length} accessories`)
this.log.success(`Child bridge started successfully with ${this.pluginConfig.length} accessories (plugin v${version}).`)
} else {
this.log(`Loaded ${this.plugin.getPluginIdentifier()} v${version} child bridge successfully`)
this.log.success(`Child bridge started successfully (plugin v${version}).`)
}
this.startBridge()
break
Expand Down Expand Up @@ -287,14 +284,36 @@ export class ChildBridgeService {
* @param signal
*/
private handleProcessClose(code: number | null, signal: string | null): void {
this.log(`Process Ended. Code: ${code}, Signal: ${signal}`)

setTimeout(() => {
if (!this.shuttingDown) {
this.log('Restarting Process...')
this.startChildProcess()
const isLikelyPluginCrash = code === 1 && signal === null
this.log.warn(`Child bridge ended (code ${code}, signal ${signal}).${isLikelyPluginCrash
? ' The child bridge ended unexpectedly, which is normally due to the plugin not catching its errors properly. Please report this to the plugin developer by clicking on the'
+ ' \'Report An Issue\' option in the plugin menu dropdown from the Homebridge UI. If there are related logs shown above, please include them in your report.'
: ''}`)

if (isLikelyPluginCrash) {
if (this.restartCount < this.maxRestarts) {
this.bridgeStatus = ChildBridgeStatus.PENDING
this.restartCount += 1
const delay = this.restartCount * 10 // first attempt after 10 seconds, second after 20 seconds, etc.
this.log(`Child bridge will automatically restart in ${delay} seconds (restart attempt ${this.restartCount} of ${this.maxRestarts}).`)
setTimeout(() => {
if (!this.shuttingDown) {
this.startChildProcess()
}
}, delay * 1000)
} else {
this.bridgeStatus = ChildBridgeStatus.DOWN
this.manuallyStopped = true
this.log.error(`Child bridge will no longer restart after failing ${this.maxRestarts + 1} times, you will need to manually start this child bridge from the Homebridge UI.`)
}
}, 7000)
return
}

if (!this.shuttingDown) {
this.bridgeStatus = ChildBridgeStatus.DOWN
this.restartCount = 0
this.startChildProcess()
}
}

/**
Expand Down Expand Up @@ -441,9 +460,10 @@ export class ChildBridgeService {
*/
public restartChildBridge(): void {
if (this.manuallyStopped) {
this.restartCount = 0
this.startChildBridge()
} else {
this.log.warn('Restarting child bridge...')
this.log.warn('Child bridge restarting...')
this.refreshConfig()
this.teardown()
}
Expand All @@ -454,13 +474,15 @@ export class ChildBridgeService {
*/
public stopChildBridge(): void {
if (!this.shuttingDown) {
this.log.warn('Stopping child bridge (will not restart)...')
this.log.warn('Child bridge stopping, will not restart.')
this.shuttingDown = true
this.manuallyStopped = true
this.restartCount = 0
this.bridgeStatus = ChildBridgeStatus.DOWN
this.child?.removeAllListeners('close')
this.teardown()
} else {
this.log.warn('Bridge already shutting down or stopped.')
this.log.warn('Child bridge already shutting down or stopped.')
}
}

Expand All @@ -469,13 +491,12 @@ export class ChildBridgeService {
*/
public startChildBridge(): void {
if (this.manuallyStopped && this.bridgeStatus === ChildBridgeStatus.DOWN && (!this.child || !this.child.connected)) {
this.log.warn('Starting child bridge...')
this.refreshConfig()
this.startChildProcess()
this.shuttingDown = false
this.manuallyStopped = false
} else {
this.log.warn('Cannot start child bridge, it is still running or was not manually stopped')
this.log.warn('Child bridge cannot be started, it is still running or was not manually stopped.')
}
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy