-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
69 lines (57 loc) · 1.62 KB
/
app.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
62
63
64
65
66
67
68
69
require 'coffee-script'
express = require 'express'
childProcess = require 'child_process'
jsonfile = require 'jsonfile'
config = jsonfile.readFileSync 'config.json'
redis =
pub: require('./redis')()
sub: require('./redis')()
if process.env.PORT?
config.env = "prod"
config.ports.prod[0].port = process.env.PORT
jsonfile.writeFileSync 'config.json', config
redis.sub.subscribe 'client'
redis.sub.on 'message', (ch, data)->
switch ch
when 'client'
data = JSON.parse data
switch data.event
when 'exit'
console.log "#{data.ps}@#{data.port} exited. Restarting..."
new Process data.ps, data.port
when 'message' then console.log "#{data.ps}@#{data.port}: #{data.text}"
when 'error'
console.log data.error
console.log 'terminating...'
process.exit 1
f = true
ps = null
# start socket(s) first, then api, then html
started = {}
startProcess = (data)->
# launch all processes with matching process in list. Skip already started processes
for item in config.ports[config.env]
if started[item.port] then continue
for ps in item.ps
if ps is data
started[item.port] = true
if f
require('./process')
port: item.port
arr: item.ps
f = false
else
t = childProcess.exec "coffee process.coffee #{item.port} #{item.ps.join ' '}"
t.stdout.on 'data', (data)->
process.stdout.write data
t.stderr.on 'data', (data)->
process.stderr.write data
break
startProcess 'socket'
startProcess 'api'
startProcess 'html'
redis.pub.publish 'server', JSON.stringify
event: 'init'
process.on 'exit', ->
redis.pub.publish 'server', JSON.stringify
event: 'exit'