Skip to content
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

app worker teardown iteration #397

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 11 additions & 14 deletions gui/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const IPC = require('pear-ipc')
const ReadyResource = require('ready-resource')
const Worker = require('../lib/worker')
const constants = require('../constants')

const kMap = Symbol('pear.gui.map')
const kCtrl = Symbol('pear.gui.ctrl')

Expand Down Expand Up @@ -380,7 +379,9 @@ class App {
appReady = false
static root = unixPathResolve(resolve(__dirname, '..'))

constructor (state, ipc) {
constructor (gui) {
const { state, ipc } = gui
this.gui = gui
this.state = state
this.ipc = ipc
this.contextMenu = null
Expand Down Expand Up @@ -647,16 +648,18 @@ class App {
resolve(false)
}
})

const unloaders = PearGUI.ctrls().map((ctrl) => {
const pipes = [...this.gui.pipes]
const closingPipes = pipes.map((pipe) => new Promise((resolve) => { pipe.once('close', resolve) }))
const unloaders = [closingPipes, ...PearGUI.ctrls().map((ctrl) => {
const closed = () => ctrl.closed
if (!ctrl.unload) {
if (ctrl.unloader) return ctrl.unloader.then(closed, closed)
return ctrl.close()
}
ctrl.unload({ type: 'close' })
return ctrl.unloader.then(closed, closed)
})
})]
for (const pipe of pipes) pipe.end()
const unloading = Promise.all(unloaders)
unloading.then(clear, clear)
const result = await Promise.race([timeout, unloading])
Expand Down Expand Up @@ -1498,8 +1501,8 @@ class PearGUI extends ReadyResource {
electron.ipcMain.handle('versions', (evt, ...args) => this.versions(...args))
electron.ipcMain.handle('restart', (evt, ...args) => this.restart(...args))

electron.ipcMain.on('workerRun', (evt, link) => {
const pipe = this.worker.run(link)
electron.ipcMain.on('workerRun', (evt, link, args) => {
const pipe = this.worker.run(link, args)
const id = this.pipes.alloc(pipe)
pipe.on('close', () => {
this.pipes.free(id)
Expand Down Expand Up @@ -1529,16 +1532,10 @@ class PearGUI extends ReadyResource {
}
pipe.write(data)
})

electron.app.once('will-quit', () => {
for (const pipe of this.pipes) {
pipe.sp.kill('SIGTERM')
}
})
}

async app () {
const app = new App(this.state, this.ipc)
const app = new App(this)
this.once('close', async () => { app.quit() })
await app.start()
return app
Expand Down
7 changes: 5 additions & 2 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Worker {
}

run (link, args = []) {
if (isElectronRenderer) return this.#ipc.workerRun(link)
if (isElectronRenderer) return this.#ipc.workerRun(link, args)
args = [...this.#args(link), ...args]
const sp = spawn(constants.RUNTIME, args, {
stdio: ['inherit', 'inherit', 'inherit', 'overlapped'],
Expand All @@ -51,7 +51,9 @@ class Worker {
this.#unref()
})
const pipe = sp.stdio[3]
pipe.sp = sp
pipe.on('end', () => {
if (pipe.ended === false) pipe.end()
})
return pipe
}

Expand All @@ -65,6 +67,7 @@ class Worker {
return null
}
const pipe = new Pipe(fd)
pipe.on('end', () => pipe.end())
this.#pipe = pipe
pipe.once('close', () => {
// allow close event to propagate between processes before exiting:
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/worker/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const pipe = Pear.worker.pipe()

let i = 0

let interval = null
pipe.on('data', (data) => {
const str = data.toString()
if (str === 'ping') {
setInterval(() => pipe.write((i++).toString()), 2000)
interval = setInterval(() => pipe.write((i++).toString()), 2000)
}
if (str === 'exit') {
clearInterval(interval)
Pear.exit()
}
})