Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (115 sloc) 3.896 kb
ce5b237 Paul Driver Initial commit
authored
1 fs = require 'fs'
2 daemon = require 'daemon'
3
4 exports.printStatus = (st) ->
5 if st.pid
6 console.log 'Process running with pid %d.', st.pid
7 process.exit 0
8
9 else if st.exists
10 console.log 'Pidfile exists, but process is dead.'
11 process.exit 1
12 else
13 console.log 'Not running.'
14 process.exit 3
15
16 exports.status = (pidfile, cb = exports.printStatus) ->
17 fs.readFile pidfile, 'utf8', (err, data) ->
18 if err
19 cb exists: err.code isnt 'ENOENT'
20 else if match = /^\d+/.exec(data)
21 pid = parseInt match[0]
22 try
23 process.kill pid, 0
24 cb pid: pid
25 catch e
26 cb exists: true
27 else
28 cb exists: true
29
30 exports.startSucceeded = (pid) ->
31 if pid
32 console.log 'Process already running with pid %d.', pid
33 else
34 console.log 'Started.'
35
36 exports.startFailed = (err) ->
37 console.log err
38 process.exit 1
39
40 # This can fail if the pidfile becomes unwriteable after status() but before
41 # daemon.lock(). It's a race condition, but it's a hard one to fix.
42 exports.start = ({ pidfile, logfile, run, success, failure }) ->
43 success or= exports.startSucceeded
44 failure or= exports.startFailed
45 logfile or= '/dev/null'
46
47 start = (err) ->
48 return failure(err) if err
49 fs.open logfile, 'a+', 0666, (err, fd) ->
50 return failure(err) if err
51 success()
52 pid = daemon.start(fd)
53 daemon.lock(pidfile)
54 run()
55
56 exports.status pidfile, (st) ->
57 if st.pid
58 success st.pid, true
59 else if st.exists
60 fs.unlink pidfile, start
61 else
62 start()
63
64 exports.stopped = (killed) ->
65 if killed
66 console.log 'Stopped.'
67 else
68 console.log 'Not running.'
69 process.exit 0
70
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
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) ->
739cf53 Ian Babrou only one SIGTERM in softKiller
bobrik authored
87 sig = "SIGTERM"
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
88 tryKill = ->
89 try
90 # throws when the process no longer exists
739cf53 Ian Babrou only one SIGTERM in softKiller
bobrik authored
91 process.kill pid, sig
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
92 console.log "Waiting for pid " + pid
739cf53 Ian Babrou only one SIGTERM in softKiller
bobrik authored
93 sig = 0 if sig != 0
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
94 first = false
95 setTimeout tryKill, timeout
96 catch e
739cf53 Ian Babrou only one SIGTERM in softKiller
bobrik authored
97 cb(sig == 0)
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
98 tryKill()
99
c9daa5b Ian Babrou fixed typo
bobrik authored
100 exports.stop = (pidfile, cb = exports.stopped, killer = exports.hardKiller(2000)) ->
ce5b237 Paul Driver Initial commit
authored
101 exports.status pidfile, ({pid}) ->
102 if pid
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
103 killer pid, (killed) ->
104 fs.unlink pidfile, -> cb(killed)
ce5b237 Paul Driver Initial commit
authored
105 else
106 cb false
107
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
108 exports.simple = ({pidfile, logfile, command, run, killer}) ->
ce5b237 Paul Driver Initial commit
authored
109 command or= process.argv[2]
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
110 killer or= null
ce5b237 Paul Driver Initial commit
authored
111 start = -> exports.start { pidfile, logfile, run }
112 switch command
113 when 'start' then start()
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
114 when 'stop' then exports.stop pidfile, null, killer
ce5b237 Paul Driver Initial commit
authored
115 when 'status' then exports.status pidfile
116 when 'restart', 'force-reload'
9fc0679 Ian Babrou added softKiller and hardKiller stop methods
bobrik authored
117 exports.stop pidfile, start, killer
ce5b237 Paul Driver Initial commit
authored
118 when 'try-restart'
119 exports.stop pidfile, (killed) ->
120 if killed
121 exports.start { pidfile, logfile, run }
122 else
123 console.log 'Not running.'
124 process.exit 1
125 else
126 console.log 'Command must be one of: ' +
127 'start|stop|status|restart|force-reload|try-restart'
128 process.exit 1
Something went wrong with that request. Please try again.