@@ -86,6 +86,7 @@ export async function startServer() {
86
86
stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ] ,
87
87
} ,
88
88
) ;
89
+ realmServer . unref ( ) ;
89
90
if ( realmServer . stdout ) {
90
91
realmServer . stdout . on ( 'data' , ( data : Buffer ) =>
91
92
console . log ( `realm server: ${ data . toString ( ) } ` ) ,
@@ -122,25 +123,36 @@ export async function startServer() {
122
123
) ;
123
124
}
124
125
125
- return new IsolatedRealmServer ( realmServer , testRealmDir ) ;
126
+ return new IsolatedRealmServer ( realmServer , workerManager , testRealmDir ) ;
126
127
}
127
128
128
129
export class IsolatedRealmServer {
129
- private stopped : ( ( ) => void ) | undefined ;
130
+ private realmServerStopped : ( ( ) => void ) | undefined ;
131
+ private workerManagerStopped : ( ( ) => void ) | undefined ;
130
132
private sqlResults : ( ( results : string ) => void ) | undefined ;
131
133
private sqlError : ( ( error : string ) => void ) | undefined ;
132
134
133
135
constructor (
134
136
private realmServerProcess : ReturnType < typeof spawn > ,
137
+ private workerManagerProcess : ReturnType < typeof spawn > ,
135
138
readonly realmPath : string , // useful for debugging
136
139
) {
140
+ workerManagerProcess . on ( 'message' , ( message ) => {
141
+ if ( message === 'stopped' ) {
142
+ if ( ! this . workerManagerStopped ) {
143
+ console . error ( `received unprompted worker manager stop` ) ;
144
+ return ;
145
+ }
146
+ this . workerManagerStopped ( ) ;
147
+ }
148
+ } ) ;
137
149
realmServerProcess . on ( 'message' , ( message ) => {
138
150
if ( message === 'stopped' ) {
139
- if ( ! this . stopped ) {
151
+ if ( ! this . realmServerStopped ) {
140
152
console . error ( `received unprompted server stop` ) ;
141
153
return ;
142
154
}
143
- this . stopped ( ) ;
155
+ this . realmServerStopped ( ) ;
144
156
} else if (
145
157
typeof message === 'string' &&
146
158
message . startsWith ( 'sql-results:' )
@@ -180,10 +192,20 @@ export class IsolatedRealmServer {
180
192
}
181
193
182
194
async stop ( ) {
183
- let stop = new Promise < void > ( ( r ) => ( this . stopped = r ) ) ;
195
+ let realmServerStop = new Promise < void > (
196
+ ( r ) => ( this . realmServerStopped = r ) ,
197
+ ) ;
184
198
this . realmServerProcess . send ( 'stop' ) ;
185
- await stop ;
186
- this . stopped = undefined ;
199
+ await realmServerStop ;
200
+ this . realmServerStopped = undefined ;
187
201
this . realmServerProcess . send ( 'kill' ) ;
202
+
203
+ let workerManagerStop = new Promise < void > (
204
+ ( r ) => ( this . workerManagerStopped = r ) ,
205
+ ) ;
206
+ this . workerManagerProcess . send ( 'stop' ) ;
207
+ await workerManagerStop ;
208
+ this . workerManagerStopped = undefined ;
209
+ this . workerManagerProcess . send ( 'kill' ) ;
188
210
}
189
211
}
0 commit comments