fix: check if port is occupied before start local server (#2098)
Signed-off-by: James <james@jan.ai> Co-authored-by: James <james@jan.ai>
This commit is contained in:
parent
53006dd26e
commit
5c7b791c26
@ -1,6 +1,5 @@
|
|||||||
import { basename, isAbsolute, join, relative } from 'path'
|
import { basename, isAbsolute, join, relative } from 'path'
|
||||||
|
|
||||||
import { AppRoute } from '../../../api'
|
|
||||||
import { Processor } from './Processor'
|
import { Processor } from './Processor'
|
||||||
import { getAppConfigurations as appConfiguration, updateAppConfiguration } from '../../helper'
|
import { getAppConfigurations as appConfiguration, updateAppConfiguration } from '../../helper'
|
||||||
import { log as writeLog, logServer as writeServerLog } from '../../helper/log'
|
import { log as writeLog, logServer as writeServerLog } from '../../helper/log'
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import {
|
|||||||
getJanExtensionsPath,
|
getJanExtensionsPath,
|
||||||
} from '@janhq/core/node'
|
} from '@janhq/core/node'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
|
import tcpPortUsed from 'tcp-port-used'
|
||||||
|
|
||||||
// Load environment variables
|
// Load environment variables
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
@ -46,6 +47,15 @@ export interface ServerConfig {
|
|||||||
* @param configs - Server configurations
|
* @param configs - Server configurations
|
||||||
*/
|
*/
|
||||||
export const startServer = async (configs?: ServerConfig): Promise<boolean> => {
|
export const startServer = async (configs?: ServerConfig): Promise<boolean> => {
|
||||||
|
if (configs?.port && configs?.host) {
|
||||||
|
const inUse = await tcpPortUsed.check(Number(configs.port), configs.host)
|
||||||
|
if (inUse) {
|
||||||
|
const errorMessage = `Port ${configs.port} is already in use.`
|
||||||
|
logServer(errorMessage)
|
||||||
|
throw new Error(errorMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update server settings
|
// Update server settings
|
||||||
isVerbose = configs?.isVerboseEnabled ?? true
|
isVerbose = configs?.isVerboseEnabled ?? true
|
||||||
hostSetting = configs?.host ?? JAN_API_HOST
|
hostSetting = configs?.host ?? JAN_API_HOST
|
||||||
|
|||||||
@ -39,6 +39,8 @@ import ModalTroubleShooting, {
|
|||||||
} from '@/containers/ModalTroubleShoot'
|
} from '@/containers/ModalTroubleShoot'
|
||||||
import ServerLogs from '@/containers/ServerLogs'
|
import ServerLogs from '@/containers/ServerLogs'
|
||||||
|
|
||||||
|
import { toaster } from '@/containers/Toast'
|
||||||
|
|
||||||
import { loadModelErrorAtom, useActiveModel } from '@/hooks/useActiveModel'
|
import { loadModelErrorAtom, useActiveModel } from '@/hooks/useActiveModel'
|
||||||
import { useLogs } from '@/hooks/useLogs'
|
import { useLogs } from '@/hooks/useLogs'
|
||||||
|
|
||||||
@ -106,6 +108,45 @@ const LocalServerScreen = () => {
|
|||||||
handleChangePort(port)
|
handleChangePort(port)
|
||||||
}, [handleChangePort, port])
|
}, [handleChangePort, port])
|
||||||
|
|
||||||
|
const onStartServerClick = async () => {
|
||||||
|
if (selectedModel == null) return
|
||||||
|
try {
|
||||||
|
const isStarted = await window.core?.api?.startServer({
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
isCorsEnabled,
|
||||||
|
isVerboseEnabled,
|
||||||
|
})
|
||||||
|
await startModel(selectedModel.id)
|
||||||
|
if (isStarted) setServerEnabled(true)
|
||||||
|
if (firstTimeVisitAPIServer) {
|
||||||
|
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
|
||||||
|
setFirstTimeVisitAPIServer(false)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
toaster({
|
||||||
|
title: `Failed to start server!`,
|
||||||
|
description: 'Please check Server Logs for more details.',
|
||||||
|
type: 'error',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onStopServerClick = async () => {
|
||||||
|
window.core?.api?.stopServer()
|
||||||
|
setServerEnabled(false)
|
||||||
|
setLoadModelError(undefined)
|
||||||
|
}
|
||||||
|
|
||||||
|
const onToggleServer = async () => {
|
||||||
|
if (serverEnabled) {
|
||||||
|
await onStopServerClick()
|
||||||
|
} else {
|
||||||
|
await onStartServerClick()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-full w-full" data-testid="local-server-testid">
|
<div className="flex h-full w-full" data-testid="local-server-testid">
|
||||||
{/* Left SideBar */}
|
{/* Left SideBar */}
|
||||||
@ -122,26 +163,7 @@ const LocalServerScreen = () => {
|
|||||||
block
|
block
|
||||||
themes={serverEnabled ? 'danger' : 'primary'}
|
themes={serverEnabled ? 'danger' : 'primary'}
|
||||||
disabled={stateModel.loading || errorRangePort || !selectedModel}
|
disabled={stateModel.loading || errorRangePort || !selectedModel}
|
||||||
onClick={async () => {
|
onClick={onToggleServer}
|
||||||
if (serverEnabled) {
|
|
||||||
window.core?.api?.stopServer()
|
|
||||||
setServerEnabled(false)
|
|
||||||
setLoadModelError(undefined)
|
|
||||||
} else {
|
|
||||||
startModel(String(selectedModel?.id))
|
|
||||||
const isStarted = await window.core?.api?.startServer({
|
|
||||||
host,
|
|
||||||
port,
|
|
||||||
isCorsEnabled,
|
|
||||||
isVerboseEnabled,
|
|
||||||
})
|
|
||||||
if (isStarted) setServerEnabled(true)
|
|
||||||
if (firstTimeVisitAPIServer) {
|
|
||||||
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
|
|
||||||
setFirstTimeVisitAPIServer(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{serverEnabled ? 'Stop' : 'Start'} Server
|
{serverEnabled ? 'Stop' : 'Start'} Server
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user