From 0f4c4b6555316e3985b83411057fa8d3b871d7df Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Tue, 1 Dec 2020 00:41:36 +0800 Subject: [PATCH] config.cli: store PID of managed rTorrent process --- config.cli.js | 46 +++++++++++++++++++++------------- server/.jest/rtorrent.setup.js | 2 ++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/config.cli.js b/config.cli.js index 44d3cbed..e8aa157d 100644 --- a/config.cli.js +++ b/config.cli.js @@ -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; diff --git a/server/.jest/rtorrent.setup.js b/server/.jest/rtorrent.setup.js index 1a112093..11d0e7de 100644 --- a/server/.jest/rtorrent.setup.js +++ b/server/.jest/rtorrent.setup.js @@ -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