From 80e9cf275767cba59f8d5314e4cb27caf6192f3d Mon Sep 17 00:00:00 2001 From: przemek-nowicki Date: Wed, 4 May 2022 23:22:27 +0200 Subject: [PATCH] read and write pid using file system --- bin/rnr.js | 17 +++++++++++-- index.js | 62 ++++++++++++++++++++++++++--------------------- package-lock.json | 16 ++++++++++++ package.json | 2 +- 4 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 package-lock.json diff --git a/bin/rnr.js b/bin/rnr.js index 6de3e70..ef69f89 100644 --- a/bin/rnr.js +++ b/bin/rnr.js @@ -2,7 +2,20 @@ "use strict"; -console.log('Inside of RnR bin!') -console.log(process); +const fs = require('fs'); +const path = require('path'); +const os = require('os'); +var args = process.argv[2]; +fs.readFile(path.join(os.tmpdir(), 'rnr.pid'), 'utf8', function (err,data) { + if (err) { + return console.log(err); + } + const pid = parseInt(data); + if (pid > 0) { + process.kill(pid, "SIGPIPE"); + } else { + console.error('pid not found in the storage'); + } +}); diff --git a/index.js b/index.js index 39b189b..21b5c2f 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,40 @@ -'use strict'; + 'use strict'; -let cbToCall; -const process = global.process + const fs = require('fs'); + const path = require('path'); + const os = require('os'); -const isProcessOk = (process) => { - return process && - typeof process === 'object' && - typeof process.kill === 'function' && - typeof process.pid === 'number' && - typeof process.on === 'function' -} + let cbToCall; + const process = global.process -const reloadCbFn = (cb) => { - cbToCall = cb; -}; - -process.on('SIGPIPE', () => { - if (typeof cbToCall === 'function') { - cbToCall(); - } else { - console.error('SIGPIPE triggered but no callback provided to execute!'); + const isProcessOk = (process) => { + return process && + typeof process === 'object' && + typeof process.kill === 'function' && + typeof process.pid === 'number' && + typeof process.on === 'function' } -}); -if (!isProcessOk(process)) { - module.exports = () => { - return () => {} - } -} else { - module.exports = reloadCbFn; -} \ No newline at end of file + const reloadCbFn = (cb) => { + cbToCall = cb; + fs.writeFile(path.join(os.tmpdir(), 'rnr.pid'), process.pid.toString(), function (err) { + if (err) return console.log(err); + console.log('rnr.pid saved successfully'); + }); + }; + + process.on('SIGPIPE', () => { + if (typeof cbToCall === 'function') { + cbToCall(); + } else { + console.error('SIGPIPE triggered but no callback provided to execute!'); + } + }); + + if (!isProcessOk(process)) { + module.exports = () => { + return () => {} + } + } else { + module.exports = reloadCbFn; + } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1a0a276 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,16 @@ +{ + "name": "runtime-node-refresh", + "version": "1.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "runtime-node-refresh", + "version": "1.0.1", + "license": "ISC", + "bin": { + "runtime-node-refresh": "bin/rnr.js" + } + } + } +} diff --git a/package.json b/package.json index 73b78a8..16fc435 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "runtime-node-refresh", - "version": "1.0.1", + "version": "1.0.2", "bin": "./bin/rnr.js", "description": "Refresh node environment variables on runtime without restarting the node server.", "main": "index.js",