@@ -23,6 +23,7 @@ import { TypedEmitter, generateSTHKey, normalizeUrl } from "@scramjet/utility";
23
23
import { ObjLogger } from "@scramjet/obj-logger" ;
24
24
import { ReasonPhrases } from "http-status-codes" ;
25
25
import { DuplexStream } from "@scramjet/api-server" ;
26
+ import { VerserClientConnection } from "@scramjet/verser/src/types" ;
26
27
27
28
type STHInformation = {
28
29
id ?: string ;
@@ -223,7 +224,7 @@ export class CPMConnector extends TypedEmitter<Events> {
223
224
* @returns {string } Host id.
224
225
*/
225
226
getId ( ) : string | undefined {
226
- return this . info . id ;
227
+ return this . config . id ;
227
228
}
228
229
229
230
/**
@@ -249,6 +250,8 @@ export class CPMConnector extends TypedEmitter<Events> {
249
250
} ;
250
251
}
251
252
253
+ await this . setLoadCheckMessageSender ( ) ;
254
+
252
255
StringStream . from ( duplex . input as Readable )
253
256
. JSONParse ( )
254
257
. map ( async ( message : EncodedControlMessage ) => {
@@ -292,9 +295,10 @@ export class CPMConnector extends TypedEmitter<Events> {
292
295
[ CPMMessageCode . NETWORK_INFO , await this . getNetworkInfo ( ) ]
293
296
) ;
294
297
295
- this . emit ( "connect" ) ;
296
298
297
- await this . setLoadCheckMessageSender ( ) ;
299
+
300
+
301
+ this . emit ( "connect" ) ;
298
302
299
303
return new Promise ( ( resolve , reject ) => {
300
304
duplex . on ( "end" , ( ) => {
@@ -332,11 +336,17 @@ export class CPMConnector extends TypedEmitter<Events> {
332
336
this . verserClient . updateHeaders ( { "x-sth-id" : this . info . id } ) ;
333
337
}
334
338
335
- let connection ;
339
+ let connection : VerserClientConnection ;
336
340
337
341
try {
338
342
this . logger . trace ( "Connecting to Manager" , this . cpmUrl , this . cpmId ) ;
339
343
connection = await this . verserClient . connect ( ) ;
344
+
345
+ connection . socket
346
+ . once ( "close" , async ( ) => {
347
+ this . logger . warn ( "CLOSE STATUS" , connection . res . statusCode )
348
+ await this . handleConnectionClose ( connection . res . statusCode || - 1 ) ;
349
+ } ) ;
340
350
} catch ( error : any ) {
341
351
this . logger . error ( "Can not connect to Manager" , this . cpmUrl , this . cpmId , error . message ) ;
342
352
@@ -345,12 +355,7 @@ export class CPMConnector extends TypedEmitter<Events> {
345
355
return ;
346
356
}
347
357
348
- this . logger . info ( "Connected to Manager" ) ;
349
-
350
- connection . socket
351
- . once ( "close" , async ( ) => {
352
- await this . handleConnectionClose ( ) ;
353
- } ) ;
358
+ this . logger . info ( "Connected..." ) ;
354
359
355
360
/**
356
361
* @TODO : Distinguish existing `connect` request and started communication (Manager handled this host
@@ -361,7 +366,7 @@ export class CPMConnector extends TypedEmitter<Events> {
361
366
this . connected = true ;
362
367
this . connectionAttempts = 0 ;
363
368
364
- connection . req . once ( "error" , async ( error : any ) => {
369
+ connection . res . once ( "error" , async ( error : any ) => {
365
370
this . logger . error ( "Request error" , error ) ;
366
371
367
372
try {
@@ -386,7 +391,7 @@ export class CPMConnector extends TypedEmitter<Events> {
386
391
* Handles connection close.
387
392
* Tries to reconnect.
388
393
*/
389
- async handleConnectionClose ( ) {
394
+ async handleConnectionClose ( connectionStatusCode : number ) {
390
395
this . handleCommunicationRequestEnd ( ) ;
391
396
392
397
this . connection ?. removeAllListeners ( ) ;
@@ -400,6 +405,10 @@ export class CPMConnector extends TypedEmitter<Events> {
400
405
clearInterval ( this . loadInterval ) ;
401
406
}
402
407
408
+ if ( connectionStatusCode === 403 ) {
409
+ this . isAbandoned = true ;
410
+ }
411
+
403
412
await this . reconnect ( ) ;
404
413
}
405
414
@@ -426,9 +435,9 @@ export class CPMConnector extends TypedEmitter<Events> {
426
435
this . isReconnecting = true ;
427
436
428
437
await new Promise < void > ( ( resolve , reject ) => {
429
- setTimeout ( async ( ) => {
430
- this . logger . info ( "Connection lost, retrying" , this . connectionAttempts ) ;
438
+ this . logger . info ( "Connection lost, retrying" , this . connectionAttempts ) ;
431
439
440
+ setTimeout ( async ( ) => {
432
441
await this . connect ( ) . then ( resolve , reject ) ;
433
442
} , this . config . reconnectionDelay ) ;
434
443
} ) ;
0 commit comments