Skip to content

Commit

Permalink
Rename option attach to detached
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jul 31, 2012
1 parent 8874c21 commit 7762b3d
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 35 deletions.
28 changes: 28 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@
List of API changes and enhancements
====================================

Version 0.2.9
-------------

Rename `attach` option to `detached` in start_stop

Version 0.2.8
-------------

Broader Node.js requirement to 0.6

Version 0.2.7
-------------

Fix Win7 completion (by [t101jv](https://github.com/t101jv))
Fix compatibility with Node.js version 0.8

Version 0.2.6
-------------

Reflect latest CoffeeScript strict mode

Version 0.2.5
-------------

Add cmd and path options to http plugin
Stylus plugin
StartStop script improvement

Version 0.2.4
-------------

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/cloud9.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Options:
- `ip` , IP address where Cloud9 will serve from. Defaults to `"127.0.0.1"`.
- `port` , Port number where Cloud9 will serve from. Defaults to `3000`.
- `workspace`, Path to the workspace that will be loaded in Cloud9, Defaults to `Shell.set('workspace')`.
- `attach` , Wether the Cloud9 process should be attached to the current process. If not defined, default to `false` (the server run as a daemon).
- `detached` , Wether the Cloud9 process should be attached to the current process. If not defined, default to `false` (the server doesn't run as a daemon).
- `pidfile` , Path to the file storing the daemon process id. Defaults to `"/.node_shell/#{md5}.pid"`
- `stdout` , Writable stream or file path to redirect cloud9 stdout.
- `stderr` , Writable stream or file path to redirect cloud9 stderr.
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/coffee.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Options:
- `output` , Directory where compiled JavaScript files are written. Used in conjunction with "compile".
- `lint` , If the `jsl` (JavaScript Lint) command is installed, use it to check the compilation of a CoffeeScript file.
- `require` , Load a library before compiling or executing your script. Can be used to hook in to the compiler (to add Growl notifications, for example).
- `attach` , Wether the Coffee process should be attached to the current process. If not defined, default to `false` (the server run as a daemon).
- `detached` , Wether the Coffee process should be attached to the current process. If not defined, default to `false` (the server doesn't run as a daemon).
- `pidfile` , Path to the file storing the daemon process id. Defaults to `"/.node_shell/#{md5}.pid"`
- `stdout` , Writable stream or file path to redirect cloud9 stdout.
- `stderr` , Writable stream or file path to redirect cloud9 stderr.
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/http.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The following properties may be provided as settings:
Properties derived from the start_stop utility:
- `attach` Wether the HTTP process should be attached to the current process. If not defined, default to `false` (the server run as a daemon).
- `detached` Wether the HTTP process should be attached to the current process. If not defined, default to `false` (the server doesn't run as a daemon).
- `pidfile` Path to the file storing the daemon process id. Defaults to `"/.node_shell/#{md5}.pid"`
- `stdout` Writable stream or file path to redirect the server stdout.
- `stderr` Writable stream or file path to redirect the server stderr.
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/redis.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Redis Plugin
Register two commands, `redis start` and `redis stop`. The following properties may be provided as settings:
- `config` , Path to the configuration file. Required to launch redis.
- `attach` , Wether the Redis process should be attached to the current process. If not defined, default to `false` (the server run as a daemon).
- `detached` , Wether the Redis process should be attached to the current process. If not defined, default to `false` (the server doesn't run as a daemon).
- `pidfile` , Path to the file storing the daemon process id. Defaults to `"/.node_shell/#{md5}.pid"`
- `stdout` , Writable stream or file path to redirect cloud9 stdout.
- `stderr` , Writable stream or file path to redirect cloud9 stderr.
Expand Down
35 changes: 24 additions & 11 deletions lib/start_stop.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

crypto = require 'crypto'
{exec} = require 'child_process'
{exec, spawn} = require 'child_process'
fs = require 'fs'
path = require 'path'
exists = fs.exists or path.exists
Expand All @@ -27,7 +27,7 @@ module.exports = start_stop =
`options` , Object with the following properties:
* `cmd` , Command to run
* `attach` , Attach the child process to the current process
* `detached` , Detached the child process from the current process
* `pidfile` , Path to the file storing the child pid
* `stdout` , Path to the file where standard output is redirected
* `stderr` , Path to the file where standard error is redirected
Expand All @@ -40,7 +40,10 @@ module.exports = start_stop =
###
start: (options, callback) ->
unless options.attach
if options.attach?
console.log 'Option attach was renamed to attached to be consistent with the new spawn option'
options.detached = not options.attach
if options.detached
cmdStdout =
if typeof options.stdout is 'string'
then options.stdout else '/dev/null'
Expand Down Expand Up @@ -99,7 +102,7 @@ module.exports = start_stop =
not provided, can be guessed from the `cmd` option used to start the process.
`options` , Object with the following properties:
* `attach` , Attach the child process to the current process
* `detached` , Detach the child process to the current process
* `cmd` , Command used to run the process, in case no pidfile is provided
* `pid` , Pid to kill in attach mode
* `pidfile` , Path to the file storing the child pid
Expand All @@ -112,8 +115,12 @@ module.exports = start_stop =
###
stop: (options, callback) ->
if options.attach?
console.log 'Option attach was renamed to attached to be consistent with the new spawn option'
options.detached = not options.attach
# Stoping a provided PID
if typeof options is 'string' or typeof options is 'number'
options = {pid: parseInt(options, 10), attach: true}
options = {pid: parseInt(options, 10), detached: false}
kill = (pid, callback) ->
# Not trully recursive, potential scripts:
# http://machine-cycle.blogspot.com/2009/05/recursive-kill-kill-process-tree.html
Expand All @@ -127,7 +134,7 @@ module.exports = start_stop =
"""
exec cmds, (err, stdout, stderr) ->
callback err
unless options.attach
if options.detached
start_stop.pid options, (err, pid) ->
return callback err if err
return callback null, false unless pid
Expand Down Expand Up @@ -155,9 +162,9 @@ module.exports = start_stop =
otherwise it is set to false.
`options` , Object with the following properties:
* `attach` , True if the child process is attached to the current process
* `detached` , True if the child process is not attached to the current process
* `cmd` , Command used to run the process, in case no pidfile is provided
* `pid` , Pid to kill in attach mode
* `pid` , Pid to kill if not running in detached mode
* `pidfile` , Path to the file storing the child pid
Expand All @@ -168,8 +175,11 @@ module.exports = start_stop =
###
pid: (options, callback) ->
if options.attach?
console.log 'Option attach was renamed to attached to be consistent with the new spawn option'
options.detached = not options.attach
# Attach mode
if options.attach
unless options.detached
return new Error 'Expect a pid property in attached mode' unless options.pid?
return callback null, options.pid
# Deamon mode
Expand All @@ -190,7 +200,7 @@ module.exports = start_stop =
argument with a pidfile property unless already present.
`options` , Object with the following properties:
* `attach` , True if the child process is attached to the current process
* `detached` , True if the child process is not attached to the current process
* `cmd` , Command used to run the process, in case no pidfile is provided
* `pid` , Pid to kill in attach mode
* `pidfile` , Path to the file storing the child pid
Expand All @@ -202,7 +212,10 @@ module.exports = start_stop =
###
file: (options, callback) ->
return callback null, null, false if options.attach
if options.attach?
console.log 'Option attach was renamed to detached to be consistent with the spawn API'
options.detached = not options.attach
return callback null, null, false unless options.detached
start = ->
return pidFileExists() if options.pidfile
dir = path.resolve process.env['HOME'], '.node_shell'
Expand Down
2 changes: 1 addition & 1 deletion samples/cloud9/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
stdout: __dirname+'/cloud9.out.log',
stderr: __dirname+'/cloud9.err.log',
pidfile: __dirname+'/cloud9.pid',
attach: false
detached: true
}));
app.use(shell.router({
shell: app
Expand Down
2 changes: 1 addition & 1 deletion samples/coffee/sample.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
stdout: __dirname + '/coffee.out.log'
stderr: __dirname + '/coffee.err.log'
pidfile: __dirname + '/coffee.pid'
attach: false
detached: true
app.use shell.help
shell: app
introduction: true
Expand Down
2 changes: 1 addition & 1 deletion samples/http/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
stdout: __dirname + '/logs/http.out.log',
stderr: __dirname + '/logs/http.err.log',
pidfile: __dirname + '/tmp/http.pid',
attach: false
detached: true
}));
app.use(shell.router({shell: app}));
app.use(shell.help({shell: app, introduction: true}));
Expand Down
3 changes: 2 additions & 1 deletion samples/redis/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ app.configure(function() {
}));
app.use(shell.redis({
config: 'redis.conf',
pidfile: 'redis.pid'
pidfile: 'redis.pid',
detached: true
}));
app.use(shell.router({
shell: app
Expand Down
4 changes: 2 additions & 2 deletions test/plugin_http.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ http = require 'http'
shell = require '..'

describe 'Plugin HTTP', ->
it 'should start and stop an HTTP server', (next) ->
it 'should start and stop an HTTP server in attach mode', (next) ->
app = shell
workspace: "#{__dirname}/plugin_http"
command: null
stdin: new shell.NullStream
stdout: new shell.NullStream
app.configure ->
app.use shell.http attach: true
app.use shell.http detached: false
app.use shell.router shell: app
app.run 'http start'
setTimeout ->
Expand Down
30 changes: 17 additions & 13 deletions test/start_stop.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ should = require 'should'
start_stop = require '../lib/start_stop'

describe 'StartStop', ->
it 'Test daemon # start and stop', (next) ->
it 'should detach a child, start and stop', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
# Start the process
start_stop.start cmd: cmd, (err, pid) ->
start_stop.start cmd: cmd, detached: true, (err, pid) ->
should.not.exist err
pid.should.be.a 'number'
# Check if process started
start_stop.running pid, (err, running) ->
should.not.exist err
running.should.be.true
# Stop process
start_stop.stop cmd: cmd, (err) ->
start_stop.stop cmd: cmd, detached: true, (err) ->
should.not.exist err
# Check if process stoped
start_stop.running pid, (err, running) ->
should.not.exist err
running.should.be.false
next()
it 'Test daemon # stop inactive process', (next) ->
it 'should detach a child and stop inactive process', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
# Stop process
start_stop.stop cmd:cmd, (err, stoped) ->
start_stop.stop cmd:cmd, detached: true, (err, stoped) ->
should.not.exist err
stoped.should.be.false
next()
it 'Test daemon # stop inactive process with pidfile # relax', (next) ->
it 'should detach a child and stop inactive process with pidfile', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
pidfile = "#{__dirname}/start_stop/pidfile"
fs.writeFile pidfile, "1234567", (err) ->
Expand All @@ -40,14 +40,18 @@ describe 'StartStop', ->
should.not.exist err
running.should.be.false
# Stop process
start_stop.stop {cmd:cmd, pidfile: pidfile}, (err, stoped) ->
start_stop.stop cmd:cmd, pidfile: pidfile, detached: true, (err, stoped) ->
should.not.exist err
stoped.should.be.false
# Pidfile shall be removed even if pid is invalid
exists pidfile, (running) ->
running.should.be.false
next()
it 'Test daemon # stop inactive process with pidfile # strict', (next) ->
it 'should detach a child and honor the strict option', (next) ->
# From the API
# Send an error when a pid file exists and reference an unrunning pid.
# Exist both in the start and stop functions
# todo: test in start function
cmd = "node #{__dirname}/start_stop/server.js"
pidfile = "#{__dirname}/start_stop/pidfile"
fs.writeFile pidfile, "1234567", (err) ->
Expand All @@ -56,23 +60,23 @@ describe 'StartStop', ->
should.not.exist err
running.should.be.false
# Stop process
start_stop.stop {cmd:cmd, pidfile: pidfile, strict: true}, (err, stoped) ->
start_stop.stop cmd:cmd, pidfile: pidfile, strict: true, detached: true, (err, stoped) ->
err.should.be.an.instanceof Error
# Pidfile shall be removed even if pid is invalid
exists pidfile, (running) ->
running.should.be.false
next()
it 'should throw an error if pidfile is not in a existing directory', (next) ->
it 'should detach a child and throw an error if pidfile not in directory', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
pidfile = "#{__dirname}/doesnotexist/pidfile"
start_stop.start cmd:cmd, pidfile: pidfile, (err, stoped) ->
start_stop.start cmd:cmd, pidfile: pidfile, detached: true, (err, stoped) ->
err.should.be.an.instanceof Error
err.message.should.eql 'Pid directory does not exist'
next()
it 'Test attach', (next) ->
it 'should attach a child', (next) ->
cmd = "node #{__dirname}/start_stop/server.js"
# Start the process
start_stop.start {cmd: cmd, attach: true}, (err, pid) ->
start_stop.start cmd: cmd, detached: false, (err, pid) ->
should.not.exist err
pid.should.be.a 'number'
# Check if process started
Expand Down
2 changes: 1 addition & 1 deletion test/start_stop/test_attach.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ start_stop = require '../../lib/start_stop'

start_stop.start
cmd: "#{__dirname}/server.js"
attach: true
detached: false
, (err, pid) ->
# Keep the process active

0 comments on commit 7762b3d

Please sign in to comment.