2017-03-22 11:47:44 +01:00
|
|
|
const {app, BrowserWindow, ipcMain} = require('electron');
|
2017-02-23 19:46:25 +01:00
|
|
|
const path = require('path');
|
|
|
|
const jayson = require('jayson');
|
2017-02-10 20:11:26 +01:00
|
|
|
// tree-kill has better cross-platform handling of
|
|
|
|
// killing a process. child-process.kill was unreliable
|
2017-02-23 19:46:25 +01:00
|
|
|
const kill = require('tree-kill');
|
2017-03-22 11:47:44 +01:00
|
|
|
const child_process = require('child_process');
|
2017-01-16 20:06:53 +01:00
|
|
|
|
|
|
|
|
2017-02-23 19:46:25 +01:00
|
|
|
let client = jayson.client.http('http://localhost:5279/lbryapi');
|
2017-01-16 20:06:53 +01:00
|
|
|
// Keep a global reference of the window object, if you don't, the window will
|
|
|
|
// be closed automatically when the JavaScript object is garbage collected.
|
|
|
|
let win
|
|
|
|
// Also keep the daemon subprocess alive
|
|
|
|
let subpy
|
2017-01-25 07:24:04 +01:00
|
|
|
// set to true when the quitting sequence has started
|
|
|
|
let quitting = false;
|
2017-01-16 20:06:53 +01:00
|
|
|
|
2017-02-20 19:59:03 +01:00
|
|
|
|
2017-01-16 20:06:53 +01:00
|
|
|
function createWindow () {
|
2017-01-18 17:32:01 +01:00
|
|
|
win = new BrowserWindow({backgroundColor: '#155b4a'})
|
2017-01-16 20:06:53 +01:00
|
|
|
win.maximize()
|
2017-01-18 17:32:01 +01:00
|
|
|
//win.webContents.openDevTools()
|
2017-01-16 20:06:53 +01:00
|
|
|
win.loadURL(`file://${__dirname}/dist/index.html`)
|
|
|
|
win.on('closed', () => {
|
|
|
|
win = null
|
|
|
|
})
|
2017-01-18 17:32:01 +01:00
|
|
|
};
|
|
|
|
|
2017-02-23 20:01:23 +01:00
|
|
|
|
2017-03-17 10:06:02 +01:00
|
|
|
function launchDaemon() {
|
2017-01-25 07:24:04 +01:00
|
|
|
if (subpy) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-23 21:15:09 +01:00
|
|
|
if (process.env.LBRY_DAEMON) {
|
|
|
|
executable = process.env.LBRY_DAEMON;
|
|
|
|
} else {
|
|
|
|
executable = path.join(__dirname, 'dist', 'lbrynet-daemon');
|
|
|
|
}
|
|
|
|
console.log('Launching daemon: ' + executable)
|
2017-03-22 11:47:44 +01:00
|
|
|
subpy = child_process.spawn(executable)
|
2017-02-10 20:11:26 +01:00
|
|
|
// 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
|
|
|
|
subpy.stdout.on('data', (buf) => {console.log(String(buf).trim());});
|
|
|
|
subpy.stderr.on('data', (buf) => {console.log(String(buf).trim());});
|
2017-01-18 17:32:01 +01:00
|
|
|
subpy.on('exit', () => {
|
|
|
|
console.log('The daemon has exited. Quitting the app');
|
|
|
|
subpy = null;
|
2017-01-25 07:24:04 +01:00
|
|
|
if (quitting) {
|
2017-02-23 19:46:25 +01:00
|
|
|
// If quitting is True it means that we were expecting the daemon
|
|
|
|
// to be shutdown so we can quit right away
|
2017-01-25 07:24:04 +01:00
|
|
|
app.quit();
|
|
|
|
} else {
|
2017-02-23 19:46:25 +01:00
|
|
|
// Otherwise, this shutdown was a surprise so display a warning
|
|
|
|
// and schedule a quit
|
|
|
|
//
|
2017-02-20 19:59:03 +01:00
|
|
|
// TODO: maybe it would be better to restart the daemon?
|
2017-01-25 07:24:04 +01:00
|
|
|
win.loadURL(`file://${__dirname}/dist/warning.html`);
|
2017-01-26 19:30:02 +01:00
|
|
|
setTimeout(app.quit, 5000)
|
2017-01-25 07:24:04 +01:00
|
|
|
}
|
2017-01-18 17:32:01 +01:00
|
|
|
});
|
|
|
|
console.log('lbrynet daemon has launched')
|
2017-01-16 20:06:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 19:46:25 +01:00
|
|
|
|
2017-01-16 20:06:53 +01:00
|
|
|
app.on('ready', function(){
|
2017-02-23 20:01:23 +01:00
|
|
|
launchDaemonIfNotRunning();
|
|
|
|
createWindow();
|
2017-02-23 19:46:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function launchDaemonIfNotRunning() {
|
2017-02-23 20:01:23 +01:00
|
|
|
// Check if the daemon is already running. If we get
|
|
|
|
// an error its because its not running
|
|
|
|
console.log('Checking for lbrynet daemon');
|
|
|
|
client.request(
|
|
|
|
'status', [],
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
console.log('lbrynet daemon needs to be launched')
|
2017-03-17 10:06:02 +01:00
|
|
|
launchDaemon();
|
2017-02-23 20:01:23 +01:00
|
|
|
} else {
|
|
|
|
console.log('lbrynet daemon is already running')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-02-23 19:46:25 +01:00
|
|
|
}
|
2017-01-16 20:06:53 +01:00
|
|
|
|
2017-03-22 11:47:44 +01:00
|
|
|
/*
|
|
|
|
* Last resort for killing unresponsive daemon instances.
|
|
|
|
* Looks for any processes called "lbrynet-daemon" and
|
|
|
|
* tries to force kill them.
|
|
|
|
*/
|
|
|
|
function forceKillAllDaemons() {
|
|
|
|
console.log("Attempting to force kill any running lbrynet-daemon instances...");
|
|
|
|
|
|
|
|
const fgrepOut = child_process.spawnSync('pgrep', ['-x', 'lbrynet-daemon'], {encoding: 'utf8'}).stdout;
|
|
|
|
const daemonPids = fgrepOut.split(/[^\d]+/).filter((pid) => pid);
|
|
|
|
if (!daemonPids) {
|
|
|
|
console.log('No lbrynet-daemon found running.');
|
|
|
|
} else {
|
|
|
|
console.log(`Found ${daemonPids.length} running daemon instances. Attempting to force kill...`);
|
|
|
|
|
|
|
|
for (const pid of daemonPids) {
|
|
|
|
kill(pid, 'SIGKILL', (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(`Failed to force kill running daemon with pid ${pid}. Error message: ${err.message}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:06:53 +01:00
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
// On macOS it is common for applications and their menu bar
|
|
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-02-23 19:46:25 +01:00
|
|
|
|
2017-01-18 17:32:01 +01:00
|
|
|
app.on('before-quit', (event) => {
|
2017-02-20 19:59:03 +01:00
|
|
|
if (subpy == null) {
|
|
|
|
return
|
2017-01-18 17:32:01 +01:00
|
|
|
}
|
2017-02-20 19:59:03 +01:00
|
|
|
event.preventDefault();
|
|
|
|
shutdownDaemon();
|
2017-01-18 17:32:01 +01:00
|
|
|
})
|
|
|
|
|
2017-02-20 19:59:03 +01:00
|
|
|
|
2017-01-16 20:06:53 +01:00
|
|
|
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.
|
|
|
|
if (win === null) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-02-20 19:59:03 +01:00
|
|
|
|
2017-03-17 23:05:25 +01:00
|
|
|
function shutdownDaemon(evenIfNotStartedByApp = false) {
|
2017-03-17 10:06:02 +01:00
|
|
|
if (subpy) {
|
|
|
|
console.log('Killing lbrynet-daemon process');
|
|
|
|
kill(subpy.pid, undefined, (err) => {
|
|
|
|
console.log('Killed lbrynet-daemon process');
|
|
|
|
});
|
2017-03-17 23:05:25 +01:00
|
|
|
} else if (evenIfNotStartedByApp) {
|
2017-03-22 11:47:44 +01:00
|
|
|
console.log('Stopping lbrynet-daemon, even though app did not start it');
|
|
|
|
client.request('daemon_stop', [], (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
// We could get an error because the daemon is already stopped (good)
|
|
|
|
// or because it's running but not responding properly (bad).
|
|
|
|
// So try to force kill any daemons that are still running.
|
|
|
|
|
|
|
|
console.log('received error when stopping lbrynet-daemon. Error message: {err.message}');
|
|
|
|
forceKillAllDaemons();
|
|
|
|
}
|
|
|
|
});
|
2017-03-17 23:05:25 +01:00
|
|
|
} else {
|
|
|
|
console.log('Not killing lbrynet-daemon because app did not start it')
|
2017-03-17 10:06:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is it safe to start the installer before the daemon finishes running?
|
|
|
|
// If not, we should wait until the daemon is closed before we start the install.
|
|
|
|
}
|
|
|
|
|
|
|
|
function shutdown() {
|
2017-02-20 19:59:03 +01:00
|
|
|
if (win) {
|
|
|
|
win.loadURL(`file://${__dirname}/dist/quit.html`);
|
|
|
|
}
|
|
|
|
quitting = true;
|
2017-03-17 10:06:02 +01:00
|
|
|
shutdownDaemon();
|
|
|
|
}
|
|
|
|
|
|
|
|
function upgrade(event, installerPath) {
|
|
|
|
app.on('quit', () => {
|
2017-03-22 11:47:44 +01:00
|
|
|
// shell.openItem doesn't reliably work from the app process, so run xdg-open directly
|
|
|
|
child_process.spawn('xdg-open', [installerPath], {
|
|
|
|
stdio: 'ignore',
|
|
|
|
});
|
2017-02-20 19:59:03 +01:00
|
|
|
});
|
2017-03-17 10:06:02 +01:00
|
|
|
if (win) {
|
|
|
|
win.loadURL(`file://${__dirname}/dist/upgrade.html`);
|
|
|
|
}
|
|
|
|
quitting = true;
|
2017-03-17 23:05:25 +01:00
|
|
|
shutdownDaemon(true);
|
|
|
|
// wait for daemon to shut down before upgrading
|
|
|
|
// what to do if no shutdown in a long time?
|
|
|
|
console.log('Update downloaded to ', installerPath);
|
|
|
|
console.log('The app will close, and you will be prompted to install the latest version of LBRY.');
|
|
|
|
console.log('After the install is complete, please reopen the app.');
|
2017-02-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
|
2017-03-17 10:06:02 +01:00
|
|
|
ipcMain.on('upgrade', upgrade);
|
|
|
|
|
2017-03-22 11:47:44 +01:00
|
|
|
ipcMain.on('shutdown', shutdown);
|