Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added softKiller and hardKiller stop methods

  • Loading branch information...
commit 9fc0679f45c5e1c1c012dd17fd11af46acef2247 1 parent 2572508
Ian Babrou authored

Showing 1 changed file with 35 additions and 15 deletions. Show diff stats Hide diff stats

  1. +35 15 init.coffee
50 init.coffee
@@ -68,32 +68,52 @@ exports.stopped = (killed) ->
68 68 console.log 'Not running.'
69 69 process.exit 0
70 70
71   -exports.stop = (pidfile, cb = exports.stopped) ->
  71 +exports.hardKiller = (timeout = 2000) ->
  72 + (pid, cb) ->
  73 + signals = ['TERM', 'INT', 'QUIT', 'KILL']
  74 + tryKill = ->
  75 + sig = "SIG#{ signals[0] }"
  76 + try
  77 + # throws when the process no longer exists
  78 + process.kill pid, sig
  79 + signals.shift() if signals.length > 1
  80 + setTimeout (-> tryKill sig), timeout
  81 + catch e
  82 + cb(signals.length < 4)
  83 + tryKill()
  84 +
  85 +exports.softKiller = (timeout = 2000) ->
  86 + (pid, cb) ->
  87 + first = true
  88 + tryKill = ->
  89 + try
  90 + # throws when the process no longer exists
  91 + process.kill pid, "SIGTERM"
  92 + console.log "Waiting for pid " + pid
  93 + first = false
  94 + setTimeout tryKill, timeout
  95 + catch e
  96 + cb(!first)
  97 + tryKill()
  98 +
  99 +exports.stop = (pidfile, cb = exports.stopped, killer = hardKiller) ->
72 100 exports.status pidfile, ({pid}) ->
73 101 if pid
74   - signals = ['TERM', 'INT', 'QUIT', 'KILL']
75   - tryKill = ->
76   - sig = "SIG#{ signals[0] }"
77   - try
78   - # throws when the process no longer exists
79   - process.kill pid, sig
80   - signals.shift() if signals.length > 1
81   - setTimeout (-> tryKill sig), 2000
82   - catch e
83   - fs.unlink pidfile, -> cb(signals.length < 4)
84   - tryKill()
  102 + killer pid, (killed) ->
  103 + fs.unlink pidfile, -> cb(killed)
85 104 else
86 105 cb false
87 106
88   -exports.simple = ({pidfile, logfile, command, run}) ->
  107 +exports.simple = ({pidfile, logfile, command, run, killer}) ->
89 108 command or= process.argv[2]
  109 + killer or= null
90 110 start = -> exports.start { pidfile, logfile, run }
91 111 switch command
92 112 when 'start' then start()
93   - when 'stop' then exports.stop pidfile
  113 + when 'stop' then exports.stop pidfile, null, killer
94 114 when 'status' then exports.status pidfile
95 115 when 'restart', 'force-reload'
96   - exports.stop pidfile, start
  116 + exports.stop pidfile, start, killer
97 117 when 'try-restart'
98 118 exports.stop pidfile, (killed) ->
99 119 if killed

0 comments on commit 9fc0679

Please sign in to comment.
Something went wrong with that request. Please try again.