From 4190dc5ab7d6b5ceacffeb329292610ae15bbef0 Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Mon, 20 Apr 2020 11:35:49 -0700 Subject: [PATCH 1/2] Add an option to not run hotel on startup This default is surely useful for many people, but it's not right for me: I have many projects on my computer, and I only want to start hotel if I'm working on certain ones. Now there's an option to disable that behavior. I cribbed what it does from user-startup, minus the startup parts. --- README.md | 5 ++++- src/cli/daemon.js | 26 ++++++++++++++++++++------ src/conf.js | 4 +++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7458562c..625fbfd5 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,10 @@ By default, `hotel` uses the following configuration values: "tld": 'localhost', // If you're behind a corporate proxy, replace this with your network proxy IP (example: "1.2.3.4:5000") - "proxy": false + "proxy": false, + + // Change to false to prevent hotel from starting itself up automatically on login + "autostart": true } ``` diff --git a/src/cli/daemon.js b/src/cli/daemon.js index 4716d320..f85fc049 100644 --- a/src/cli/daemon.js +++ b/src/cli/daemon.js @@ -1,3 +1,4 @@ +const cp = require('child_process') const fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') @@ -15,14 +16,27 @@ module.exports = { function start() { const node = process.execPath const daemonFile = path.join(__dirname, '../daemon') - const startupFile = startup.getFile('hotel') - startup.create('hotel', node, [daemonFile], common.logFile) + if (conf.autostart) { + const startupFile = startup.getFile('hotel') + startup.create('hotel', node, [daemonFile], common.logFile) - // Save startup file path in ~/.hotel - // Will be used later by uninstall script - mkdirp.sync(common.hotelDir) - fs.writeFileSync(common.startupFile, startupFile) + // Save startup file path in ~/.hotel + // Will be used later by uninstall script + mkdirp.sync(common.hotelDir) + fs.writeFileSync(common.startupFile, startupFile) + } else { + const fd = fs.openSync(common.logFile, 'w') + const opts = { + detached: true, // needed to unref() below + stdio: ['ignore', fd, fd] + } + + cp + .spawn(node, [daemonFile], opts) + .on('error', console.log) + .unref() + } console.log(`Started http://localhost:${conf.port}`) } diff --git a/src/conf.js b/src/conf.js index 717d150d..36b9995c 100644 --- a/src/conf.js +++ b/src/conf.js @@ -13,7 +13,9 @@ const defaults = { tld: 'localhost', // Replace with your network proxy IP (1.2.3.4:5000) if any // For example, if you're behind a corporate proxy - proxy: false + proxy: false, + // Set to false to disable automatic running on startup. + autostart: true } // Create empty conf it it doesn't exist From 7b865dde4545a00adbf26345c7692bf04f029b0c Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Mon, 20 Apr 2020 14:57:44 -0700 Subject: [PATCH 2/2] Don't start if the pidfile exists @dbraley noticed that we'll now try to start multiple processes if you run `hotel start` multiple times. (We already did, on Linux, but not on Mac.) Now, we check for the pidfile, and report and exit if there is one. (Note that this fixes issue for Linux in the case where we do autostart as well.) --- src/cli/daemon.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/cli/daemon.js b/src/cli/daemon.js index f85fc049..31abfbc2 100644 --- a/src/cli/daemon.js +++ b/src/cli/daemon.js @@ -5,6 +5,7 @@ const mkdirp = require('mkdirp') const startup = require('user-startup') const common = require('../common') const conf = require('../conf') +const pidFile = require('../pid-file') const uninstall = require('../scripts/uninstall') module.exports = { @@ -17,6 +18,12 @@ function start() { const node = process.execPath const daemonFile = path.join(__dirname, '../daemon') + const pid = pidFile.read() + if (pid) { + console.log(`Already running (process ${pid})`) + return + } + if (conf.autostart) { const startupFile = startup.getFile('hotel') startup.create('hotel', node, [daemonFile], common.logFile)