1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-09-11 10:25:40 +03:00

feat(server): detach the TCP server from the main process in dev

This commit is contained in:
louistiti 2022-09-04 18:54:16 +08:00
parent 891b032543
commit cf563f5f32
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
3 changed files with 15 additions and 16 deletions

View File

@ -115,8 +115,6 @@ class Nlu {
)
})
} else {
log.title('NLU')
try {
const container = await containerBootstrap()
@ -161,8 +159,6 @@ class Nlu {
)
})
} else {
log.title('NLU')
try {
const container = await containerBootstrap()

View File

@ -1,9 +1,13 @@
import Net from 'net'
import { EventEmitter } from 'events'
import { IS_PRODUCTION_ENV } from '@/constants'
import log from '@/helpers/log'
const interval = 3000 // ms
// Time interval between each try (in ms)
const INTERVAL = IS_PRODUCTION_ENV ? 3000 : 300
// Number of retries to connect to the TCP server
const RETRIES_NB = IS_PRODUCTION_ENV ? 5 : 15
export default class TcpClient {
constructor(host, port) {
@ -41,7 +45,7 @@ export default class TcpClient {
if (err.code === 'ECONNREFUSED') {
this.reconnectCounter += 1
if (this.reconnectCounter >= 5) {
if (this.reconnectCounter >= RETRIES_NB) {
log.error('Failed to connect to the TCP server')
this.tcpSocket.end()
}
@ -51,7 +55,7 @@ export default class TcpClient {
setTimeout(() => {
this.connect()
}, interval * this.reconnectCounter)
}, INTERVAL * this.reconnectCounter)
}
} else {
log.error(`Failed to connect to the TCP server: ${err}`)
@ -65,7 +69,7 @@ export default class TcpClient {
setTimeout(() => {
this.connect()
}, interval)
}, INTERVAL)
}
get status() {

View File

@ -1,7 +1,10 @@
import { spawn } from 'child_process'
// import { IS_DEVELOPMENT_ENV } from '@/constants'
import { TCP_SERVER_HOST, TCP_SERVER_PORT } from '@/constants'
import {
TCP_SERVER_HOST,
TCP_SERVER_PORT,
IS_DEVELOPMENT_ENV
} from '@/constants'
import lang from '@/helpers/lang'
import TcpClient from '@/core/tcp-client'
import server from '@/core/http-server/server'
@ -13,15 +16,11 @@ import server from '@/core/http-server/server'
process.env['LEON_LANG']
)}`,
{
shell: true
// detached: IS_DEVELOPMENT_ENV
shell: true,
detached: IS_DEVELOPMENT_ENV
}
)
/*if (IS_DEVELOPMENT_ENV) {
}*/
global.tcpClient = new TcpClient(TCP_SERVER_HOST, TCP_SERVER_PORT)
await server.init()