-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
116 lines (96 loc) · 2.71 KB
/
index.js
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
const fs = require('fs')
const tx2 = require('tx2')
console.log('PID: ', process.pid)
const config = require('./src/config')
const Server = require('./src/server')
const alertService = require('./src/services/alert')
const { saveConnections } = require('./src/services/connections')
const socketService = require('./src/services/socket')
/* Load available exchanges
*/
if (!config.exchanges || !config.exchanges.length) {
config.exchanges = []
fs.readdirSync('./src/exchanges/').forEach(file => {
;/\.js$/.test(file) && config.exchanges.push(file.replace(/\.js$/, ''))
})
}
const exchanges = []
for (let name of config.exchanges) {
const exchange = new (require('./src/exchanges/' + name))()
config.exchanges[config.exchanges.indexOf(name)] = exchange.id
exchanges.push(exchange)
}
/* Start server
*/
const server = new Server(exchanges)
/* Backup server on SIGINT
*/
process.on('SIGINT', async function () {
console.log('\nSIGINT')
if (!server.canExit()) {
return
}
if (config.collect) {
if (alertService.enabled) {
try {
await alertService.persistAlerts()
console.log(`[exit] saved alerts ✓`)
} catch (error) {
console.error(`[exit] failed to save alerts`, error.message)
}
}
if (config.persistConnections) {
try {
await saveConnections(true)
console.log(`[exit] saved connections ✓`)
} catch (error) {
console.error(`[exit] failed to save connections`, error.message)
}
}
try {
await server.backupTrades(true)
console.log(`[exit] saved trades ✓`)
} catch (error) {
console.error(`[exit] failed to save trades`, error.message)
}
}
try {
await socketService.close()
console.log(`[exit] closed sockets ✓`)
} catch (error) {
console.error(`[exit] failed to close sockets`, error.message)
}
console.log('[init] goodbye')
process.exit()
})
if (process.env.pmx) {
tx2.action('connect', function (markets, reply) {
server
.connect(markets.split(','))
.then(result => {
reply(result.join(', '))
})
.catch(err => {
reply(`FAILED to connect ${markets} (${err.message})`)
})
})
tx2.action('disconnect', function (markets, reply) {
server
.disconnect(markets.split(','))
.then(result => {
reply(result.join(', '))
})
.catch(err => {
reply(`FAILED to disconnect ${markets} (${err.message})`)
})
})
tx2.action('triggeralert', function (user, reply) {
// offline webpush testing
try {
const result = server.triggerAlert(user)
reply(result)
} catch (error) {
console.error('FAILED to trigger alert', user, error.message)
}
})
}