Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.3.0 Node God as four processes, rotates log on size and time, Licen…

…se, ChangeLog
  • Loading branch information...
commit 954f5e17ee6ce6a98ed7fac943f1318805719e04 1 parent 047dbf4
@haraldrudell authored
Showing with 693 additions and 113 deletions.
  1. +19 −0 ChangeLog
  2. +9 −0 LICENSE
  3. +18 −10 app.js
  4. +2 −1  lib/appentity.js
  5. +1 −1  lib/appstate.js
  6. +121 −0 lib/childipc.js
  7. +54 −9 lib/fslogpipe.js
  8. +1 −0  lib/getfilenames.js
  9. +4 −1 lib/godcontrol.js
  10. +2 −2 lib/godmodel.js
  11. +2 −1  lib/godsocket.js
  12. +33 −0 lib/listenermanager.js
  13. +1 −1  lib/master/appconduit.js
  14. +13 −19 lib/master/logpipe.js
  15. +1 −1  lib/master/masterserver.js
  16. +11 −7 lib/master/nodegodmaster.js
  17. +1 −1  lib/master/pidgetter.js
  18. +1 −1  lib/master/processfinder.js
  19. +1 −1  lib/master/streamlabeller.js
  20. +4 −4 lib/master/uimanager.js
  21. +78 −0 lib/masterlauncher.js
  22. +1 −1  lib/masterlink.js
  23. +5 −3 lib/nodegodweb.js
  24. +85 −0 lib/parentipc.js
  25. +1 −1  lib/pidlink.js
  26. +4 −3 { → lib}/routes/godview.js
  27. +1 −1  lib/streamcopier.js
  28. 0  { → lib}/views/index.ejs
  29. 0  { → lib}/views/layout.ejs
  30. 0  { → lib}/views/partials/app.ejs
  31. +2 −0  lib/watchcopy.js
  32. +1 −1  lib/watchit.js
  33. +6 −1 logprocess.js
  34. +19 −0 masterprocess.js
  35. +7 −6 package.json
  36. +22 −11 test/test-app.js
  37. +15 −0 test/test-childipc.js
  38. +15 −0 test/test-listenermanager.js
  39. +27 −0 test/test-masterlauncher.js
  40. +39 −0 test/test-masterprocess.js
  41. +15 −12 test/test-nodegodmaster.js
  42. +15 −0 test/test-parentipc.js
  43. +15 −0 test/test-streamcopier.js
  44. +18 −12 test/test-uimanager.js
  45. +2 −0  test/test-webprocess.js
  46. +1 −1  webprocess.js
