-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart.coffee
55 lines (49 loc) · 1.64 KB
/
start.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
# prepare settings, setup logs/pid/background states
# and finally load sybil, so neat.
fs = require "fs"
http = require "http"
http.globalAgent.maxSockets = 100 * 100;
https = require "https"
https.globalAgent.maxSockets = 100* 100;
settings = require("./settings.coffee")
logger = require("./common/logger.coffee")
try
settings.parseConfig("./settings.user.json")
catch e
console.error e
console.error "fail to parse user config"
console.error "panic"
process.exit(1)
if process.argv[2] is "debug"
settings.debug = true
if process.argv[2] is "settings"
console.log settings[process.argv[3]] or ""
process.exit(0)
logger.useColor = settings.logWithColor
logger.root = settings.root
# setup log redirections if log path is defined
try
process.chdir __dirname
catch
console.error "fail to change dir to",dirname
# do we have any clone running (and murder it :( )
pm = require("./common/processManager.coffee")
pidPath = settings.pidPath or "./pid"
if fs.existsSync(pidPath)
pid = parseInt(fs.readFileSync(pidPath))
if not isNaN pid
pm.ensureDeath(pid)
# ensure it's running in background ( or suicide and respawn)
if not settings.debug and process.stdin and process.stdin.isTTY
console.log "fork to background"
if pidPath and fs.existsSync pidPath
fs.unlinkSync pidPath
pm.background()
if settings.logPath and not settings.debug
logStream = fs.createWriteStream(settings.logPath,{flags:"a"});
process.__defineGetter__ "stdout",()->logStream
process.__defineGetter__ "stderr",()->logStream
# save pid
fs.writeFileSync(pidPath,process.pid)
# finally, start it!
require("./core/sybil.coffee")