Skip to content

Commit

Permalink
feat(stopper): Enable programically detached server
Browse files Browse the repository at this point in the history
When setting `detached=true` in config, the server will start
detached.
  • Loading branch information
budde377 committed Feb 21, 2016
1 parent 3d4fa00 commit f10fd81
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
18 changes: 1 addition & 17 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var path = require('path')
var optimist = require('optimist')
var fs = require('graceful-fs')
var spawn = require('child_process').spawn

var Server = require('./server')
var helper = require('./helper')
Expand Down Expand Up @@ -209,21 +208,6 @@ var describeCompletion = function () {
.describe('help', 'Print usage.')
}

var startServer = function (config) {
var args = process.argv
var detachedIndex = args.indexOf('--detached')
if (detachedIndex === -1) {
new Server(config).start()
return
}
args.splice(detachedIndex, 1)
var child = spawn(args[0], args.slice(1), {
detached: true,
stdio: ['ignore', 'ignore', 'ignore']
})
child.unref()
}

exports.process = function () {
var argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2)))
var options = {
Expand Down Expand Up @@ -272,7 +256,7 @@ exports.run = function () {

switch (config.cmd) {
case 'start':
startServer(config)
new Server(config).start()
break
case 'run':
require('./runner').run(config)
Expand Down
9 changes: 9 additions & 0 deletions lib/detached.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fs = require('fs')

var Server = require('./server')
var configurationFile = process.argv[2]
var fileContents = fs.readFileSync(configurationFile, 'utf-8')
fs.unlink(configurationFile, function () {})
var data = JSON.parse(fileContents)
var server = new Server(data)
server.start(data)
2 changes: 1 addition & 1 deletion lib/middleware/stopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var createStopperMiddleware = function (urlRoot) {
response.writeHead(200)
log.info('Stopping server')
response.end('OK')
process.exit(0)
process.kill(process.pid, 'SIGINT')
}
}

Expand Down
30 changes: 29 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var SocketIO = require('socket.io')
var di = require('di')
var util = require('util')
var Promise = require('bluebird')

var spawn = require('child_process').spawn
var tmp = require('tmp')
var fs = require('fs')
var path = require('path')
var root = global || window || this

var cfg = require('./config')
Expand Down Expand Up @@ -131,6 +134,10 @@ Server.prototype.refreshFiles = function () {
Server.prototype._start = function (config, launcher, preprocess, fileList, webServer,
capturedBrowsers, socketServer, executor, done) {
var self = this
if (config.detached) {
this._detach(config, done)
return
}

self._fileList = fileList

Expand Down Expand Up @@ -362,6 +369,27 @@ Server.prototype._start = function (config, launcher, preprocess, fileList, webS
})
}

Server.prototype._detach = function (config, done) {
var log = this.log
var tmpFile = tmp.fileSync({keep: true})
log.info('Starting karma detached')
log.info('Run "karma stop" to stop the server.')
log.debug('Writing config to tmp-file %s', tmpFile.name)
config.detached = false
try {
fs.writeFileSync(tmpFile.name, JSON.stringify(config), 'utf8')
} catch (e) {
log.error("Couldn't write temporary configuration file")
done(1)
return
}
var child = spawn(process.argv[0], [path.resolve(__dirname, '../lib/detached.js'), tmpFile.name], {
detached: true,
stdio: 'ignore'
})
child.unref()
}

// Export
// ------

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
"rimraf": "^2.3.3",
"socket.io": "^1.4.5",
"source-map": "^0.5.3",
"tmp": "0.0.28",
"useragent": "^2.1.6"
},
"devDependencies": {
Expand Down

0 comments on commit f10fd81

Please sign in to comment.