Skip to content

Commit

Permalink
read and write pid using file system
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek-nowicki committed May 4, 2022
1 parent e6e424f commit 80e9cf2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
17 changes: 15 additions & 2 deletions bin/rnr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
62 changes: 35 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
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;
}
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 80e9cf2

Please sign in to comment.