Skip to content

Commit

Permalink
kill dolt server process when removing connection
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu-dev committed Feb 13, 2025
1 parent 1880e98 commit 0f95b18
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
20 changes: 18 additions & 2 deletions web/main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import serve from "electron-serve";
import { createWindow } from "./helpers";
import { initMenu } from "./helpers/menu";
import { removeDoltServerFolder, startServer } from "./doltServer";
import { ChildProcess } from "child_process";
import { L } from "ace-builds-internal/lib/bidiutil";

const isProd = process.env.NODE_ENV === "production";
const userDataPath = app.getPath("userData");
Expand All @@ -35,6 +37,7 @@ if (isProd) {

let graphqlServerProcess: UtilityProcess | null;
let mainWindow: BrowserWindow;
let doltServerProcess: ChildProcess | null;

function isExternalUrl(url: string) {
return !url.includes("localhost:") && !url.includes("app://");
Expand Down Expand Up @@ -189,6 +192,10 @@ app.on("before-quit", () => {
graphqlServerProcess.kill();
graphqlServerProcess = null;
}
if (doltServerProcess) {
doltServerProcess.kill();
doltServerProcess = null;
}
});

app.on("window-all-closed", () => {
Expand Down Expand Up @@ -229,7 +236,12 @@ ipcMain.handle(
"start-dolt-server",
async (event, connectionName: string, port: string, init?: boolean) => {
try {
await startServer(mainWindow, connectionName, port, init);
doltServerProcess = await startServer(
mainWindow,
connectionName,
port,
init,
);
} catch (error) {
throw new Error(getErrorMessage(error));
} finally {
Expand All @@ -240,7 +252,11 @@ ipcMain.handle(

ipcMain.handle(
"remove-dolt-connection",
async (event, connectionName: string, port: string) => {
async (event, connectionName: string) => {
if (doltServerProcess) {
doltServerProcess.kill();
doltServerProcess = null;
}
const dbFolderPath = isProd
? path.join(app.getPath("userData"), "databases", connectionName)
: path.join(__dirname, "..", "build", "databases", connectionName);
Expand Down
27 changes: 8 additions & 19 deletions web/main/doltServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import path from "path";
import { app, BrowserWindow } from "electron";
import { exec } from "child_process";
import { ChildProcess, exec } from "child_process";

type ErrorReturnType = {
errorMsg?: string;
Expand All @@ -14,7 +14,7 @@ export async function startServer(
connectionName: string,
port: string,
init?: boolean,
): Promise<void> {
): Promise<ChildProcess | null> {
const dbFolderPath = isProd
? path.join(app.getPath("userData"), "databases", connectionName)
: path.join(__dirname, "..", "build", "databases", connectionName);
Expand All @@ -31,15 +31,10 @@ export async function startServer(
}

// Initialize and start the server without checking if it's already running
await initializeDoltRepository(
doltPath,
dbFolderPath,
connectionName,
mainWindow,
);
await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
await initializeDoltRepository(doltPath, dbFolderPath, mainWindow);
return await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
} else {
await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
return await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
}
} catch (error) {
console.error("Failed to set up Dolt server:", error);
Expand Down Expand Up @@ -82,7 +77,6 @@ function getDoltPaths(): string {
function initializeDoltRepository(
doltPath: string,
dbFolderPath: string,
connectionName: string,
mainWindow: BrowserWindow,
): Promise<void> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -136,7 +130,7 @@ function startServerProcess(
dbFolderPath: string,
port: string,
mainWindow: BrowserWindow,
): Promise<void> {
): Promise<ChildProcess | null> {
return new Promise((resolve, reject) => {
const doltServerProcess = exec(
`${doltPath} sql-server -P ${port}`,
Expand All @@ -160,7 +154,7 @@ function startServerProcess(

// Resolve the promise when the server is ready
if (logMessage.includes("Server ready")) {
resolve();
resolve(doltServerProcess);
}
};

Expand All @@ -182,17 +176,12 @@ function startServerProcess(

// Resolve the promise when the server is ready
if (errorMessage.includes("Server ready")) {
resolve();
resolve(doltServerProcess);
}
};

doltServerProcess.stdout?.on("data", handleServerLog);
doltServerProcess.stderr?.on("data", handleServerError);
app.on("before-quit", () => {
if (doltServerProcess) {
doltServerProcess.kill();
}
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions web/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const handler = {
ipcRenderer.on("toggle-left-sidebar", _event => callback()),
startDoltServer: (connectionName: string, port: string, init?: boolean) =>
ipcRenderer.send("start-dolt-server", connectionName, port, init),
removeDoltConnection: (connectionName: string, port: string) =>
ipcRenderer.send("remove-dolt-connection", connectionName, port),
removeDoltConnection: (connectionName: string) =>
ipcRenderer.send("remove-dolt-connection", connectionName),
getDoltServerError: (callback: (value: string) => {}) =>
ipcRenderer.on("server-error", (_event, value) => callback(value)),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ export default function ExistingConnections(props: Props) {
const [deleteModalOpen, setDeleteModalOpen] = useState(false);
const [connectionNameToDelete, setConnectionNameToDelete] = useState("");
const [isLocalDolt, setIsLocalDolt] = useState(false);
const [port, setPort] = useState("");

const onDeleteClicked = (name: string, local: boolean, p: string) => {
const onDeleteClicked = (name: string, local: boolean) => {
setConnectionNameToDelete(name);
setDeleteModalOpen(true);
setIsLocalDolt(local);
setPort(p);
};

// TODO: need to reveal the remove folder error
const removeLocalDoltFolder = async (name: string, p: string) => {
const removeLocalDoltFolder = async (name: string) => {
try {
const result = await window.ipc.invoke("remove-dolt-connection", name, p);
const result = await window.ipc.invoke("remove-dolt-connection", name);
console.log(result);
} catch (error) {
console.error("Failed to remove local Dolt server:", error);
Expand Down Expand Up @@ -103,7 +101,7 @@ export default function ExistingConnections(props: Props) {
}}
callback={
isLocalDolt
? async () => removeLocalDoltFolder(connectionNameToDelete, port)
? async () => removeLocalDoltFolder(connectionNameToDelete)
: undefined
}
/>
Expand Down

0 comments on commit 0f95b18

Please sign in to comment.