-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80e9cf2
commit 703c39f
Showing
4 changed files
with
70 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|