Use reduced logging on auto server start

This commit is contained in:
Ryan Huang
2024-05-01 10:18:10 -04:00
parent dc91add950
commit a76302a478
2 changed files with 29 additions and 13 deletions

View File

@ -5,7 +5,7 @@ import { flag } from "cmd-ts";
import inquirer from "inquirer"; import inquirer from "inquirer";
import { platform } from "os"; import { platform } from "os";
import { getCliPref } from "./cliPref"; import { getCliPref } from "./cliPref";
import { getLogLevelMap, type LogLevelArgs, type LogLevelMap } from "./logLevel"; import { type LogLevelArgs, type LogLevelMap } from "./logLevel";
import { import {
checkHttpServer, checkHttpServer,
getServerLastStatus, getServerLastStatus,
@ -165,21 +165,21 @@ export async function createClient(
) { ) {
const { noLaunch, yes } = args; const { noLaunch, yes } = args;
let port: number; let port: number;
const selfDeletingLogger = createSelfDeletingLogger(logger, getLogLevelMap(args)); // const selfDeletingLogger = createSelfDeletingLogger(logger, getLogLevelMap(args));
try { try {
const lastStatus = await getServerLastStatus(selfDeletingLogger); const lastStatus = await getServerLastStatus(logger);
port = lastStatus.port; port = lastStatus.port;
} catch (e) { } catch (e) {
selfDeletingLogger.debug("Failed to get last server status", e); logger.debug("Failed to get last server status", e);
port = 1234; port = 1234;
} }
if (!(await checkHttpServer(selfDeletingLogger, port))) { if (!(await checkHttpServer(logger, port))) {
if (!(await maybeTryStartServer(selfDeletingLogger, { port, noLaunch, yes }))) { if (!(await maybeTryStartServer(logger, { port, noLaunch, yes, useReducedLogging: true }))) {
process.exit(1); process.exit(1);
} }
} }
const baseUrl = `ws://127.0.0.1:${port}`; const baseUrl = `ws://127.0.0.1:${port}`;
selfDeletingLogger.debug(`Connecting to server with baseUrl ${port}`); logger.debug(`Connecting to server with baseUrl ${port}`);
process.stderr.clearLine(0); process.stderr.clearLine(0);
return new LMStudioClient({ return new LMStudioClient({
baseUrl, baseUrl,

View File

@ -167,10 +167,11 @@ export interface StartServerOpts {
cors?: boolean; cors?: boolean;
noLaunch?: boolean; noLaunch?: boolean;
yes?: boolean; yes?: boolean;
useReducedLogging?: boolean;
} }
export async function startServer( export async function startServer(
logger: SimpleLogger, logger: SimpleLogger,
{ port, cors, noLaunch, yes }: StartServerOpts = {}, { port, cors, noLaunch, yes, useReducedLogging }: StartServerOpts = {},
): Promise<boolean> { ): Promise<boolean> {
if (port === undefined) { if (port === undefined) {
try { try {
@ -189,10 +190,16 @@ export async function startServer(
CORS is enabled. This means any website you visit can use the LM Studio server. CORS is enabled. This means any website you visit can use the LM Studio server.
`; `;
} }
logger.info(`Attempting to start the server on port ${port}...`); logger.logAtLevel(
useReducedLogging ? "debug" : "info",
`Attempting to start the server on port ${port}...`,
);
await writeToServerCtl(logger, { type: "start", port, cors }); await writeToServerCtl(logger, { type: "start", port, cors });
if (await waitForCtlFileClear(logger, 100, 10)) { if (await waitForCtlFileClear(logger, 100, 10)) {
logger.info(`Requested the server to be started on port ${port}.`); logger.logAtLevel(
useReducedLogging ? "debug" : "info",
`Requested the server to be started on port ${port}.`,
);
} else { } else {
if (platform() === "linux") { if (platform() === "linux") {
// Sorry, linux users :( // Sorry, linux users :(
@ -261,7 +268,10 @@ export async function startServer(
// At this point, LM Studio is launching. Once it is ready, it will consume the control file // At this point, LM Studio is launching. Once it is ready, it will consume the control file
// and start the server. Let's wait for that to happen. // and start the server. Let's wait for that to happen.
if (await waitForCtlFileClear(logger, 1000, 10)) { if (await waitForCtlFileClear(logger, 1000, 10)) {
logger.info(`Requested the server to be started on port ${port}.`); logger.logAtLevel(
useReducedLogging ? "debug" : "info",
`Requested the server to be started on port ${port}.`,
);
} else { } else {
logger.error(`Failed to start the server on port ${port}`); logger.error(`Failed to start the server on port ${port}`);
process.exit(1); process.exit(1);
@ -274,10 +284,16 @@ export async function startServer(
return false; return false;
} }
} }
logger.info("Verifying the server is running..."); logger.logAtLevel(useReducedLogging ? "debug" : "info", "Verifying the server is running...");
if (await checkHttpServerWithRetries(logger, port, 5)) { if (await checkHttpServerWithRetries(logger, port, 5)) {
logger.info(`Verification succeeded. The server is running on port ${port}.`); logger.logAtLevel(
useReducedLogging ? "debug" : "info",
`Verification succeeded. The server is running on port ${port}.`,
);
if (useReducedLogging) {
logger.info("Successfully started the server and verified it is running.");
}
return true; return true;
} else { } else {
logger.error("Failed to verify the server is running. Please try to use another port."); logger.error("Failed to verify the server is running. Please try to use another port.");