This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathprocesses.coffee
More file actions
executable file
·203 lines (171 loc) · 5.9 KB
/
Copy pathprocesses.coffee
File metadata and controls
executable file
·203 lines (171 loc) · 5.9 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# this is a work in progress.
# it's doing proper cleanup and management of child processes
# use it with care, not all edge cases are covered.
#
# author @devrim
traverse = require 'traverse'
_ = require 'underscore'
fs = require "fs"
hat = require 'hat'
{exec,spawn,fork} = require "child_process"
{EventEmitter} = require "events"
try
fs.mkdirSync __dirname+"/.tmp/"
catch e
# do nothing, probably dir is already there.
# guaranteed death.
process.on 'SIGTERM', ->
console.log "going down"
process.exit(0)
class Processes extends EventEmitter
constructor:(options={})->
{main} = options
@list = {}
@childProcessesContainerDir = __dirname+"/.tmp"
if main
previouschildProcessesContainerDir = @childProcessesContainerDir+"."+Date.now()
fs.renameSync @childProcessesContainerDir, previouschildProcessesContainerDir
fs.mkdirSync @childProcessesContainerDir
exec "rm -rf #{previouschildProcessesContainerDir}",(err,stdout,stderr)->
if err
console.log "couldn't delete previous process tree at #{previouschildProcessesContainerDir}"
#else
# console.log "previous process tree is deleted."
fork: (options,callback) ->
options.fork = yes
@run options,callback
spawn: (options,callback)->
options.spawn = yes
@run options,callback
exec:(cmd,callback)->
@spawn
name : Date.now()
cmd : cmd
restart : no
stdout : process.stdout
stderr : process.stderr
onExit : callback
run : (options,callback) ->
{name,monitor,onMessage,modulePath,args,opts,restart,restartInterval,cmd,log,onExit,stdout,stderr,verbose,die} = options
onExit ?= ->
name ?= Date.now()
restartInterval ?= 1000
verbose ?= no
if "string" is typeof options.cmd
cmdA = cmd.split(" ")
else
console.log e = "[PROCESSES][ERROR][#{name}] Can't start without a valid cmd param." # if verbose
return callback e
if options.fork
if options.pool
opts = traverse(options).clone()
delete opts.pool
@run opts for i in [0..options.pool.size]
return
else
cp = fork cmdA[0],cmdA[1...],opts
else
cp = spawn cmdA[0],cmdA[1...]
cp.stdout.pipe stdout if stdout
cp.stderr.pipe stderr if stderr
console.log "[PROCESSES][#{name}]Started the process: #{name} with #{cp.pid}" if verbose
@createPidFile cp.pid,name
childObj =
name : name
process : cp
pid : cp.pid
verbose : verbose
if cmd
childObj.cmd = cmd
else if modulePath
childObj.fork = {modulePath,args,options}
if @list[name]
d = Date.now()
console.log "[WARN][Processes] Process name '#{name}' not unique. to be able to kill it properly it is now: #{name}-#{d}" if verbose
name += "-"+d
@list[name] = childObj
if die?.after
console.log "[PROCESSES][#{name}]i will kill this process:#{name} every #{die.after}msecs" if verbose
setTimeout =>
console.log "[PROCESSES][#{name}]it's time you die process:#{name}" if verbose
@kill name
,die.after
cp.on 'exit',=>
console.log "[PROCESSES][#{name}] did exit." if verbose
@emit "exit",cp.pid,name
delete @list[name]
cp.on 'close', () =>
console.log "[PROCESSES][#{name}] did close." if verbose
@emit "close",cp.pid,name
onExit cp.pid,name
if restart is yes
restartInterval = 1 unless restartInterval
setTimeout =>
@run options
,restartInterval
console.log "[PROCESSES][#{name}] Restarting the process in #{restartInterval} msecs." if verbose
cp.on 'message',(msg)->
onMessage msg if "function" is typeof onMessage
return childObj
# cp.on 'message',(msg)=>
# @emit 'message',childObj,msg
# onMessage msg if onMessage
createPidFile : (pid,name)->
fs.writeFileSync "#{@childProcessesContainerDir}/#{process.pid}.#{pid}",""
get : (name)->
if @list[name]
return @list[name].process
else
console.log "[ERROR][Processes] #{name} Can't be found."
fetchChildren : (parentPid,callback)->
findChildren = (arr, pid) ->
findChildrenRec = (pid,root=yes)->
c={}
pid = ""+pid
i = -1
arr.forEach (item)->
[parent,child] = item.split(".")
if parent is pid
c[child] = findChildrenRec child,no
return c
arr = traverse(findChildrenRec pid).paths()
arr.forEach (group) -> group.reverse()
arr = _.sortBy arr,(a)-> a.length*-1
return _.uniq _.flatten arr,yes
fs.readdir @childProcessesContainerDir,(err,list)->
if err
callback err
else
callback null,findChildren list,parentPid
killAllChildren : (parentPid, callback) ->
@fetchChildren parentPid,(err,orderedKillList)->
console.log "ordered kill list:"+orderedKillList
unless err
pidPair = []
orderedKillList.forEach (pid)->
console.log "killing children: #{pid}"
try
pidPair.push pid
process.kill(pid*1)
if pidPair.length is 2
pidPair.reverse()
deleteFile = pidPair.join(".")
console.log "deleting"+deleteFile
fs.unlinkSync deleteFile
pidPair.shift()
catch e
console.log "child process #{pid} did not exist. not killed by me."
callback null
sendMessage : (name,msg)->
if @list[name]?.process?.send
@list[name].process.send msg
kill :(name,callback=->)->
unless @list[name]
console.log "[PROCESSES][#{name}] cant kill #{name}, couldn't find it."
else
pid = @list[name].pid
console.log "[PROCESSES][#{name}] killing parent: #{pid}"
@killAllChildren pid,(err,res)->
console.log "[PROCESSES][#{name}] killing #{name} #{pid}"
process.kill pid
module.exports = Processes