Skip to content

Commit 1ff6464

Browse files
move exit handler to startMySQL method
1 parent 0aab95b commit 1ff6464

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/libraries/Executor.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { onExit } from "signal-exit";
1313
class Executor {
1414
logger: Logger;
1515
DBDestroySignal = new AbortController();
16+
removeExitHandler: () => void
1617

1718
constructor(logger: Logger) {
1819
this.logger = logger;
@@ -90,27 +91,6 @@ class Executor {
9091
const socket = os.platform() === 'win32' ? `MySQL-${crypto.randomUUID()}` : `${dbPath}/m.sock`
9192
const xSocket = os.platform() === 'win32' ? `MySQLX-${crypto.randomUUID()}` : `${dbPath}/x.sock`
9293

93-
const removeExitHandler = onExit(() => {
94-
this.DBDestroySignal.abort()
95-
96-
if (options._DO_NOT_USE_deleteDBAfterStopped) {
97-
try {
98-
fs.rmSync(options._DO_NOT_USE_dbPath, {recursive: true, maxRetries: 50, force: true})
99-
} catch (e) {
100-
this.logger.error('An error occurred while deleting database directory path:', e)
101-
}
102-
}
103-
104-
const binaryPathToDelete = this.#returnBinaryPathToDelete(binaryFilepath, options)
105-
if (binaryPathToDelete) {
106-
try {
107-
fs.rmSync(binaryPathToDelete, {force: true, recursive: true, maxRetries: 50})
108-
} catch (e) {
109-
this.logger.error('An error occurred while deleting database binary:', e)
110-
}
111-
}
112-
})
113-
11494
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'})
11595

11696
//resolveFunction is the function that will be called to resolve the promise that stops the database.
@@ -213,7 +193,7 @@ class Executor {
213193
return new Promise(async (resolve, reject) => {
214194
resolveFunction = resolve;
215195

216-
removeExitHandler()
196+
this.removeExitHandler()
217197

218198
const killed = await this.#killProcess(process)
219199

@@ -440,6 +420,27 @@ class Executor {
440420
}
441421

442422
async startMySQL(options: InternalServerOptions, binaryFilepath: string): Promise<MySQLDB> {
423+
this.removeExitHandler = onExit(() => {
424+
this.DBDestroySignal.abort()
425+
426+
if (options._DO_NOT_USE_deleteDBAfterStopped) {
427+
try {
428+
fs.rmSync(options._DO_NOT_USE_dbPath, {recursive: true, maxRetries: 50, force: true})
429+
} catch (e) {
430+
this.logger.error('An error occurred while deleting database directory path:', e)
431+
}
432+
}
433+
434+
const binaryPathToDelete = this.#returnBinaryPathToDelete(binaryFilepath, options)
435+
if (binaryPathToDelete) {
436+
try {
437+
fs.rmSync(binaryPathToDelete, {force: true, recursive: true, maxRetries: 50})
438+
} catch (e) {
439+
this.logger.error('An error occurred while deleting database binary:', e)
440+
}
441+
}
442+
})
443+
443444
let retries = 0;
444445

445446
const datadir = normalizePath(`${options._DO_NOT_USE_dbPath}/data`)

0 commit comments

Comments
 (0)