2018-01-18 03:13:08 +01:00
|
|
|
import path from 'path';
|
|
|
|
import { spawn, execSync } from 'child_process';
|
2019-02-18 18:33:02 +01:00
|
|
|
import { Lbry } from 'lbry-redux';
|
2018-01-18 03:13:08 +01:00
|
|
|
|
|
|
|
export default class Daemon {
|
2019-03-19 02:44:49 +01:00
|
|
|
static path =
|
|
|
|
process.env.LBRY_DAEMON ||
|
|
|
|
(process.env.NODE_ENV === 'production'
|
|
|
|
? path.join(process.resourcesPath, 'static/daemon', 'lbrynet')
|
|
|
|
: path.join(__static, 'daemon/lbrynet'));
|
2018-01-18 03:13:08 +01:00
|
|
|
subprocess;
|
|
|
|
handlers;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.handlers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
launch() {
|
2018-10-26 06:20:18 +02:00
|
|
|
this.subprocess = spawn(Daemon.path, ['start']);
|
2018-01-18 03:13:08 +01:00
|
|
|
|
|
|
|
this.subprocess.stdout.on('data', data => console.log(`Daemon: ${data}`));
|
|
|
|
this.subprocess.stderr.on('data', data => console.error(`Daemon: ${data}`));
|
|
|
|
this.subprocess.on('exit', () => this.fire('exit'));
|
2018-10-26 06:20:18 +02:00
|
|
|
this.subprocess.on('error', error => console.error(`Daemon error: ${error}`));
|
2018-01-18 03:13:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
quit() {
|
2019-02-18 18:33:02 +01:00
|
|
|
Lbry.stop()
|
|
|
|
.then()
|
|
|
|
.catch(() => {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
try {
|
|
|
|
execSync(`taskkill /pid ${this.subprocess.pid} /t /f`);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.subprocess.kill();
|
|
|
|
}
|
|
|
|
});
|
2018-01-18 03:13:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Follows the publish/subscribe pattern
|
|
|
|
|
|
|
|
// Subscribe method
|
|
|
|
on(event, handler, context = handler) {
|
|
|
|
this.handlers.push({ event, handler: handler.bind(context) });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish method
|
|
|
|
fire(event, args) {
|
|
|
|
this.handlers.forEach(topic => {
|
|
|
|
if (topic.event === event) topic.handler(args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|