config.cli: store PID of managed rTorrent process

This commit is contained in:
Jesse Chan
2020-12-01 00:41:36 +08:00
parent 83ee76e0b6
commit 0f4c4b6555
2 changed files with 30 additions and 18 deletions

View File

@@ -146,10 +146,28 @@ const {argv} = require('yargs')
hidden: true,
type: 'string',
})
.option('test', {
default: false,
describe: 'DEV ONLY: Test setup',
hidden: true,
type: 'boolean',
})
.version(require('./package.json').version)
.alias('v', 'version')
.help();
process.on('SIGINT', () => {
process.exit();
});
try {
fs.mkdirSync(path.join(argv.rundir, 'db'), {recursive: true});
fs.mkdirSync(path.join(argv.rundir, 'temp'), {recursive: true});
} catch (error) {
console.error('Failed to access runtime directory');
process.exit(1);
}
if (argv.rtorrent) {
const args = [];
let opts = 'system.daemon.set=true';
@@ -161,12 +179,16 @@ if (argv.rtorrent) {
const rTorrentProcess = spawn('rtorrent', args.concat(['-o', opts]), {stdio: 'inherit'});
rTorrentProcess.on('close', () => {
process.exit(1);
});
rTorrentProcess.on('error', () => {
process.exit(1);
});
fs.writeFileSync(path.join(argv.rundir, 'rtorrent.pid'), `${rTorrentProcess.pid}`);
if (!argv.test) {
rTorrentProcess.on('close', () => {
process.exit(1);
});
rTorrentProcess.on('error', () => {
process.exit(1);
});
}
process.on('exit', () => {
console.log('Killing rTorrent daemon...');
@@ -174,18 +196,6 @@ if (argv.rtorrent) {
});
}
process.on('SIGINT', () => {
process.exit();
});
try {
fs.mkdirSync(path.join(argv.rundir, 'db'), {recursive: true});
fs.mkdirSync(path.join(argv.rundir, 'temp'), {recursive: true});
} catch (error) {
console.error('Failed to access runtime directory');
process.exit(1);
}
const DEFAULT_SECRET_PATH = path.join(argv.rundir, 'flood.secret');
let secret;

View File

@@ -26,8 +26,10 @@ process.argv.push('--rtsocket', rTorrentSocket);
process.argv.push('--allowedpath', temporaryRuntimeDirectory);
process.argv.push('--rtorrent');
process.argv.push('--rtconfig', `${temporaryRuntimeDirectory}/rtorrent.rc`);
process.argv.push('--test');
afterAll((done) => {
process.kill(Number(fs.readFileSync(`${temporaryRuntimeDirectory}/rtorrent.pid`).toString()));
if (process.env.CI !== 'true') {
// TODO: This leads to test flakiness caused by ENOENT error
// NeDB provides no method to close database connection