Skip to content

Commit

Permalink
move io operations to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek-nowicki committed May 5, 2022
1 parent 80e9cf2 commit 703c39f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 48 deletions.
26 changes: 12 additions & 14 deletions bin/rnr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
8 changes: 8 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -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;
65 changes: 31 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
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;
}
19 changes: 19 additions & 0 deletions storage.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 703c39f

Please sign in to comment.