Merge pull request #878 from shocknet/restart-sub-process

restart sub p (reopened for review)
This commit is contained in:
Justin (shocknet) 2026-02-17 09:46:01 -05:00 committed by GitHub
commit f4b302ed43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,28 +11,69 @@ export default class NostrSubprocess {
utils: Utils utils: Utils
awaitingPongs: (() => void)[] = [] awaitingPongs: (() => void)[] = []
log = getLogger({}) log = getLogger({})
latestRestart = 0
private settings: NostrSettings
private eventCallback: EventCallback
private beaconCallback: BeaconCallback
private isShuttingDown = false
constructor(settings: NostrSettings, utils: Utils, eventCallback: EventCallback, beaconCallback: BeaconCallback) { constructor(settings: NostrSettings, utils: Utils, eventCallback: EventCallback, beaconCallback: BeaconCallback) {
this.utils = utils this.utils = utils
this.settings = settings
this.eventCallback = eventCallback
this.beaconCallback = beaconCallback
this.startSubProcess()
}
private cleanupProcess() {
if (this.childProcess) {
this.childProcess.removeAllListeners()
if (!this.childProcess.killed) {
this.childProcess.kill('SIGTERM')
}
}
}
private startSubProcess() {
this.cleanupProcess()
this.childProcess = fork("./build/src/services/nostr/handler") this.childProcess = fork("./build/src/services/nostr/handler")
this.childProcess.on("error", (error) => { this.childProcess.on("error", (error) => {
this.log(ERROR, "nostr subprocess error", error) this.log(ERROR, "nostr subprocess error", error)
}) })
this.childProcess.on("exit", (code, signal) => { this.childProcess.on("exit", (code, signal) => {
this.log(ERROR, `nostr subprocess exited with code ${code} and signal ${signal}`) if (this.isShuttingDown) {
if (code === 0) { this.log("nostr subprocess stopped")
return return
} }
throw new Error(`nostr subprocess exited with code ${code} and signal ${signal}`)
if (code === 0) {
this.log("nostr subprocess exited cleanly")
return
}
this.log(ERROR, `nostr subprocess exited with code ${code} and signal ${signal}`)
const now = Date.now()
if (now - this.latestRestart < 5000) {
this.log(ERROR, "nostr subprocess exited too quickly, not restarting")
throw new Error("nostr subprocess crashed repeatedly")
}
this.log("restarting nostr subprocess...")
this.latestRestart = now
setTimeout(() => this.startSubProcess(), 100)
}) })
this.childProcess.on("message", (message: ChildProcessResponse) => { this.childProcess.on("message", (message: ChildProcessResponse) => {
switch (message.type) { switch (message.type) {
case 'ready': case 'ready':
this.sendToChildProcess({ type: 'settings', settings: settings }) this.sendToChildProcess({ type: 'settings', settings: this.settings })
break; break;
case 'event': case 'event':
eventCallback(message.event) this.eventCallback(message.event)
break break
case 'processMetrics': case 'processMetrics':
this.utils.tlvStorageFactory.ProcessMetrics(message.metrics, 'nostr') this.utils.tlvStorageFactory.ProcessMetrics(message.metrics, 'nostr')
@ -42,7 +83,7 @@ export default class NostrSubprocess {
this.awaitingPongs = [] this.awaitingPongs = []
break break
case 'beacon': case 'beacon':
beaconCallback({ content: message.content, pub: message.pub }) this.beaconCallback({ content: message.content, pub: message.pub })
break break
default: default:
console.error("unknown nostr event response", message) console.error("unknown nostr event response", message)
@ -50,11 +91,15 @@ export default class NostrSubprocess {
} }
}) })
} }
sendToChildProcess(message: ChildProcessRequest) { sendToChildProcess(message: ChildProcessRequest) {
this.childProcess.send(message) if (this.childProcess && !this.childProcess.killed) {
this.childProcess.send(message)
}
} }
Reset(settings: NostrSettings) { Reset(settings: NostrSettings) {
this.settings = settings
this.sendToChildProcess({ type: 'settings', settings }) this.sendToChildProcess({ type: 'settings', settings })
} }
@ -68,7 +113,9 @@ export default class NostrSubprocess {
Send(initiator: SendInitiator, data: SendData, relays?: string[]) { Send(initiator: SendInitiator, data: SendData, relays?: string[]) {
this.sendToChildProcess({ type: 'send', data, initiator, relays }) this.sendToChildProcess({ type: 'send', data, initiator, relays })
} }
Stop() { Stop() {
this.childProcess.kill(0) this.isShuttingDown = true
this.cleanupProcess()
} }
} }