stopper.js
1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var http = require('http')
var cfg = require('./config')
var logger = require('./logger')
var helper = require('./helper')
exports.stop = function (config, done) {
config = config || {}
logger.setupFromConfig(config)
done = helper.isFunction(done) ? done : process.exit
var log = logger.create('stopper')
config = cfg.parseConfig(config.configFile, config)
var options = {
hostname: config.hostname,
path: config.urlRoot + 'stop',
port: config.port,
method: 'GET'
}
var request = http.request(options)
request.on('response', function (response) {
if (response.statusCode !== 200) {
log.error('Server returned status code: ' + response.statusCode)
done(1)
return
}
log.info('Server stopped.')
done(0)
})
request.on('error', function (e) {
if (e.code === 'ECONNREFUSED') {
log.error('There is no server listening on port %d', options.port)
done(1, e.code)
} else {
throw e
}
})
request.end()
}