forked from leon-ai/leon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
73 lines (61 loc) · 1.67 KB
/
index.ts
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
import { spawn } from 'node:child_process'
import fs from 'node:fs'
import {
IS_DEVELOPMENT_ENV,
IS_TELEMETRY_ENABLED,
LANG as LEON_LANG,
TCP_SERVER_BIN_PATH
} from '@/constants'
import { TCP_CLIENT, HTTP_SERVER, SOCKET_SERVER } from '@/core'
import { Telemetry } from '@/telemetry'
import { LangHelper } from '@/helpers/lang-helper'
import { LogHelper } from '@/helpers/log-helper'
;(async (): Promise<void> => {
process.title = 'leon'
// Start the TCP server
global.tcpServerProcess = spawn(
`${TCP_SERVER_BIN_PATH} ${LangHelper.getShortCode(LEON_LANG)}`,
{
shell: true,
detached: IS_DEVELOPMENT_ENV
}
)
// Connect the TCP client to the TCP server
TCP_CLIENT.connect()
// Start the HTTP server
await HTTP_SERVER.init()
// TODO
// Register HTTP API endpoints
// await HTTP_API.register()
// Start the socket server
SOCKET_SERVER.init()
// Telemetry events
if (IS_TELEMETRY_ENABLED) {
Telemetry.start()
// Watch for errors in the error log file and report them to the telemetry service
fs.watchFile(LogHelper.ERRORS_FILE_PATH, async () => {
const logErrors = await LogHelper.parseErrorLogs()
const lastError = logErrors[logErrors.length - 1] || ''
Telemetry.error(lastError)
})
setInterval(() => {
Telemetry.heartbeat()
}, 1_000 * 3_600 * 6)
;[
'exit',
'SIGINT',
'SIGUSR1',
'SIGUSR2',
'uncaughtException',
'SIGTERM'
].forEach((eventType) => {
process.on(eventType, () => {
Telemetry.stop()
global.tcpServerProcess.kill()
setTimeout(() => {
process.exit(0)
}, 1_000)
})
})
}
})()