Skip to content

Commit

Permalink
Use reduced logging on auto server start
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-the-crayon committed May 1, 2024
1 parent dc91add commit a76302a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { flag } from "cmd-ts";
import inquirer from "inquirer";
import { platform } from "os";
import { getCliPref } from "./cliPref";
import { getLogLevelMap, type LogLevelArgs, type LogLevelMap } from "./logLevel";
import { type LogLevelArgs, type LogLevelMap } from "./logLevel";
import {
checkHttpServer,
getServerLastStatus,
Expand Down Expand Up @@ -165,21 +165,21 @@ export async function createClient(
) {
const { noLaunch, yes } = args;
let port: number;
const selfDeletingLogger = createSelfDeletingLogger(logger, getLogLevelMap(args));
// const selfDeletingLogger = createSelfDeletingLogger(logger, getLogLevelMap(args));
try {
const lastStatus = await getServerLastStatus(selfDeletingLogger);
const lastStatus = await getServerLastStatus(logger);
port = lastStatus.port;
} catch (e) {
selfDeletingLogger.debug("Failed to get last server status", e);
logger.debug("Failed to get last server status", e);
port = 1234;
}
if (!(await checkHttpServer(selfDeletingLogger, port))) {
if (!(await maybeTryStartServer(selfDeletingLogger, { port, noLaunch, yes }))) {
if (!(await checkHttpServer(logger, port))) {
if (!(await maybeTryStartServer(logger, { port, noLaunch, yes, useReducedLogging: true }))) {
process.exit(1);
}
}
const baseUrl = `ws:https://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);
return new LMStudioClient({
baseUrl,
Expand Down
28 changes: 22 additions & 6 deletions src/subcommands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ export interface StartServerOpts {
cors?: boolean;
noLaunch?: boolean;
yes?: boolean;
useReducedLogging?: boolean;
}
export async function startServer(
logger: SimpleLogger,
{ port, cors, noLaunch, yes }: StartServerOpts = {},
{ port, cors, noLaunch, yes, useReducedLogging }: StartServerOpts = {},
): Promise<boolean> {
if (port === undefined) {
try {
Expand All @@ -189,10 +190,16 @@ export async function startServer(
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 });
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 {
if (platform() === "linux") {
// Sorry, linux users :(
Expand Down Expand Up @@ -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
// and start the server. Let's wait for that to happen.
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 {
logger.error(`Failed to start the server on port ${port}`);
process.exit(1);
Expand All @@ -274,10 +284,16 @@ export async function startServer(
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)) {
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;
} else {
logger.error("Failed to verify the server is running. Please try to use another port.");
Expand Down

0 comments on commit a76302a

Please sign in to comment.