fix: move daemonization to process

This commit is contained in:
Alexis Tyler
2021-03-08 15:59:12 +10:30
parent e8a95d4f0c
commit b7867f66dc
3 changed files with 33 additions and 193 deletions

View File

@@ -1,8 +1,7 @@
import fs from 'fs';
import path from 'path';
import { spawn } from 'child_process';
import { parse, ArgsParseOptions, ArgumentConfig } from 'ts-command-line-args';
import dotEnv from 'dotenv';
import daemonizeProcess from 'daemonize-process';
import findProcess from 'find-process';
import pidusage from 'pidusage';
import prettyMs from 'pretty-ms';
@@ -87,9 +86,29 @@ const commands = {
require(indexPath);
if (!mainOptions.debug) {
// Convert process into daemon
console.log('Demonising process.');
daemonizeProcess();
if ('_DAEMONIZE_PROCESS' in process.env) {
// In the child, clean up the tracking environment variable
delete process.env._DAEMONIZE_PROCESS;
} else {
console.debug('Daemonizing process.');
// Spawn child
const child = spawn(process.execPath, process.argv.slice(2), {
// In the parent set the tracking environment variable
env: Object.assign(process.env, { _DAEMONIZE_PROCESS: '1' }),
cwd: process.cwd(),
stdio: 'ignore',
detached: true
});
// Convert process into daemon
child.unref();
console.log('Daemonized successfully!');
// Exit cleanly
process.exit(0);
}
}
},
/**
@@ -98,7 +117,8 @@ const commands = {
async stop() {
// Find and kill all processes called "unraid-api"
const foundProcesses = await findProcess('name', 'unraid-api');
if (foundProcesses.length === 1) {
if (foundProcesses.length <= 1) {
console.log('Found no running processes.');
return;
}
@@ -138,12 +158,14 @@ const commands = {
console.log(`API has been running for ${prettyMs(stats.elapsed)} and is in "${environment}" mode!`);
},
async report() {
// Find all processes called "unraid-api"
const foundProcesses = await findProcess('name', 'unraid-api');
const unraidVersion = fs.existsSync('/etc/unraid-version') ? fs.readFileSync('/etc/unraid-version', 'utf8').split('"')[1] : 'unknown';
console.log(dedent`
<-----UNRAID-API-REPORT----->
Env ${environment}
Node API v${version}
Unraid ${unraidVersion}
Environment: ${environment}
Node API version: ${version} (${foundProcesses.length >= 2 ? 'running' : 'stopped'})
Unraid version: ${unraidVersion}
</----UNRAID-API-REPORT----->
`);
}

179
package-lock.json generated
View File

@@ -1235,16 +1235,6 @@
"@types/node": "*"
}
},
"@types/glob": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz",
"integrity": "sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==",
"dev": true,
"requires": {
"@types/minimatch": "*",
"@types/node": "*"
}
},
"@types/graphql-upload": {
"version": "8.0.4",
"resolved": "https://registry.npmjs.org/@types/graphql-upload/-/graphql-upload-8.0.4.tgz",
@@ -1325,12 +1315,6 @@
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
"integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw=="
},
"@types/minimatch": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz",
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==",
"dev": true
},
"@types/minimist": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.1.tgz",
@@ -5358,99 +5342,6 @@
}
}
},
"del-cli": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/del-cli/-/del-cli-3.0.1.tgz",
"integrity": "sha512-BLHItGr82rUbHhjMu41d+vw9Md49i81jmZSV00HdTq4t+RTHywmEht/23mNFpUl2YeLYJZJyGz4rdlMAyOxNeg==",
"dev": true,
"requires": {
"del": "^5.1.0",
"meow": "^6.1.1"
},
"dependencies": {
"del": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/del/-/del-5.1.0.tgz",
"integrity": "sha512-wH9xOVHnczo9jN2IW68BabcecVPxacIA3g/7z6vhSU/4stOKQzeCRK0yD0A24WiAAUJmmVpWqrERcTxnLo3AnA==",
"dev": true,
"requires": {
"globby": "^10.0.1",
"graceful-fs": "^4.2.2",
"is-glob": "^4.0.1",
"is-path-cwd": "^2.2.0",
"is-path-inside": "^3.0.1",
"p-map": "^3.0.0",
"rimraf": "^3.0.0",
"slash": "^3.0.0"
}
},
"globby": {
"version": "10.0.2",
"resolved": "https://registry.npmjs.org/globby/-/globby-10.0.2.tgz",
"integrity": "sha512-7dUi7RvCoT/xast/o/dLN53oqND4yk0nsHkhRgn9w65C4PofCLOoJ39iSOg+qVDdWQPIEj+eszMHQ+aLVwwQSg==",
"dev": true,
"requires": {
"@types/glob": "^7.1.1",
"array-union": "^2.1.0",
"dir-glob": "^3.0.1",
"fast-glob": "^3.0.3",
"glob": "^7.1.3",
"ignore": "^5.1.1",
"merge2": "^1.2.3",
"slash": "^3.0.0"
}
},
"hosted-git-info": {
"version": "2.8.8",
"resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz",
"integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==",
"dev": true
},
"meow": {
"version": "6.1.1",
"resolved": "https://registry.npmjs.org/meow/-/meow-6.1.1.tgz",
"integrity": "sha512-3YffViIt2QWgTy6Pale5QpopX/IvU3LPL03jOTqp6pGj3VjesdO/U8CuHMKpnQr4shCNCM5fd5XFFvIIl6JBHg==",
"dev": true,
"requires": {
"@types/minimist": "^1.2.0",
"camelcase-keys": "^6.2.2",
"decamelize-keys": "^1.1.0",
"hard-rejection": "^2.1.0",
"minimist-options": "^4.0.2",
"normalize-package-data": "^2.5.0",
"read-pkg-up": "^7.0.1",
"redent": "^3.0.0",
"trim-newlines": "^3.0.0",
"type-fest": "^0.13.1",
"yargs-parser": "^18.1.3"
}
},
"normalize-package-data": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz",
"integrity": "sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA==",
"dev": true,
"requires": {
"hosted-git-info": "^2.1.4",
"resolve": "^1.10.0",
"semver": "2 || 3 || 4 || 5",
"validate-npm-package-license": "^3.0.1"
}
},
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
},
"type-fest": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.13.1.tgz",
"integrity": "sha512-34R7HTnG0XIJcBSn5XhDd7nNFPRcXYRZrBB2O2jdKqYODldSzBAqzsWoZYYvduky73toYS/ESqxPvkDf/F0XMg==",
"dev": true
}
}
},
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
@@ -10294,76 +10185,6 @@
"integrity": "sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==",
"dev": true
},
"move-file": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/move-file/-/move-file-2.0.0.tgz",
"integrity": "sha512-cdkdhNCgbP5dvS4tlGxZbD+nloio9GIimP57EjqFhwLcMjnU+XJKAZzlmg/TN/AK1LuNAdTSvm3CPPP4Xkv0iQ==",
"dev": true,
"requires": {
"path-exists": "^4.0.0"
}
},
"move-file-cli": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/move-file-cli/-/move-file-cli-2.0.0.tgz",
"integrity": "sha512-8VydABdgie+0DorUA0mf0D1neJFcf/c+sGQv1wPjPT8NjKefZ/DHW+q+UIz7fXn6jiUxeRKZakyHYryPl8HnVg==",
"dev": true,
"requires": {
"meow": "^7.0.1",
"move-file": "^2.0.0"
},
"dependencies": {
"hosted-git-info": {
"version": "2.8.8",
"resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz",
"integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==",
"dev": true
},
"meow": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/meow/-/meow-7.1.1.tgz",
"integrity": "sha512-GWHvA5QOcS412WCo8vwKDlTelGLsCGBVevQB5Kva961rmNfun0PCbv5+xta2kUMFJyR8/oWnn7ddeKdosbAPbA==",
"dev": true,
"requires": {
"@types/minimist": "^1.2.0",
"camelcase-keys": "^6.2.2",
"decamelize-keys": "^1.1.0",
"hard-rejection": "^2.1.0",
"minimist-options": "4.1.0",
"normalize-package-data": "^2.5.0",
"read-pkg-up": "^7.0.1",
"redent": "^3.0.0",
"trim-newlines": "^3.0.0",
"type-fest": "^0.13.1",
"yargs-parser": "^18.1.3"
}
},
"normalize-package-data": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz",
"integrity": "sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA==",
"dev": true,
"requires": {
"hosted-git-info": "^2.1.4",
"resolve": "^1.10.0",
"semver": "2 || 3 || 4 || 5",
"validate-npm-package-license": "^3.0.1"
}
},
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
},
"type-fest": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.13.1.tgz",
"integrity": "sha512-34R7HTnG0XIJcBSn5XhDd7nNFPRcXYRZrBB2O2jdKqYODldSzBAqzsWoZYYvduky73toYS/ESqxPvkDf/F0XMg==",
"dev": true
}
}
},
"mqtt": {
"version": "github:OmgImAlexis/MQTT.js#10dd30e805e5a81db661fd636d95594316ccea16",
"from": "github:OmgImAlexis/MQTT.js#master",

View File

@@ -10,8 +10,8 @@
"build-app": "npx tsup ./app/index.ts",
"build-cli": "npx tsup ./app/cli.ts",
"build-binary-validation": "node -e \"require('fs').exists('/etc/unraid-version', exists => {if (!exists) {console.log('PLEASE RUN THIS ON AN UNRAID 6.9.0 or newer MACHINE');process.exit(1);}})\"",
"build-binary-step-1": "nexe ./dist/cli.js --build -r './dist/**/*' -r './node_modules/ts-command-line-args/dist/helpers/**/*' -r './node_modules/get-current-line/**/*'",
"build-binary-step-2": "del node_modules && del dist && move-file ./cli ./unraid-api",
"build-binary-step-1": "nexe ./dist/cli.js --build -r './dist/**/*' -r './node_modules' && mv ./cli ./unraid-api && echo '✔ Binary built: ./unraid-api'",
"build-binary-step-2": "rm -rf ./node_modules && rm -rf ./dist && echo '✔ Source files deleted'",
"build-binary": "npm run build-binary-validation && npm run build-binary-step-1 && npm run build-binary-step-2",
"copy-schemas": "cpx app/**/*.graphql dist/types",
"clean": "modclean --no-progress --run --path .",
@@ -60,7 +60,6 @@
"cli-table": "^0.3.4",
"command-exists": "^1.2.9",
"cross-fetch": "^3.0.6",
"daemonize-process": "^3.0.0",
"deepmerge": "^4.2.2",
"dl-tgz": "^0.7.1",
"dockerode": "^3.2.1",
@@ -149,11 +148,9 @@
"bundle-dependencies": "^1.0.2",
"cpx": "1.5.0",
"cz-conventional-changelog": "3.3.0",
"del-cli": "^3.0.1",
"eslint": "^7.20.0",
"esprint": "^2.0.0",
"modclean": "^3.0.0-beta.1",
"move-file-cli": "^2.0.0",
"node-env-run": "^4.0.2",
"p-each-series": "^2.2.0",
"source-map-support": "0.5.19",