View
19 ChangeLog
@@ -0,0 +1,19 @@
+2013-01-20 0.3.0 Node God as four processes, rotates log on size and time, License, ChangeLog
+2013-01-14 0.2.3 one-line top-level files, nodegodweb api rewrote godmodel, apprunner 0.2.2
+2013-01-12 0.2.2 refactored perioder, rotatedlogger with rename-retry
+2013-01-12 0.2.1 pidlink with retry, test pass
+2013-01-10 0.2.0 resilitient master process architecture change
+2013-01-02 0.1.4 master using socket server. ui can now recycle with apps running
+2012-12-29 0.1.3 support haraldutill 0.1.4
+2012-12-25 0.1.2 refactor master process, tests
+2012-12-22 0.1.1 recovery now continuous, log uses getISOPacific
+2012-12-22 0.1.0 master process and ipc
+2012-09-20 0.0.11 force kill printout
+2012-09-14 0.0.10 top-color red on lost connection, loggin of changes only
+2012-09-11 0.0.9 sigint sigusr2 better watchdelay warning coloring apprunner
+2012-09-10 0.0.8 shutdown with SIGINT then kill
+2012-09-03 0.0.7 Watch events update watcher files, watchCopy, haraldutil 0.1.5
+2012-07-23 0.0.6: support haraldops 0.1.1
+2012-07-02 0.0.5 better comms and updates
+2012-06-23 0.0.4 Fixed file watchers
+2012-05-27 v 0.0.1 back to express 2, sockets in appFile loading
View
9 LICENSE
@@ -0,0 +1,9 @@
+Copyright © Harald Rudell 2011 MIT License
+
+The MIT License (MIT)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
28 app.js
@@ -1,13 +1,21 @@
// app.js
// Nodegod manages apps
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
-var appIdentifier = 'nodegodmaster'
-require('./lib/master/nodegodmaster').run({
- port: 1113,
- interface: '127.0.0.1',
- ignoredSignals: ['SIGINT', 'SIGUSR2', 'SIGHUP'],
- appIdentifier: appIdentifier,
- launchArray: ['node', require('path').join(__dirname, 'webprocess')],
- fsLogArray: ['node', require('path').join(__dirname, 'logprocess'), appIdentifier],
-})
+require('apprunner').initApp(require('haraldops').init({
+ appName: 'Node God',
+ noFile: true, // haraldops: don't load a settings file
+ noInfoLog: true, // appinit quiet
+ logger: function () {}, // haraldops quiet
+ api: {
+ apiMap: {
+ masterlauncher: {
+ onLoad: true,
+ spawn: {
+ file: 'node',
+ args: [require('path').join(__dirname, 'masterprocess')],
+ }
+ }
+ }
+ }
+}))
View
3  lib/appentity.js
@@ -1,5 +1,6 @@
// appentity.js
-// © Harald Rudell 2012
+// Manage data for one app
+// © Harald Rudell 2012 MIT License
var require = require('apprunner').getRequire(require)
var pidlink = require('./pidlink')
View
2  lib/appstate.js
@@ -1,6 +1,6 @@
// appstate.js
// manage one app through the master process
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
var masterlink = require('./masterlink')
View
121 lib/childipc.js
@@ -0,0 +1,121 @@
+// childipc.js
+// Write ipc to a child process
+// © Harald Rudell 2013 <harald@therudells.com> MIT License
+
+// http://nodejs.org/api/util.html
+var util = require('util')
+// http://nodejs.org/api/stream.html
+var stream = require('stream')
+
+exports.ChildIpc = ChildIpc
+
+/*
+ipc write stream for child
+if this.writable ipc is available
+will emit drain, close and errors
+final event is close or error
+*/
+function ChildIpc() {
+ var self = this
+ stream.Stream.call(this)
+ this.writable
+ this.write = write
+ this.end = end
+ this.destroy = destroy
+ var didEmitError
+ var isConnected
+
+ if (process.send) { // process.once does not return process...
+ process.on('error', processErrorListener)
+ .once('disconnect', processDisconnectListener)
+ isConnected = true
+ this.writable = true
+ }
+
+ // write(message, handle, cb)
+ function write(message, handle) {
+ var result = false
+ var err
+ var args = Array.prototype.slice.call(arguments)
+ var cb = typeof args[args.length - 1] === 'function' ? args.pop() : null
+ if (args.length > 1 && typeof handle === 'string') err = new Error('write handle argument cannot be string')
+ else if (!self.writable) err = new Error('ipc not available')
+ else {
+ try {
+ result = process.send.apply(process, args)
+ } catch (e) {
+ err = e
+ }
+ if (!err) self.emit('drain')
+ }
+ if (err) internalDestroy(cb, [err])
+ else if (cb) {
+ var cbArgs = []
+ if (err) cbArgs.push(err)
+ cb.apply(this, cbArgs)
+ }
+ return result
+ }
+
+ // end(message, handle, cb)
+ function end() {
+ var err
+ var args = Array.prototype.slice.call(arguments)
+ var cb = typeof args[args.length - 1] === 'function' ? args.pop() : null
+ if (self.writable && args.length) write.apply(this, args.concat(afterWrite))
+ else afterWrite()
+
+ function afterWrite(e) {
+ if (e) err = e
+ destroy(afterDestroy)
+ }
+
+ function afterDestroy(e) {
+ if (e && !err) err = e
+ if (cb) {
+ var cbArgs = []
+ if (err) cbArgs.push(err)
+ cb.apply(this, cbArgs)
+ }
+ }
+ }
+
+ function destroy(cb) {
+ internalDestroy(cb)
+ }
+
+ /*
+ cb(err) optional funciton
+ args: array of values, first may be instanceof Error
+ */
+ function internalDestroy(cb, args) {
+ var isError = Array.isArray(args) && args[0] instanceof Error
+ if (self.writable) {
+ self.writable = false
+ if (isConnected) {
+ isConnected = false
+ process.disconnect()
+ process.removeListener('disconnect', processDisconnectListener)
+ }
+ process.removeListener('error', processErrorListener)
+ if (!isError) self.emit('close')
+ }
+ if (isError) self.emit.apply(self, ['error'].concat(args))
+ if (cb) {
+ var cbArgs = []
+ if (isError) cbArgs.push(args[0])
+ cb.apply(this, cbArgs)
+ }
+ }
+
+ function processDisconnectListener() {
+ isConnected = false
+ internalDestroy()
+ }
+ function processErrorListener() {
+ var args = Array.prototype.slice.call(arguments)
+ if (!(args[0] instanceof Error)) args.unshift(new Error('process error'))
+ internalDestroy(null, args)
+ }
+}
+util.inherits(ChildIpc, stream.Stream)
View
63 lib/fslogpipe.js
@@ -2,14 +2,17 @@
// Log stdin to rotatedlogger
// © Harald Rudell 2013 MIT License
-// http://nodejs.org/api/util.html
-var util = require('util')
-
require = require('apprunner').getRequire(require, exports, {
api: require('apprunner').getAppData().appName, initApi: initApi,
ready: false})
var rotatedlogger = require('./rotatedlogger')
+// http://nodejs.org/api/util.html
+var util = require('util')
+// https://github.com/haraldrudell/apprunner
+var apprunner = require('apprunner')
+
+var log = console.log
/*
Write stdin to a rotated log
@@ -19,14 +22,56 @@ opts: object
*/
function initApi(opts) {
var logMarker = require.emitter.id + ':' + process.pid
- var logger = new rotatedlogger.RotatedLog({stdout: false, file: true, streamOpts: {logFile: opts.logName}})
+ log = function () {
+ console.log(logMarker, util.format.apply(this, Array.prototype.slice.call(arguments)))
+ }
+ var logger
+
+ if (Array.isArray(opts.ignoredSignals)) opts.ignoredSignals.forEach(function (signal) {
+ process.on(signal, getSignalHandler(signal))
+ })
+
+
+ if (process.send) {
+ logger = new rotatedlogger.RotatedLog({stdout: false, file: true, streamOpts: {logFile: opts.logName}})
- process.stdin.on('data', echoData) // returns this
- process.stdin.setEncoding('utf-8') // returns undefined
- process.stdin.resume()
- if (process.send) process.send('ok')
+ process.stdin.setEncoding('utf-8') // returns undefined
+ process.stdin.on('data', stdinDataListener) // returns this
+ .once('close', stdinCloseListener)
+ .resume()
+ process.send('ok') // let parent know we are ready
+ } else {
+ log('Fatal: ipc not available')
+ shutdown()
+ }
- function echoData(data) {
+ function stdinDataListener(data) {
logger.write(data)
}
+ function stdinCloseListener() {
+ log('Detected pipe close')
+ logger.write('Log pipe close event ' + (new Date).toISOString())
+ logger.close(cleanup)
+ }
+
+ function cleanup(err) {
+ if (err) log('logger.close:', err)
+ process.stdin.removeListener('data', stdinDataListener)
+ process.stdin.removeListener('close', stdinCloseListener)
+ process.stdin.pause()
+ logger = null
+ shutdown()
+ }
+
+ function shutdown() {
+ apprunner.shutdown()
+ }
+}
+
+function getSignalHandler(signal) {
+ return notifySignal
+
+ function notifySignal() {
+ log('Ignoring signal:', signal)
+ }
}
View
1  lib/getfilenames.js
@@ -1,5 +1,6 @@
// getfilenames.js
// build an array of filenames from a single entry or an array of file and folder names
+// © Harald Rudell 2011 <harald@therudells.com> MIT License
// http://nodejs.org/docs/latest/api/fs.html
var fs = require('fs')
View
5 lib/godcontrol.js
@@ -1,4 +1,7 @@
-// nodereq.js
+// godcontrol.js
+// Controller for Node God
+// © Harald Rudell 2011 MIT License
+
var godmodel = require('./godmodel')
var appentity = require('./appentity')
var godsocket = require('./godsocket')
View
4 lib/godmodel.js
@@ -1,6 +1,6 @@
// godmodel.js
// Execute on configuration files
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
require = require('apprunner').getRequire(require)
@@ -82,7 +82,7 @@ function loadParentData(cb) {
result = data.appMap
end(true)
}
- if (data.launchTime && typeof data.launchTime == 'number') require('../routes/godview').setTitle(undefined, data.launchTime)
+ if (data.launchTime && typeof data.launchTime == 'number') require('./routes/godview').setTitle(undefined, data.launchTime)
}
function end(isMessage) {
View
3  lib/godsocket.js
@@ -1,5 +1,6 @@
// godsocket.js
-// socket layer for nodegod
+// Server-side socket layer for Node Fod
+// © Harald Rudell 2011 MIT License
// imports
var socketio = require('socket.io')
View
33 lib/listenermanager.js
@@ -0,0 +1,33 @@
+// listenermanager.js
+// Manage event listeners for various events.EventEmitters
+// © Harald Rudell 2013 <harald@therudells.com> MIT License
+
+exports.ListenerManager = ListenerManager
+
+function ListenerManager() {
+ var listeners = []
+ this.addListener = addListener
+ this.removeListeners = removeListeners
+
+ /*
+ object: events.EventEmitter
+ fName: string: the function name to use 'on' or 'once'
+ event: string: the event name eg. 'exit'
+ listener: function: the event listener
+ */
+ function addListener(object, fName, event, listener) {
+ listeners.push({
+ object: object[fName](event, listener),
+ event: event,
+ listener: listener,
+ })
+ }
+
+ function removeListeners() {
+ var list = listeners
+ listeners = []
+ list.forEach(function (o) {
+ o.object.removeListener(o.event, o.listener)
+ })
+ }
+}
View
2  lib/master/appconduit.js
@@ -1,6 +1,6 @@
// appconduit.js
// manage a child process for the ui
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
var streamlabeller = require('./streamlabeller')
View
32 lib/master/logpipe.js
@@ -3,7 +3,7 @@
// © Harald Rudell 2013 MIT License
var streamcopier = require('../streamcopier')
-
+var childipc = require('../childipc')
// http://nodejs.org/api/util.html
var util = require('util')
// http://nodejs.org/api/child_process.html
@@ -17,24 +17,26 @@ var spawnOptions = {
stdio: ['pipe', 1, 2, 'ipc'],
}
-function LogPipe(processName, launchArray) {
+function LogPipe(processName, spawn) {
var self = this
streamcopier.StreamCopier.call(this)
- self.connectPipe = connectPipe
- var log = self.pLog
- var pipeIndex
+ this.connectPipe = connectPipe
+ var log = this.setPrepend(processName).pLog
- self.on('warning', copierWarningListener)
- .on('error', copierErrorListener)
- .setPrepend(processName)
- self.addStream(process.stdout, 'stdout')
+ // stream to parent
+ var childIpc = new childipc.ChildIpc()
+ if (childIpc.writable) this.addStream(childIpc, 'parent pipe')
+ else {
+ childIpc.destroy()
+ childIpc = null
+ }
- function connectPipe(cb) {
+ function connectPipe(cb) { // spawn and connect to log process
var child
var timer
var didCb
try {
- var child = child_process.spawn(launchArray[0], launchArray.slice(1), spawnOptions)
+ var child = child_process.spawn(spawn.file, spawn.args, spawnOptions)
} catch (err) {
cb(err)
}
@@ -90,13 +92,5 @@ function LogPipe(processName, launchArray) {
log('Child error: ', err, err.stack)
}
}
-
- function copierWarningListener() {
- console.log(arguments.callee.name, Array.prototype.slice.call(arguments))
- }
-
- function copierErrorListener() {
- console.log(arguments.callee.name, Array.prototype.slice.call(arguments))
- }
}
util.inherits(LogPipe, streamcopier.StreamCopier)
View
2  lib/master/masterserver.js
@@ -1,6 +1,6 @@
// masterserver.js
// Provide interprocess communication via tcp socket
-// © Harald Rudell 2013
+// © Harald Rudell 2013 MIT License
// http://nodejs.org/api/util.html
var util = require('util')
View
18 lib/master/nodegodmaster.js
@@ -1,6 +1,6 @@
// nodegodmaster.js
// Master process of nodegod
-// © Harald Rudell 2013
+// © Harald Rudell 2013 MIT License
var logpipe = require('./logpipe')
var processfinder = require('./processfinder')
@@ -24,7 +24,7 @@ function run(opts) {
// setup logging
processName = opts.appIdentifier + ':' + process.pid
- var logger = new logpipe.LogPipe(processName, opts.fsLogArray)
+ var logger = new logpipe.LogPipe(processName, opts.spawnLog)
log = logger.pLog
var launchISO = new Date(masterLaunchTime).toISOString()
log('Starting master candidate at:', launchISO)
@@ -32,21 +32,25 @@ function run(opts) {
// find out if this process is master
processfinder.isProcessMaster({port: opts.port, interface: opts.interface, processName: processName, log: log}, masterResult)
- function masterResult(isMaster, closeFn) {
+ function masterResult(isMaster, closeServerFn) {
if (isMaster === true) {
log('Is master: launching log process')
logger.connectPipe(master) // start writing to the logFile, not only console
- } else if (typeof isMaster === 'number' && isMaster) log('Exiting: notified existing master with process id:', isMaster) // the master will relaunch its web ui
- else log('Failure communicating with exiting master:', isMaster instanceof Error ? isMaster.message : isMaster)
+ } else {
+ if (typeof isMaster === 'number' && isMaster) log('Exiting: notified existing master with process id:', isMaster) // the master will relaunch its web ui
+ else log('Failure communicating with exiting master:', isMaster instanceof Error ? isMaster.message : isMaster)
+ logger.end()
+ }
function master(err, loggingPid) { // we need to launch the Web ui
if (!err) {
log('Logging process:', loggingPid, 'Launching ui', launchISO)
processfinder.setResetUi(uimanager.getUiRelauncher())
- uimanager.launchUi({launchArray: opts.launchArray, rlog: logger, launchTime: masterLaunchTime})
+ uimanager.launchUi({spawn: opts.spawnWeb, rlog: logger, launchTime: masterLaunchTime})
} else {
log('Log process failed:', err.message)
- closeFn()
+ closeServerFn()
+ logger.end()
}
}
}
View
2  lib/master/pidgetter.js
@@ -1,6 +1,6 @@
// pidgetter.js
// Fetch a pid using a tcp socket
-// © Harald Rudell 2013
+// © Harald Rudell 2013 MIT License
// http://nodejs.org/api/net.html
var net = require('net')
View
2  lib/master/processfinder.js
@@ -1,6 +1,6 @@
// processfinder.js
// determine if an app already has a running master process
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
var masterserver = require('./masterserver')
var pidgetter = require('./pidgetter')
View
2  lib/master/streamlabeller.js
@@ -1,6 +1,6 @@
// streamlabeller.js
// Add logging preceded by a slogan to a child process
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
;[
logSocket, logChild,
View
8 lib/master/uimanager.js
@@ -1,6 +1,6 @@
// uimanager.js
// manages the ui process lifecycle and connects it with appconduit
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
var appconduit = require('./appconduit')
var streamlabeller = require('./streamlabeller')
@@ -18,7 +18,7 @@ var maxBadLaunches = 10
var minChildDurationForRelaunch = time3Seconds
var timeToSigkill = time3Seconds
-var options = {
+var spawnOptions = {
detached: true,
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
}
@@ -30,7 +30,7 @@ var log = console.log
/*
Launch and relaunch the webserver process
opts: object
-.launchArray: array of string: executable command and arguments eg. ['node', '/home..../file.js']
+.spawn: object file, args
.log: rotated streamlabeller
.launchTime: timeval for initial process launch
.restartIntercept for testing
@@ -56,7 +56,7 @@ function launchUi(opts) {
for (var aPid in killFnMap) killFnMap[aPid]()
// launch child process connected to appconduit
- child = child_process.spawn(opts.launchArray[0], opts.launchArray.slice(1), options)
+ child = child_process.spawn(opts.spawn.file, opts.spawn.args, spawnOptions)
.once('exit', uiExit)
.on('message', appconduit.receiveFromUi)
.once('disconnect', appconduit.uiDisconnect)
View
78 lib/masterlauncher.js
@@ -0,0 +1,78 @@
+// masterlauncher.js
+// Launch master process and display output
+// © Harald Rudell 2013 MIT License
+
+require = require('apprunner').getRequire(require, exports, {
+ api: require('apprunner').getAppData().appName, initApi: initApi,
+ ready: false})
+
+var parentipc = require('./parentipc')
+var listenermanager = require('./listenermanager')
+// http://nodejs.org/api/child_process.html
+var child_process = require('child_process')
+// https://github.com/haraldrudell/apprunner
+var apprunner = require('apprunner')
+
+var spawnOptions = {
+ detached: true,
+ stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
+}
+
+var log = console.log
+
+function initApi(opts) {
+ var lm = new listenermanager.ListenerManager
+ var didStdinResume
+ var isShutdown
+
+ // launch and detach from child process
+ var child = child_process.spawn(opts.spawn.file, opts.spawn.args, spawnOptions)
+ var childPid = child.pid
+ child.unref()
+
+ // connect to child ipc
+ var parentIpc = new parentipc.ParentIpc(child)
+ child = null
+ if (parentIpc.readable) {
+ lm.addListener(parentIpc, 'on', 'data', process.stdout.write.bind(process.stdout))
+ lm.addListener(parentIpc, 'once', 'end', ipcEndListener)
+ lm.addListener(parentIpc, 'on', 'error', ipcErrorListener)
+ lm.addListener(process.stdin, 'on', 'data', stdinDataListener)
+ parentIpc.resume()
+ didStdinResume = true
+ process.stdin.resume()
+ log('Launcher:', process.pid, ' master:', childPid, ' Press Enter to end display.')
+ } else shutdown('Fatal: ipc link to child process: ' + childPid + ' missing.')
+
+ function ipcEndListener() {
+ shutdown('End of data')
+ }
+ function ipcErrorListener(err) {
+ shutdown('Pipe read error: ' + err)
+ }
+ function stdinDataListener(data) {
+ shutdown('Keystroke received: exiting')
+ }
+
+ function shutdown(s) {
+ log(s)
+ if (!isShutdown) {
+ isShutdown = true
+ if (didStdinResume) {
+ didStdinResume = false
+ process.stdin.pause()
+ }
+ parentIpc.destroy(exit)
+ }
+ }
+
+ function exit(err) {
+ if (err) log('Error shutting down ipc channel:', err)
+ parentIpc = null
+ if (lm) {
+ lm.removeListeners()
+ lm = null
+ }
+ apprunner.shutdown()
+ }
+}
View
2  lib/masterlink.js
@@ -1,6 +1,6 @@
// masterlink.js
// ipc link to master process
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
// process.send: possible function: sends data to master
// process message event: received data from master
View
8 lib/nodegodweb.js
@@ -1,17 +1,19 @@
// nodegodweb.js
// Launch Node God's Web server
-// © Harald Rudell 2013
+// © Harald Rudell 2013 MIT License
require = require('apprunner').getRequire(require, exports, {
api: 'Node God Web', initApi: initApi,
ready: false})
var godcontrol = require('./godcontrol')
-var godview = require('../routes/godview')
+var godview = require('./routes/godview')
// https://github.com/visionmedia/express
var express = require('express')
// https://github.com/haraldrudell/apprunner
var apprunner = require('apprunner')
+// http://nodejs.org/api/path.html
+var path = require('path')
function initApi(opts) {
var appData = apprunner.getAppData()
@@ -21,7 +23,7 @@ function initApi(opts) {
godview.setTitle(appData.appName)
godcontrol.init(app, opts)
app.configure(function(){
- app.set('views', appData.launchFolder + '/views')
+ app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
//3 app.use(express.favicon())
app.use(express.bodyParser())
View
85 lib/parentipc.js
@@ -0,0 +1,85 @@
+// parentipc.js
+// Read the ipc from a child process
+// © Harald Rudell 2013 <harald@therudells.com> MIT License
+
+// http://nodejs.org/api/util.html
+var util = require('util')
+// http://nodejs.org/api/stream.html
+var stream = require('stream')
+
+exports.ParentIpc = ParentIpc
+
+function ParentIpc(child) {
+ var self = this
+ stream.Stream.call(this)
+ this.readable
+ this.pause = pause
+ this.resume = resume
+ this.destroy = destroy
+ var paused = true
+ var buffer = []
+ var childRemoveListener
+ var childDisconnect
+ var isConnected
+
+ if (child.send) {
+ child.once('disconnect', childDisconnectListener)
+ child.on('message', childMessageListener)
+ childDisconnect = child.disconnect.bind(child)
+ childRemoveListener = child.removeListener.bind(child)
+ isConnected = true
+ this.readable = true
+ }
+ child = null
+
+ function pause() {
+ paused = true
+ }
+ function resume() {
+ paused = false
+ if (self.readable) emitBuffer()
+ }
+ function destroy(cb) {
+ if (self.readable) {
+ self.readable = false
+ removeListeners()
+ buffer = null
+ self.emit('end')
+ }
+ if (cb) cb()
+ }
+
+ function emitBuffer() {
+ while (self.readable && buffer.length) emitData(buffer.shift())
+ if (self.readable && !isConnected) destroy()
+ }
+ function emitData(args) {
+ self.emit('data', args[0]) // json object
+ if (args[1] !== undefined) self.emit('handle', args[1])
+ }
+ function childDisconnectListener() {
+ childDisconnect = null
+ removeListeners(true)
+ if (!buffer.length) destroy()
+ }
+ function removeListeners(skipDisconnect) {
+ if (isConnected) {
+ isConnected = false
+ if (!skipDisconnect) {
+ childRemoveListener('disconnect', childDisconnectListener)
+ childDisconnect()
+ childDisconnect = null
+ }
+ childRemoveListener('message', childMessageListener)
+ childRemoveListener = null
+ }
+ }
+ function childMessageListener(data) {
+ if (self.readable) {
+ var args = Array.prototype.slice.call(arguments)
+ if (paused) buffer.push([args])
+ else emitData(args)
+ }
+ }
+}
+util.inherits(ParentIpc, stream.Stream)
View
2  lib/pidlink.js
@@ -1,6 +1,6 @@
// pidlink.js
// Request json-encoded information from another process
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
// https://github.com/haraldrudell/greatjson
var greatjson = require('greatjson')
View
7 routes/godview.js → lib/routes/godview.js
@@ -1,7 +1,8 @@
-// index.js
-// views for nodegod
+// godview.js
+// View for Node God
+// © Harald Rudell 2011 MIT License
-var godmodel = require('../lib/godmodel')
+var godmodel = require('../godmodel')
module.exports = {
index: index,
View
2  lib/streamcopier.js
@@ -97,7 +97,7 @@ function StreamCopier() {
if (!e) e = new Error(msg)
self.writable = false
end(e)
- } else writeToPipe(msg, end) // write to remaining streams
+ } else write(msg, end) // write to remaining streams
}
}
View
0  views/index.ejs → lib/views/index.ejs
File renamed without changes
View
0  views/layout.ejs → lib/views/layout.ejs
File renamed without changes
View
0  views/partials/app.ejs → lib/views/partials/app.ejs
File renamed without changes
View
2  lib/watchcopy.js
@@ -1,4 +1,6 @@
// watchcopy.js
+// Watchers that copy files
+// © Harald Rudell 2011 MIT License
// http://nodejs.org/api/path.html
var path = require('path')
View
2  lib/watchit.js
@@ -1,6 +1,6 @@
// watchit.js
// watch file modifications
-// © Harald Rudell 2012
+// © Harald Rudell 2012 MIT License
// http://nodejs.org/docs/latest/api/fs.html
var fs = require('fs')
View
7 logprocess.js
@@ -1,5 +1,5 @@
// logprocess.js
-// Log stdin to a rotatedlogger
+// Node God log process writes the filesystem log file
// © Harald Rudell 2013 MIT License
require('apprunner').initApp(require('haraldops').init({
@@ -8,10 +8,15 @@ require('apprunner').initApp(require('haraldops').init({
noInfoLog: true, // appinit quiet
logger: function () {}, // haraldops quiet
api: {
+ signals: {
+ 'SIGINT' : false,
+ 'SIGUSR2': false,
+ },
apiMap: {
fslogpipe: {
onLoad: true,
logName: process.argv[2],
+ ignoredSignals: ['SIGINT', 'SIGUSR2', 'SIGHUP'],
}
}
}
View
19 masterprocess.js
@@ -0,0 +1,19 @@
+// masterprocess.js
+// Nodegod master process supervises apps
+// © Harald Rudell 2012
+
+var appIdentifier = 'nodegodmaster'
+require('./lib/master/nodegodmaster').run({
+ port: 1113,
+ interface: '127.0.0.1',
+ ignoredSignals: ['SIGINT', 'SIGUSR2', 'SIGHUP'],
+ appIdentifier: appIdentifier,
+ spawnWeb: {
+ file: 'node',
+ args: [require('path').join(__dirname, 'webprocess')],
+ },
+ spawnLog: {
+ file: 'node',
+ args: [require('path').join(__dirname, 'logprocess'), appIdentifier],
+ },
+})
View
13 package.json
@@ -12,7 +12,7 @@
"tools",
"operations"
],
- "version": "0.2.3",
+ "version": "0.3.0",
"contributors": [
{
"name": "Harald Rudell",
@@ -24,13 +24,13 @@
"mochawrapper": ">= 0.0.34"
},
"dependencies": {
- "request": "*",
- "socket.io": "*",
- "ejsinbrowser": "*",
+ "request": ">= 2.12.1",
+ "socket.io": ">= 0.9.13",
+ "ejsinbrowser": ">= 0.0.2",
"ejs": "*",
"express": "< 3",
"greatjson": ">= 0.0.8",
- "apprunner": ">= 0.2.2",
+ "apprunner": ">= 0.2.5",
"haraldops": ">= 0.1.9",
"haraldutil": ">= 0.1.15"
},
@@ -43,7 +43,8 @@
"test": "mocha --ui exports --reporter spec",
"monitor": "mocha --ui exports --reporter min --watch",
"debugtest": "mocha --debug-brk --ui exports --reporter spec",
- "coverage": "mochacoverage"
+ "coverage": "mochacoverage",
+ "changelog": "git log --format=\"%ad %s\" --date=short"
},
"engines": {"node": ">= 0.8.0"}
}
View
33 test/test-app.js
@@ -1,29 +1,40 @@
// test-app.js
// © Harald Rudell 2013 MIT License
-var nodegodmaster = require('../lib/master/nodegodmaster')
+// https://github.com/haraldrudell/apprunner
+var apprunner = require('apprunner')
+// https://github.com/haraldrudell/haraldops
+var haraldops = require('haraldops')
// https://github.com/haraldrudell/mochawrapper
var assert = require('mochawrapper')
-var run = nodegodmaster.run
+var hi = haraldops.init
+var ia = apprunner.initApp
exports['App:'] = {
'Require': function () {
- var aRun = []
- nodegodmaster.run = function mockRun(o) {aRun.push(o)}
+ var aInit = []
+ apprunner.initApp = function mockInitApp(o) {aInit.push(o)}
+ haraldops.init = function mockInit(o) {return o}
require('../app')
- assert.equal(aRun.length, 1)
- var opts = aRun[0]
+ assert.equal(aInit.length, 1, 'app was already required')
+ var defaults = aInit[0]
+ assert.ok(defaults)
+ assert.equal(typeof defaults.appName, 'string')
+ assert.ok(defaults.api)
+ assert.ok(defaults.api.apiMap)
+ var opts = defaults.api.apiMap.masterlauncher
assert.ok(opts)
- assert.equal(typeof opts.port, 'number')
- assert.equal(typeof opts.appIdentifier, 'string')
- assert.ok(Array.isArray(opts.launchArray))
- assert.ok(Array.isArray(opts.fsLogArray))
+ assert.ok(opts.onLoad)
+ assert.ok(opts.spawn)
+ assert.equal(typeof opts.spawn.file, 'string')
+ assert.ok(Array.isArray(opts.spawn.args))
},
'after': function () {
- nodegodmaster.run = run
+ haraldops.init = hi
+ apprunner.initApp = ia
},
}
View
15 test/test-childipc.js
@@ -0,0 +1,15 @@
+// test-childipc.js
+// © Harald Rudell 2013 MIT License
+
+var childipc = require('../lib/childipc')
+
+// https://github.com/haraldrudell/mochawrapper
+var assert = require('mochawrapper')
+
+exports['Child Ipc:'] = {
+ 'Exports': function () {
+ assert.exportsTest(childipc, 1)
+ },
+ 'TODO': function () {
+ },
+}
View
15 test/test-listenermanager.js
@@ -0,0 +1,15 @@
+// test-listenermanager.js
+// © Harald Rudell 2013 MIT License
+
+var listenermanager = require('../lib/listenermanager')
+
+// https://github.com/haraldrudell/mochawrapper
+var assert = require('mochawrapper')
+
+exports['Listener Manager:'] = {
+ 'Exports': function () {
+ assert.exportsTest(listenermanager, 1)
+ },
+ 'TODO': function () {
+ },
+}
View
27 test/test-masterlauncher.js
@@ -0,0 +1,27 @@
+// test-masterlauncher.js
+// © Harald Rudell 2013 MIT License
+
+var masterlauncher
+
+// https://github.com/haraldrudell/apprunner
+var apprunner = require('apprunner')
+
+// https://github.com/haraldrudell/mochawrapper
+var assert = require('mochawrapper')
+
+ga = apprunner.getAppData
+
+exports['Master Launcher:'] = {
+ 'before': function () {
+ apprunner.getAppData = function() {return {appName: 'Node God'}}
+ masterlauncher = require('../lib/masterlauncher')
+ },
+ 'Exports': function () {
+ assert.exportsTest(masterlauncher, 1)
+ },
+ 'TODO': function () {
+ },
+ 'after': function () {
+ apprunner.getAppData = ga
+ }
+}
View
39 test/test-masterprocess.js
@@ -0,0 +1,39 @@
+// test-masterprocess.js
+// © Harald Rudell 2013 MIT License
+
+var nodegodmaster = require('../lib/master/nodegodmaster')
+
+// https://github.com/haraldrudell/apprunner
+var apprunner = require('apprunner')
+
+// https://github.com/haraldrudell/mochawrapper
+var assert = require('mochawrapper')
+
+
+var run = nodegodmaster.run
+
+exports['Master Process:'] = {
+ 'Require': function () {
+ var aRun = []
+ nodegodmaster.run = function mockRun(o) {aRun.push(o)}
+
+ require('../masterprocess')
+
+ assert.equal(aRun.length, 1)
+ var opts = aRun[0]
+ assert.ok(opts)
+ assert.equal(typeof opts.port, 'number')
+ assert.equal(typeof opts.appIdentifier, 'string')
+ var spawn = opts.spawnWeb
+ assert.ok(spawn)
+ assert.equal(typeof spawn.file, 'string')
+ assert.ok(Array.isArray(spawn.args))
+ var spawn = opts.spawnLog
+ assert.ok(spawn)
+ assert.equal(typeof spawn.file, 'string')
+ assert.ok(Array.isArray(spawn.args))
+ },
+ 'after': function () {
+ nodegodmaster.run = run
+ },
+}
View
27 test/test-nodegodmaster.js
@@ -17,7 +17,7 @@ var sru = processfinder.setResetUi
var ipm = processfinder.isProcessMaster
var lui = uimanager.launchUi
var gur = uimanager.getUiRelauncher
-var lg = logpipe.getLogPipe
+var lg = logpipe.LogPipe
exports['Node God Master:'] = {
'Exports': function () {
@@ -29,16 +29,18 @@ exports['Node God Master:'] = {
interface: '127.0.0.1',
ignoredSignals: ['SIGINT', 'SIGUSR2', 'SIGHUP'],
appIndentifier: 'nodegodmaster',
- launchArray: ['node', require('path').join(__dirname, '..', 'webprocess')],
- fsLogArray: ['node', require('path').join(__dirname, '..', 'logprocess')],
+ spawnWeb: {file: 'node', args: [require('path').join(__dirname, '..', 'webprocess')]},
+ spawnLog: {file: 'node', args: [require('path').join(__dirname, '..', 'logprocess')]},
}
- var mockLogManager = {
- log: function () {},
- pLog: function () {},
- connectPipe: function (cb) {cb()},
- write: function () {},
+
+ logpipe.LogPipe = MockLogPipe
+ function MockLogPipe() {
+ this.log = function () {}
+ this.pLog = function () {}
+ this.connectPipe = function (cb) {cb()}
+ this.write = function () {}
+ this.end = function () {}
}
- logpipe.LogPipe = function mockLogPipe() {return mockLogManager}
// execute is not master
var aOn = {}
@@ -85,8 +87,8 @@ exports['Node God Master:'] = {
var aLaunchUi = []
var eLaunchUi = [{
- launchArray: ['node', path.join(__dirname, '..', 'webprocess')],
- rlog: mockLogManager,
+ spawn: opts.spawnWeb,
+ rlog: 'x',
launchTime: 0
}]
uimanager.launchUi = function (opts) {aLaunchUi.push(opts)}
@@ -95,6 +97,7 @@ exports['Node God Master:'] = {
assert.deepEqual(aReset, eReset)
assert.equal(typeof(eLaunchUi[0].launchTime = aLaunchUi[0] && aLaunchUi[0].launchTime), 'number')
+ assert.ok((eLaunchUi[0].rlog = aLaunchUi[0] && aLaunchUi[0].rlog) instanceof MockLogPipe)
assert.deepEqual(aLaunchUi, eLaunchUi)
},
'after': function() {
@@ -103,6 +106,6 @@ exports['Node God Master:'] = {
processfinder.setResetUi = sru
uimanager.launchUi = lui
uimanager.getUiRelauncher = gur
- logpipe.getLogPipe = lg
+ logpipe.LogPipe = lg
},
}
View
15 test/test-parentipc.js
@@ -0,0 +1,15 @@
+// test-parentipc.js
+// © Harald Rudell 2013 MIT License
+
+var parentipc = require('../lib/parentipc')
+
+// https://github.com/haraldrudell/mochawrapper
+var assert = require('mochawrapper')
+
+exports['Parent Ipc:'] = {
+ 'Exports': function () {
+ assert.exportsTest(parentipc, 1)
+ },
+ 'TODO': function () {
+ },
+}
View
15 test/test-streamcopier.js
@@ -0,0 +1,15 @@
+// test-streamcopier.js
+// © Harald Rudell 2013 MIT License
+
+var streamcopier = require('../lib/streamcopier')
+
+// https://github.com/haraldrudell/mochawrapper
+var assert = require('mochawrapper')
+
+exports['Stream Copier:'] = {
+ 'Exports': function () {
+ assert.exportsTest(streamcopier, 1)
+ },
+ 'TODO': function () {
+ },
+}
View
30 test/test-uimanager.js
@@ -28,18 +28,24 @@ exports['UiManager:'] = {
assert.equal(typeof actual, 'function')
},
'LaunchUi': function(done) {
- var launchTime = 7
- var mockLogger = {
- pLog: function () {},
- configure: function () {},
- write: function () {},
+ var opts = {
+ spawn: {
+ file: 'NODE',
+ args: ['SCRIPT']
+ },
+ rlog: {
+ pLog: function () {},
+ configure: function () {},
+ write: function () {},
+ },
+ launchTime: 7,
+ restartIntercept: restartIntercept,
}
- var launchArray = ['EXECUTABLE', 'ARGUMENT']
var aLaunchData = []
var eLaunchData = [{
- masterLaunch: launchTime,
- rlog: mockLogger,
+ masterLaunch: opts.launchTime,
+ rlog: opts.rlog,
}]
appconduit.setLaunchData = function (o) {aLaunchData.push(o)}
@@ -51,8 +57,8 @@ exports['UiManager:'] = {
var aSpawn = []
var eSpawn = [[
- launchArray[0],
- launchArray.slice(1),
+ opts.spawn.file,
+ opts.spawn.args,
{detached: true, stdio: ['ignore', 'pipe', 'pipe', 'ipc']},
]]
child_process.spawn = function mockSpawn(c, a, o) {aSpawn.push([c, a, o]); return child}
@@ -65,9 +71,9 @@ exports['UiManager:'] = {
appconduit.uiConnect = function (f) {aUiConnect.push(f)}
var aException = []
- var restartIntercept = function (e) {aException.push(e)}
+ function restartIntercept(e) {aException.push(e)}
- uimanager.launchUi({launchArray: launchArray, restartIntercept: restartIntercept, rlog: mockLogger, launchTime: launchTime})
+ uimanager.launchUi(opts)
assert.deepEqual(aException, [], 'launchUi exceptions')
assert.deepEqual(aLaunchData, eLaunchData)
View
2  test/test-webprocess.js
@@ -1,7 +1,9 @@
// test-webprocess.js
// © Harald Rudell 2013 MIT License
+// https://github.com/haraldrudell/apprunner
var apprunner = require('apprunner')
+https://github.com/haraldrudell/haraldops
var haraldops = require('haraldops')
// https://github.com/haraldrudell/mochawrapper
View
2  webprocess.js
@@ -1,5 +1,5 @@
// webprocess.js
-// Web server process for Node God
+// Node God Web process provides Web interface
// © Harald Rudell 2012
require('apprunner').initApp(require('haraldops').init({
Please sign in to comment.
Something went wrong with that request. Please try again.