-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: failed to relaunch app to update (#3351)
- Loading branch information
Showing
5 changed files
with
141 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import { windowManager } from './../managers/window' | ||
import { app } from 'electron' | ||
import { cleanCortexProcesses, stopCortexApiServer } from './cortex' | ||
|
||
export function cleanUpAndQuit() { | ||
|
||
/** | ||
* Clean up windows then quit | ||
*/ | ||
export async function cleanUpAndQuit() { | ||
windowManager.cleanUp() | ||
await stopCortexApiServer() | ||
await cleanCortexProcesses() | ||
app.quit() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { killProcessesOnPort } from './process' | ||
|
||
// Cortex server configurations | ||
export const cortexJsPort = 1338 | ||
export const cortexCppPort = 3940 | ||
export const cortexHost = '127.0.0.1' | ||
|
||
/** | ||
* Kills all possible running cortex processes | ||
*/ | ||
export async function cleanCortexProcesses() { | ||
await killProcessesOnPort(cortexCppPort) | ||
await killProcessesOnPort(cortexJsPort) | ||
} | ||
|
||
/** | ||
* Stops the cortex API server | ||
*/ | ||
export async function stopCortexApiServer() { | ||
// this function is not meant to be success. It will throw an error. | ||
try { | ||
await fetch(`http://${cortexHost}:${cortexJsPort}/v1/system`, { | ||
method: 'DELETE', | ||
}) | ||
} catch (error) { | ||
// Do nothing | ||
// Accept failure here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { execSync } from 'child_process' | ||
|
||
/** | ||
* Kill process on port util | ||
* @param port port number to kill | ||
*/ | ||
export function killProcessesOnPort(port: number): void { | ||
try { | ||
console.log(`Killing processes on port ${port}...`) | ||
if (process.platform === 'win32') { | ||
killProcessesOnWindowsPort(port) | ||
} else { | ||
killProcessesOnUnixPort(port) | ||
} | ||
} catch (error) { | ||
console.error( | ||
`Failed to kill process(es) on port ${port}: ${(error as Error).message}` | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Kill process on port - Windows | ||
* @param port | ||
* @returns | ||
*/ | ||
function killProcessesOnWindowsPort(port: number): void { | ||
let result: string | ||
try { | ||
result = execSync(`netstat -ano | findstr :${port}`).toString() | ||
} catch (error) { | ||
console.log(`No processes found on port ${port}.`) | ||
return | ||
} | ||
|
||
const lines = result.split('\n').filter(Boolean) | ||
|
||
if (lines.length === 0) { | ||
console.log(`No processes found on port ${port}.`) | ||
return | ||
} | ||
|
||
const pids = lines | ||
.map((line) => { | ||
const parts = line.trim().split(/\s+/) | ||
return parts[parts.length - 1] | ||
}) | ||
.filter((pid): pid is string => Boolean(pid) && !isNaN(Number(pid))) | ||
|
||
if (pids.length === 0) { | ||
console.log(`No valid PIDs found for port ${port}.`) | ||
return | ||
} | ||
const uniquePids = Array.from(new Set(pids)) | ||
console.log('uniquePids', uniquePids) | ||
|
||
uniquePids.forEach((pid) => { | ||
try { | ||
execSync(`taskkill /PID ${pid} /F`) | ||
console.log( | ||
`Process with PID ${pid} on port ${port} has been terminated.` | ||
) | ||
} catch (error) { | ||
console.error( | ||
`Failed to kill process with PID ${pid}: ${(error as Error).message}` | ||
) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Kill process on port - Unix | ||
* @param port | ||
* @returns | ||
*/ | ||
function killProcessesOnUnixPort(port: number): void { | ||
let pids: string[] | ||
|
||
try { | ||
pids = execSync(`lsof -ti tcp:${port}`) | ||
.toString() | ||
.trim() | ||
.split('\n') | ||
.filter(Boolean) | ||
} catch (error) { | ||
if ((error as { status?: number }).status === 1) { | ||
console.log(`No processes found on port ${port}.`) | ||
return | ||
} | ||
throw error // Re-throw if it's not the "no processes found" error | ||
} | ||
|
||
pids.forEach((pid) => { | ||
process.kill(parseInt(pid), 'SIGTERM') | ||
console.log(`Process with PID ${pid} on port ${port} has been terminated.`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters