Skip to content

Commit f804c72

Browse files
add binary deletion on cli exit
1 parent 8d63443 commit f804c72

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/libraries/Executor.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ class Executor {
6565
}
6666
}
6767

68+
//Returns a path to the binary if it should be deleted
69+
//If it should not be deleted then it returns null
70+
#returnBinaryPathToDelete(binaryFilepath: string, options: InternalServerOptions): string | null {
71+
if (binaryFilepath.includes(os.tmpdir()) && !options.downloadBinaryOnce) {
72+
const splitPath = binaryFilepath.split(os.platform() === 'win32' ? '\\' : '/')
73+
const binariesIndex = splitPath.indexOf('binaries')
74+
//The path will be the directory path for the binary download
75+
splitPath.splice(binariesIndex + 2)
76+
return splitPath.join('/')
77+
}
78+
79+
return null
80+
}
81+
6882
#startMySQLProcess(options: InternalServerOptions, port: number, mySQLXPort: number, datadir: string, dbPath: string, binaryFilepath: string): Promise<MySQLDB> {
6983
const errors: string[] = []
7084
const logFile = `${dbPath}/log.log`
@@ -78,9 +92,15 @@ class Executor {
7892

7993
const removeExitHandler = onExit(() => {
8094
this.DBDestroySignal.abort()
95+
8196
if (options._DO_NOT_USE_deleteDBAfterStopped) {
8297
fs.rmSync(options._DO_NOT_USE_dbPath, {recursive: true, maxRetries: 50, force: true})
8398
}
99+
100+
const binaryPathToDelete = this.#returnBinaryPathToDelete(binaryFilepath, options)
101+
if (binaryPathToDelete) {
102+
fs.rmSync(binaryPathToDelete, {force: true, recursive: true, maxRetries: 50})
103+
}
84104
})
85105

86106
const process = spawn(binaryFilepath, ['--no-defaults', `--port=${port}`, `--datadir=${datadir}`, `--mysqlx-port=${mySQLXPort}`, `--mysqlx-socket=${xSocket}`, `--socket=${socket}`, `--general-log-file=${logFile}`, '--general-log=1', `--init-file=${dbPath}/init.sql`, '--bind-address=127.0.0.1', '--innodb-doublewrite=OFF', '--mysqlx=FORCE', `--log-error=${errorLogFile}`, `--user=${os.userInfo().username}`], {signal: this.DBDestroySignal.signal, killSignal: 'SIGKILL'})
@@ -122,13 +142,9 @@ class Executor {
122142
this.logger.error('An error occurred while deleting database directory at path:', dbPath, '| The error was:', e)
123143
} finally {
124144
try {
125-
if (binaryFilepath.includes(os.tmpdir()) && !options.downloadBinaryOnce) {
126-
const splitPath = binaryFilepath.split(os.platform() === 'win32' ? '\\' : '/')
127-
const binariesIndex = splitPath.indexOf('binaries')
128-
//The path will be the directory path for the binary download
129-
splitPath.splice(binariesIndex + 2)
130-
//Delete the binary folder
131-
await fsPromises.rm(splitPath.join('/'), {force: true, recursive: true})
145+
const binaryPathToDelete = this.#returnBinaryPathToDelete(binaryFilepath, options)
146+
if (binaryPathToDelete) {
147+
await fsPromises.rm(binaryPathToDelete, {force: true, recursive: true, maxRetries: 50})
132148
}
133149
} catch (e) {
134150
this.logger.error('An error occurred while deleting no longer needed MySQL binary:', e)

0 commit comments

Comments
 (0)