From f09bf620b22a19952f1525553764383a3f03549c Mon Sep 17 00:00:00 2001 From: jobevers Date: Mon, 20 Feb 2017 12:59:03 -0600 Subject: [PATCH] allow render process to trigger a shutdown --- app/main.js | 49 ++++++++++++++++++++++++++++++++----------------- lbry | 2 +- lbry-web-ui | 2 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/app/main.js b/app/main.js index 0e5741b00..070c6d3e3 100644 --- a/app/main.js +++ b/app/main.js @@ -1,4 +1,4 @@ -const {app, BrowserWindow} = require('electron'); +const {app, BrowserWindow, ipcMain} = require('electron'); var path = require('path'); @@ -18,6 +18,7 @@ let subpy // set to true when the quitting sequence has started let quitting = false; + function createWindow () { // Create the browser window. //win = new BrowserWindow({x: 0, y: 0, width: 1440, height: 414, backgroundColor: '#155b4a'}) @@ -44,7 +45,7 @@ function lauchDaemon() { } console.log(`${__dirname}`); executable = path.join(__dirname, 'dist', 'lbrynet-daemon'); - subpy = require('child_process').spawn(executable)//, {stdio: ['ignore', process.stdout, process.stderr]}); + subpy = require('child_process').spawn(executable) // Need to handle the data event instead of attaching to // process.stdout because the latter doesn't work. I believe on // windows it buffers stdout and we don't get any meaningful output @@ -56,6 +57,7 @@ function lauchDaemon() { if (quitting) { app.quit(); } else { + // TODO: maybe it would be better to restart the daemon? win.loadURL(`file://${__dirname}/dist/warning.html`); setTimeout(app.quit, 5000) } @@ -71,9 +73,8 @@ app.on('ready', function(){ // an error its because its not running console.log('Checking for lbrynet daemon') client.request( - 'status', [], - function (err, res) { - // Did it all work ? + 'status', [], + function (err, res) { if (err) { console.log('lbrynet daemon needs to be launched') lauchDaemon(); @@ -96,19 +97,14 @@ app.on('window-all-closed', () => { }) app.on('before-quit', (event) => { - if (subpy != null) { - event.preventDefault() - if (win) { - win.loadURL(`file://${__dirname}/dist/quit.html`); - } - quitting = true; - console.log('Killing lbrynet-daemon process'); - kill(subpy.pid, undefined, (err) => { - console.log('Killed lbrynet-daemon process'); - }); + if (subpy == null) { + return } + event.preventDefault(); + shutdownDaemon(); }) + app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. @@ -117,5 +113,24 @@ app.on('activate', () => { } }) -// In this file you can include the rest of your app's specific main process -// code. You can also put them in separate files and require them here. + +function shutdownDaemon() { + console.log('Shutdown triggered'); + if (subpy == null) { + // TODO: In this case, we didn't start the process so I'm hesitant + // to shut it down. We might want to send a stop command + // though instead of just letting it run. + console.log('Not killing lbrynet because we did not start it') + return + } + if (win) { + win.loadURL(`file://${__dirname}/dist/quit.html`); + } + quitting = true; + console.log('Killing lbrynet-daemon process'); + kill(subpy.pid, undefined, (err) => { + console.log('Killed lbrynet-daemon process'); + }); +} + +ipcMain.on('shutdown', shutdownDaemon); diff --git a/lbry b/lbry index 14b4c0696..a1650d7f0 160000 --- a/lbry +++ b/lbry @@ -1 +1 @@ -Subproject commit 14b4c069696bede86bfd473f6e634000ad397661 +Subproject commit a1650d7f036eec01364214985d471f9322e259cd diff --git a/lbry-web-ui b/lbry-web-ui index 5b454a383..86351d173 160000 --- a/lbry-web-ui +++ b/lbry-web-ui @@ -1 +1 @@ -Subproject commit 5b454a383798f19aa11b661d770e183dc6b76336 +Subproject commit 86351d17362148168507958d33aad6111fd634aa