-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.coffee
61 lines (48 loc) · 1.55 KB
/
index.coffee
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
# setup extend YAML `require` parser
require './lib/util/yaml-extend'
{fork} = require 'child_process'
logger = require('./lib/util/logger')()
class Master
SIG: ["SIGTERM", "SIGINT", "SIGABRT", "SIGHUP"]
constructor: () ->
@exec_path = "./lib/agent/sync.coffee"
@_closing = no
@agent = null
fork: ->
logger.info "[master]", "process start"
master = new Master()
master.init()
master.spawnAgent()
init: ->
for sig in @SIG
logger.info "[master]", "registry signal event:", sig
process.on sig, @signalHandle sig
signalHandle: (signal) =>
=>
logger.warn "[master]", "got signal:", signal
@_closing = yes
@agent?.kill "SIGTERM"
spawnAgent: ->
logger.info "[master]", "spawning agent process"
@agent = fork @exec_path, process.argv[2..]
@initAgent()
initAgent: ->
@agent?.on 'exit', @onAgentExit
.on 'error', @onAgentError
.on 'disconnect', @onAgentDisconnect
uninitAgent: ->
@agent?.removeListener 'exit', @onAgentExit
.removeListener 'error', @onAgentError
.removeListener 'disconnect', @onAgentDisconnect
onAgentExit: (code, signal) =>
logger.warn "[master] agent exit with code [#{code}], due to signal [#{signal}]"
@uninitAgent()
if @_closing
logger.warn "[master]", "process exit"
process.exit 0
else
logger.info "respawning..."
@spawnAgent()
onAgentError: (err) => logger.error err
onAgentDisconnect: => logger.warn '[master]', 'agent disconnect'
module.exports = Master