stopper.js 1005 B

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