Skip to content

Commit b7467cc

Browse files
committed
fix: when main error then child will not die
1 parent f94009a commit b7467cc

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

packages/network-debugger/src/core/fork.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class MainProcess {
6767
})
6868
this.ws
6969
.then((ws) => {
70+
this.healthCheck()
7071
ws.on('error', (e) => {
7172
console.error('MainProcess Socket Error: ', e)
7273
})
@@ -136,6 +137,20 @@ export class MainProcess {
136137
return this
137138
}
138139

140+
private async healthCheck() {
141+
const ws = await this.ws
142+
const ping = () => {
143+
ws.send(
144+
JSON.stringify({
145+
type: 'health',
146+
data: {}
147+
})
148+
)
149+
}
150+
ping()
151+
setInterval(ping, 2000)
152+
}
153+
139154
public responseRequest(id: string, response: IncomingMessage) {
140155
const responseBuffer: Buffer[] = []
141156

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createPlugin, useHandler } from '../common'
2+
3+
export const healthPlugin = createPlugin('health', () => {
4+
const exitProcess = () => {
5+
process.exit(0)
6+
}
7+
8+
let id = setTimeout(exitProcess, 5000)
9+
useHandler('healthcheck', () => {
10+
clearTimeout(id)
11+
id = setTimeout(exitProcess, 5000)
12+
})
13+
})

packages/network-debugger/src/fork/module/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { RequestCenter } from '../request-center'
22
import { debuggerPlugin } from './debugger'
33
import { networkPlugin } from './network'
44
import { websocketPlugin } from './websocket'
5+
import { healthPlugin } from './health'
56

67
export const loadPlugin = (instance: RequestCenter) => {
7-
instance.loadPlugins([networkPlugin, debuggerPlugin, websocketPlugin])
8+
instance.loadPlugins([networkPlugin, debuggerPlugin, websocketPlugin, healthPlugin])
89
}

packages/network-debugger/src/fork/request-center.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class RequestCenter {
2727
private options: RequestCenterInitOptions
2828
constructor(options: RequestCenterInitOptions) {
2929
this.options = options
30-
const { serverPort, requests, autoOpenDevtool } = options
30+
const { serverPort, autoOpenDevtool } = options
3131
this.devtool = new DevtoolServer({
3232
port: serverPort,
3333
autoOpenDevtool: autoOpenDevtool,
@@ -113,7 +113,7 @@ export class RequestCenter {
113113
{
114114
const listenerList = this.listeners[_message.type]
115115
if (!listenerList) {
116-
console.warn('unknown message type', _message.type)
116+
// console.warn('unknown message type', _message.type)
117117
break
118118
}
119119

0 commit comments

Comments
 (0)