Skip to content

Commit

Permalink
chore: support more configurations via direct js import (#997)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Aug 8, 2024
1 parent 1d822da commit 14ab0f5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
97 changes: 77 additions & 20 deletions cortex-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,101 @@
import {
CORTEX_CPP_PROCESS_DESTROY_URL,
CORTEX_JS_SYSTEM_URL,
defaultCortexCppPort,
defaultCortexJsHost,
defaultCortexJsPort,
} from '@/infrastructure/constants/cortex';
import { getApp } from './app';
import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service';
import { CortexUsecases } from './usecases/cortex/cortex.usecases';

let host: string;
let port: number;
let enginePort: number;

/**
* Start the API server
*/
export async function start(host?: string, port?: number) {
// getting port from env
const sHost = host || process.env.CORTEX_JS_HOST || defaultCortexJsHost;
const sPort = port || process.env.CORTEX_JS_PORT || defaultCortexJsPort;
const app = await getApp(sHost, Number(sPort));
export async function start(
name?: string,
address?: string,
portNumber?: number,
enginePortNumber?: number,
dataFolder?: string,
) {
if (name) {
const isProfileConfigExists = fileManagerService.profileConfigExists(name);
if (!isProfileConfigExists) {
await fileManagerService.writeConfigFile({
...fileManagerService.defaultConfig(),
apiServerHost: address || defaultCortexJsHost,
apiServerPort: port || defaultCortexJsPort,
cortexCppPort: Number(enginePort) || defaultCortexCppPort,
});
}
}
const {
apiServerHost: configApiServerHost,
apiServerPort: configApiServerPort,
cortexCppPort: configCortexCppPort,
} = await fileManagerService.getConfig();

host = address || configApiServerHost || defaultCortexJsHost;
port = portNumber || configApiServerPort || defaultCortexJsPort;
if (host === 'localhost') {
host = '127.0.0.1';
}
enginePort =
Number(enginePortNumber) || configCortexCppPort || defaultCortexCppPort;
const dataFolderPath = dataFolder;

return startServer(dataFolderPath);
}

async function startServer(dataFolderPath?: string) {
const config = await fileManagerService.getConfig();
try {
await app.listen(sPort, sHost);
if (dataFolderPath) {
await fileManagerService.writeConfigFile({
...config,
dataFolderPath,
});
// load config again to create the data folder
await fileManagerService.getConfig(dataFolderPath);
}
const app = await getApp(host, port);
const cortexUsecases = await app.resolve(CortexUsecases);
await cortexUsecases.startCortex();
console.log(`Started server at http://${sHost}:${sPort}`);
console.log(`API Playground available at http://${sHost}:${sPort}/api`);
} catch {
await cortexUsecases.startCortex().catch((e) => {
throw e;
});
const isServerOnline = await cortexUsecases.isAPIServerOnline();
if (isServerOnline) {
console.log(
`Server is already running at http://${host}:${port}. Please use 'cortex stop' to stop the server.`,
);
}
await app.listen(port, host);
await fileManagerService.writeConfigFile({
...config,
apiServerHost: host,
apiServerPort: port,
dataFolderPath: dataFolderPath || config.dataFolderPath,
cortexCppPort: enginePort,
});
} catch (e) {
console.error(e);
// revert the data folder path if it was set
await fileManagerService.writeConfigFile({
...config,
});
console.error(`Failed to start server. Is port ${port} in use?`);
}
}

/**
* Stop the API server
* @returns
*/
export async function stop(host?: string, port?: number) {
return fetch(CORTEX_JS_SYSTEM_URL(host, port), {
method: 'DELETE',
})
.catch(() => {})
.then(() =>
fetch(CORTEX_CPP_PROCESS_DESTROY_URL(host, port), {
method: 'DELETE',
}),
)
.catch(() => {});
});
}
2 changes: 1 addition & 1 deletion cortex-js/src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function cleanLogs(

// Schedule the next execution with doubled delays
timeout = setTimeout(
() => this.cleanLogs(maxFileSizeBytes, daysToKeep),
() => cleanLogs(maxFileSizeBytes, daysToKeep),
logCleaningInterval,
);
}

0 comments on commit 14ab0f5

Please sign in to comment.