@@ -11,13 +11,13 @@ import {Semaphore} from "async-mutex";
11
11
12
12
13
13
type ParamTypeMap = Map < CxParamType , string > ;
14
- const semaphore = new Semaphore ( 1 ) ; // Semaphore with 1 slot
15
-
16
14
17
15
export class CxWrapper {
18
- private static instances = new Map < string , CxWrapper > ( ) ; // Multiton pattern
16
+ private static instances = new Map < string , CxWrapper > ( ) ; // Multiton pattern
17
+ private static semaphore = new Semaphore ( 1 ) ; // Semaphore with 1 slot
19
18
config : CxConfig ;
20
19
cxInstaller : CxInstaller ;
20
+
21
21
private constructor ( cxScanConfig : CxConfig , logFilePath ?: string ) {
22
22
this . cxInstaller = new CxInstaller ( process . platform ) ;
23
23
this . config = new CxConfig ( ) ;
@@ -51,27 +51,30 @@ export class CxWrapper {
51
51
}
52
52
53
53
static async getInstance ( cxScanConfig : CxConfig , logFilePath : string ) : Promise < CxWrapper > {
54
- const [ , release ] = await semaphore . acquire ( ) ;
55
- const key = this . generateKey ( cxScanConfig , logFilePath ) ;
56
- let wrapper = CxWrapper . instances . get ( key ) ;
57
- if ( ! wrapper ) {
58
- wrapper = new CxWrapper ( cxScanConfig , logFilePath ) ;
59
- CxWrapper . instances . set ( key , wrapper ) ;
60
- await wrapper . init ( ) ;
54
+ const [ , release ] = await this . semaphore . acquire ( ) ;
55
+ try {
56
+ const key = this . generateKey ( cxScanConfig , logFilePath ) ;
57
+ let wrapper = CxWrapper . instances . get ( key ) ;
58
+ if ( ! wrapper ) {
59
+ wrapper = new CxWrapper ( cxScanConfig , logFilePath ) ;
60
+ CxWrapper . instances . set ( key , wrapper ) ;
61
+ await wrapper . init ( ) ;
62
+ }
63
+ return wrapper ;
64
+ } finally {
65
+ console . log ( `Semaphore released by process: ${ process . pid } ` ) ;
66
+ release ( ) ;
61
67
}
62
- release ( ) ;
63
-
64
- return wrapper ;
65
68
}
66
-
67
- static generateKey ( config :CxConfig , logFilePath :string ) : string {
69
+
70
+ static generateKey ( config : CxConfig , logFilePath : string ) : string {
68
71
return `${ config . baseUri } ${ config . baseAuthUri } ${ config . clientId } ${ config . clientSecret } ${ config . apiKey } ${ config . tenant } ${ config . additionalParameters } ${ config . pathToExecutable } ${ logFilePath } ` . toLowerCase ( ) ;
69
72
}
70
73
71
74
async init ( ) : Promise < void > {
72
75
return await this . cxInstaller . downloadIfNotInstalledCLI ( ) ;
73
76
}
74
-
77
+
75
78
public cloneWithNewConfig ( scanConfig : CxConfig ) : CxWrapper {
76
79
return new CxWrapper ( scanConfig ) ;
77
80
}
0 commit comments