Skip to content

Commit

Permalink
0.2.1 pidlink with retry, test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldrudell committed Jan 12, 2013
1 parent 887161a commit 967a904
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
60 changes: 32 additions & 28 deletions lib/pidlink.js
Expand Up @@ -15,12 +15,15 @@ init, getData

var time2s = 2e3
var time500ms = 5e2
var initialWait = time2s
var waitTime = time500ms
var signal = 'SIGUSR2'

var folder
var log = console.log
var settings = {
initialWait: time2s,
waitTime: time500ms,
retries: 20,
signal: 'SIGUSR2',
folder: undefined,
log: console.log,
}

// get error emitter without being an api
var r = require; var require = require('apprunner').getRequire(r, null, {emScope: 'pidlink',})
Expand All @@ -37,18 +40,11 @@ opts: optional object
The signal handler in the other process is the apprunner module's appshutdown file.
*/
function init(opts) {
var result = {
folder: folder,
signal: signal,
initialWait: initialWait,
waitTime: waitTime
}
if (opts) {
if (opts.folder != null) folder = opts.folder
if (opts.signal) signal = opts.signal
if (opts.initialWait != null) initialWait = opts.initialWait
if (opts.waitTime != null) waitTime = opts.waitTime
if (typeof opts.log == 'function') log = opts.log
if (!opts) opts = {}
var result = {}
for (var p in settings) {
result[p] = settings[p]
if (opts[p] != null) settings[p] = opts[p]
}
return result
}
Expand All @@ -71,27 +67,29 @@ function getData(pid, slogan, cb) {
var result
var err
var invocation = new Error('invocationStack')
var retries = 1

// filename: must have pid and folder
if (!folder) end(new Error('Folder missing'))
if (!settings.folder) end(new Error('Folder missing'))
pid = +pid
if (!(pid > 0)) end(new Error('Bad process id: ' + pid))
var file = path.join(folder, pid + '.json')
var file = path.join(settings.folder, pid + '.json')

var cbCounter = 2
fs.unlink(file, sendSignal) // delete any pre-existing file
setTimeout(sendSignal, initialWait) // wait for app to start
setTimeout(sendSignal, settings.initialWait) // wait for app to start
var otherProcess = slogan ? slogan + ':' + pid : pid

function sendSignal(e) {
if (e && e.code == 'ENOENT') e = null // ignore file not found
if (!e && !err) {
if (!--cbCounter) {
try { // instruct process to write to filesystem
var x = process.kill(pid, signal) // returns true
var x = process.kill(pid, settings.signal) // returns true

log('Sent signal:', signal, 'to', otherProcess)
setTimeout(fetchData, waitTime)
settings.log('Sent signal:', settings.signal, 'to', otherProcess, (new Date).toISOString())
retries = settings.retries
setTimeout(fetchData, settings.waitTime)
} catch (e) { // if process can't be found, it probably died already
end() //e.code == 'ESRCH'
}
Expand All @@ -106,13 +104,19 @@ function getData(pid, slogan, cb) {
function readResult(e, data) {
if (!e) {
var obj = greatjson.parse(data)
if (!(obj instanceof Error)) result = obj
else err = obj // saving parsing trouble
fs.unlink(file, end)
if (!(obj instanceof Error)) {
result = obj
fs.unlink(file, end)
} else if (!--retries) {
err = obj // saving parsing trouble
fs.unlink(file, end)
}
} else {
if (e.code == 'ENOENT') {
log('No data written by:', otherProcess)
end()
if (!--retries) {
settings.log('No data written by:', otherProcess, (new Date).toISOString())
end()
} else setTimeout(fetchData, settings.waitTime)
} else end(e)
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"node",
"operations"
],
"version": "0.2.0",
"version": "0.2.1",
"contributors": [
{
"name": "Harald Rudell",
Expand Down
7 changes: 6 additions & 1 deletion test/test-masterserver.js
Expand Up @@ -48,13 +48,18 @@ exports['MasterServer:'] = {
var eEvents = ['error', 'close']
var aRListener = 0
var aOn = {}
var ignoreProperties = ['domain', '_events', '_maxListeners']

// constructor
net.createServer = function (f) {aCreate.push(f); return server}
var masterServer = new masterserver.MasterServer(port, interface)

assert.ok(masterServer instanceof events.EventEmitter)
assert.equal(Object.keys(masterServer).length, 2)
var keys = []
Object.keys(masterServer).forEach(function (p) {
if (!~ignoreProperties.indexOf(p)) keys.push(p)
})
assert.equal(keys.length, 2, 'List is: ' + keys)
assert.equal(typeof masterServer.shutdown, 'function')
assert.equal(typeof masterServer.isUp, 'function')
assert.equal(aCreate.length, 1)
Expand Down
8 changes: 7 additions & 1 deletion test/test-perioder.js
Expand Up @@ -11,9 +11,15 @@ exports['Perioder:'] = {
assert.exportsTest(perioder, 1)
},
'TimeEmitter GetState Cancel': function () {
var ignoreProperties = ['domain', '_events', '_maxListeners']

var t = new perioder.TimeEmitter() // timer for beginning of the month
assert.ok(t)
assert.equal(Object.keys(t).length, 2)
var keys = []
Object.keys(t).forEach(function (p) {
if (!~ignoreProperties.indexOf(p)) keys.push(p)
})
assert.equal(keys.length, 2, 'List is: ' + keys)
assert.equal(typeof t.cancel, 'function')
assert.equal(typeof t.getState, 'function')

Expand Down

0 comments on commit 967a904

Please sign in to comment.