diff --git a/bin/rnr.js b/bin/rnr.js index ef69f89..44fa53d 100644 --- a/bin/rnr.js +++ b/bin/rnr.js @@ -2,20 +2,18 @@ "use strict"; -const fs = require('fs'); -const path = require('path'); -const os = require('os'); +var config = require('../config') +var storage = require('../storage'); -var args = process.argv[2]; +var data = storage.read(); +var pid = parseInt(data); -fs.readFile(path.join(os.tmpdir(), 'rnr.pid'), 'utf8', function (err,data) { - if (err) { - return console.log(err); +if (pid > 0) { + try { + process.kill(pid, config.SIGNAL_EVENT); + } catch(e) { + console.error('Could not connect to PID ' + pid); } - const pid = parseInt(data); - if (pid > 0) { - process.kill(pid, "SIGPIPE"); - } else { - console.error('pid not found in the storage'); - } -}); +} else { + console.error('pid not found in the storage'); +} \ No newline at end of file diff --git a/config.js b/config.js new file mode 100644 index 0000000..ae007cd --- /dev/null +++ b/config.js @@ -0,0 +1,8 @@ +var path = require('path'); +var os = require('os'); + +var SIGNAL_EVENT = 'SIGPIPE'; +var STORAGE_SOURCE = path.join(os.tmpdir(), 'rnr.pid'); + +module.exports.SIGNAL_EVENT = SIGNAL_EVENT; +module.exports.STORAGE_SOURCE = STORAGE_SOURCE; \ No newline at end of file diff --git a/index.js b/index.js index 21b5c2f..1623eca 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,37 @@ - 'use strict'; +'use strict'; - const fs = require('fs'); - const path = require('path'); - const os = require('os'); +var config = require('./config'); +var storage = require('./storage'); - let cbToCall; - const process = global.process +var cbToCall; +var process = global.process; - const isProcessOk = (process) => { - return process && - typeof process === 'object' && - typeof process.kill === 'function' && - typeof process.pid === 'number' && - typeof process.on === 'function' - } - - 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'); - }); - }; +function isProcessOk(process) { + return process && + typeof process === 'object' && + typeof process.kill === 'function' && + typeof process.pid === 'number' && + typeof process.on === 'function' +} - process.on('SIGPIPE', () => { - if (typeof cbToCall === 'function') { - cbToCall(); - } else { - console.error('SIGPIPE triggered but no callback provided to execute!'); - } - }); +function reloadCbFn(cb) { + cbToCall = cb; + storage.write(process.pid.toString()); +}; - if (!isProcessOk(process)) { - module.exports = () => { - return () => {} - } +process.on(config.SIGNAL_EVENT, function() { + if (typeof cbToCall === 'function') { + cbToCall(); } else { - module.exports = reloadCbFn; - } \ No newline at end of file + console.error('SIGPIPE triggered but no callback provided to execute'); + } +}); + +if (!isProcessOk(process)) { + console.error('Invalid nodejs process object detected') + module.exports = function () { + return function () {} + } +} else { + module.exports = reloadCbFn; +} \ No newline at end of file diff --git a/storage.js b/storage.js new file mode 100644 index 0000000..b64cc42 --- /dev/null +++ b/storage.js @@ -0,0 +1,19 @@ +var fs = require('fs'); +var config = require('./config'); + +function write(data) { + fs.writeFile(config.STORAGE_SOURCE, data, function (err) { + if (err) return console.error(err); + }); +} + +function read() { + if(!fs.existsSync(config.STORAGE_SOURCE)) { + console.error("File not found"); + } + return fs.readFileSync(config.STORAGE_SOURCE); +} + +module.exports.write = write; +module.exports.read = read; +