@@ -5,21 +5,22 @@ import * as fsPromises from 'fs/promises';
55import * as fs from 'fs' ;
66import Logger from "./Logger" ;
77import { GenerateRandomPort } from "./Port" ;
8- import DBDestroySignal from "./AbortSignal" ;
98import { ExecuteFileReturn , InstalledMySQLVersion , InternalServerOptions , MySQLDB } from "../../types" ;
109import { normalize as normalizePath , resolve as resolvePath } from 'path'
1110import { lockFile , waitForLock } from "./FileLock" ;
11+ import { onExit } from "signal-exit" ;
1212
1313class Executor {
1414 logger : Logger ;
15+ DBDestroySignal = new AbortController ( ) ;
1516
1617 constructor ( logger : Logger ) {
1718 this . logger = logger ;
1819 }
1920
2021 #executeFile( command : string , args : string [ ] ) : Promise < ExecuteFileReturn > {
2122 return new Promise ( resolve => {
22- execFile ( command , args , { signal : DBDestroySignal . signal } , ( error , stdout , stderr ) => {
23+ execFile ( command , args , { signal : this . DBDestroySignal . signal } , ( error , stdout , stderr ) => {
2324 resolve ( { error, stdout, stderr} )
2425 } )
2526 } )
@@ -75,7 +76,14 @@ class Executor {
7576 const socket = os . platform ( ) === 'win32' ? `MySQL-${ crypto . randomUUID ( ) } ` : `${ dbPath } /m.sock`
7677 const xSocket = os . platform ( ) === 'win32' ? `MySQLX-${ crypto . randomUUID ( ) } ` : `${ dbPath } /x.sock`
7778
78- 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 : DBDestroySignal . signal , killSignal : 'SIGKILL' } )
79+ const removeExitHandler = onExit ( ( ) => {
80+ this . DBDestroySignal . abort ( )
81+ if ( options . _DO_NOT_USE_deleteDBAfterStopped ) {
82+ fs . rmSync ( options . _DO_NOT_USE_dbPath , { recursive : true , maxRetries : 50 , force : true } )
83+ }
84+ } )
85+
86+ 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' } )
7987
8088 //resolveFunction is the function that will be called to resolve the promise that stops the database.
8189 //If resolveFunction is not undefined, the database has received a kill signal and data cleanup procedures should run.
@@ -111,7 +119,7 @@ class Executor {
111119 await this . #deleteDatabaseDirectory( dbPath )
112120 }
113121 } catch ( e ) {
114- this . logger . error ( 'An erorr occurred while deleting database directory at path:' , dbPath , '| The error was:' , e )
122+ this . logger . error ( 'An error occurred while deleting database directory at path:' , dbPath , '| The error was:' , e )
115123 } finally {
116124 try {
117125 if ( binaryFilepath . includes ( os . tmpdir ( ) ) && ! options . downloadBinaryOnce ) {
@@ -170,6 +178,7 @@ class Executor {
170178 }
171179 } else if ( file . includes ( 'ready for connections. Version:' ) ) {
172180 fs . unwatchFile ( errorLogFile )
181+ removeExitHandler ( )
173182 resolve ( {
174183 port,
175184 xPort : mySQLXPort ,
0 commit comments